A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. public class SqlHelper {
  7. // 定义需要的变量
  8. static Connection ct = null;
  9. static PreparedStatement ps = null;
  10. static ResultSet rs = null;
  11. // 连接数据库的参数
  12. private static String url = "jdbc:mysql://127.0.0.1:3306/hotel";
  13. private static String username = "root";
  14. private static String password = "henry";
  15. private static String drivername = "com.mysql.jdbc.Driver";
  16. //static Properties pp = null;
  17. //static FileInputStream fis = null;
  18. public SqlHelper(){
  19. System.out.println(" 0");
  20. // 加载驱动,只需要一次
  21. System.out.println(ct+" 1 0");
  22. //加载驱动
  23. try {
  24. System.out.println(ct+" 2 0");
  25. Class.forName(drivername);
  26. System.out.println(ct+" 3 0");
  27. try {
  28. ct=DriverManager.getConnection(url,username,password);
  29. System.out.println(ct+" 4 0");
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33. } catch (ClassNotFoundException e) {
  34. }
  35. }
  36. // 如果有多个增删改[需要考虑事务]
  37. // 比如:把一个学生,并同时更新该同学的所在班级的总人数
  38. public void executeUpdate2(String sql[], String[][] parameters) {
  39. try {
  40. // 1.获得连接
  41. //ct = getConnection();
  42. // 用户出入的可能是多个sql语句
  43. ct.setAutoCommit(false);
  44. for (int i = 0; i < sql.length; i++) {
  45. if (parameters[i] != null) {
  46. ps = ct.prepareStatement(sql[i]);
  47. for (int j = 0; j < parameters[i].length; j++) {
  48. ps.setString(j + 1, parameters[i][j]);
  49. }
  50. ps.executeUpdate();
  51. }
  52. }
  53. ct.commit();
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. throw new RuntimeException(e.getMessage());
  57. } finally {
  58. close();
  59. }
  60. }
  61. // 一个增删改
  62. // sql格式:update 表名 set 字段名=? where 字段=?
  63. public static void executeUpdate(String sql, String[] parameters) {
  64. // 1.创建一个ps
  65. //ct = getConnection();
  66. try {
  67. System.out.println(ct+" 5 0");
  68. System.out.println(sql);
  69. System.out.println(parameters[0]);
  70. <FONT color=red>ps = ct.prepareStatement(sql);//.....这里怎么回事啊 搞什么飞机啊  </FONT>
  71. // 给?赋值
  72. for (int i = 0; i < parameters.length; i++) {
  73. ps.setString(i + 1, parameters[i]);
  74. }
  75. // 执行
  76. ps.executeUpdate();
  77. } catch (SQLException e) {
  78. e.printStackTrace();
  79. // 执行异常,抛出异常(好处:可以给调用的函数可以处理也可以不处理)
  80. throw new RuntimeException(e.getMessage());
  81. } finally {
  82. // 关闭资源
  83. close();
  84. }
  85. }
  86. // 关闭资源的方法
  87. public static void close() {
  88. try {
  89. if (rs != null)
  90. rs.close();
  91. if (ps != null)
  92. ps.close();
  93. if (ct != null)
  94. ct.close();
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
田建 + 1

查看全部评分

1 个回复

倒序浏览
申请加分
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马