Guest User

Untitled

a guest
Nov 27th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. package com.maysnow;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. /**
  13. * 访问数据库的工具类
  14. * @author MaySnow
  15. *
  16. */
  17. public class DBHelp<T> {
  18. //一定要先导入jar包mysql-connector-java-5.0.5-bin.jar
  19. private final String DRIVER = "com.mysql.jdbc.Driver";
  20. private final String URL = "jdbc:mysql://localhost:3306/mydb";
  21. private final String NAME = "root";
  22. private final String PWD = "root";
  23.  
  24. /**
  25. * 获取数据库连接对象
  26. * @return Connection类的对象
  27. */
  28. public Connection getConnection(){
  29. Connection conn = null;
  30. try {
  31. Class.forName(DRIVER);
  32. conn = DriverManager.getConnection(URL, NAME, PWD);
  33. } catch (ClassNotFoundException e) {
  34. e.printStackTrace();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. return conn;
  39. }
  40.  
  41. public List<T> executeQueryForList(String sql, RowMapper<T> rm, Object...args) { //相当于是一个args数组,里面是Object类型的数
  42. Connection conn= null;
  43. PreparedStatement stat = null;
  44. ResultSet rs = null;
  45.  
  46. List<T> list = new ArrayList<T>();
  47.  
  48. try {
  49. conn = getConnection();//连接数据库
  50. stat = conn.prepareStatement(sql);//创建一个preparedStatement对象来将参数化的sql语句发送到数据库
  51.  
  52. //给指定参数赋值
  53. for(int i = 0; i < args.length; i++) {
  54. stat.setObject(i + 1, args[i]);
  55. }
  56.  
  57. rs = stat.executeQuery();
  58.  
  59. while(rs.next()) { //rs.next(),Resultset光标最初位于第一行之前;当第一次调用next()方法使第一行成为当前行;第二次调用使第二行成为当前行;依次类推
  60. T obj = rm.mapRow(rs);
  61. list.add(obj);
  62. }
  63. } catch (SQLException e) {
  64. e.printStackTrace();
  65. } finally {
  66. close(rs, stat, conn);
  67. }
  68. return list;
  69.  
  70. }
  71.  
  72. public T executeQueryForObject(String sql, RowMapper<T> rm, Object...args) {
  73. Connection conn = null;
  74. PreparedStatement stat = null;
  75. ResultSet rs = null;
  76. T obj = null;
  77.  
  78. try {
  79. conn = getConnection();
  80. stat = conn.prepareStatement(sql);
  81.  
  82. for(int i = 0; i<args.length; i++) {
  83. stat.setObject(i + 1, args[i]);
  84. }
  85.  
  86. rs = stat.executeQuery();
  87.  
  88. if(rs.next()) {
  89. obj = rm.mapRow(rs);
  90. }
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. } finally {
  94. close(rs, stat, conn);
  95. }
  96. return obj;
  97. }
  98.  
  99. /**
  100. *执行insert、update、delete语句
  101. * @param sql insert or update or delete
  102. * @param args
  103. * @return true代表成功, false代表失败
  104. */
  105. public boolean executeSQL(String sql, Object...args) {
  106. Connection conn = null;
  107. PreparedStatement stat = null;
  108.  
  109. try {
  110. conn = getConnection();
  111. stat = conn.prepareStatement(sql);
  112.  
  113. for(int i = 0; i<args.length; i++) {
  114. stat.setObject(i + 1, args[i]);
  115. }
  116.  
  117. int rows = stat.executeUpdate();
  118. if(rows > 0) {
  119. return true;
  120. }
  121. } catch (SQLException e) {
  122. e.printStackTrace();
  123. } finally {
  124. close(stat, conn);
  125. }
  126.  
  127. return false;
  128. }
  129.  
  130. /**
  131. * 释放数据库资源(注意finally)
  132. * @param rs
  133. * @param stat
  134. * @param conn
  135. */
  136. public void close(ResultSet rs, Statement stat, Connection conn) {
  137. try {
  138. if(rs != null) {
  139. rs.close();
  140. }
  141. } catch (SQLException e) {
  142. e.printStackTrace();
  143. } finally {
  144. try {
  145. if(stat != null) {
  146. stat.cancel();
  147. }
  148. } catch (SQLException e) {
  149. e.printStackTrace();
  150. } finally {
  151. try {
  152. if(conn != null) {
  153. conn.close();
  154. }
  155. } catch (SQLException e) {
  156. e.printStackTrace();
  157. }
  158. }
  159. }
  160. }
  161.  
  162. /**
  163. * 释放数据库资源(方法重载)
  164. * @param stat
  165. * @param conn
  166. */
  167. public void close(Statement stat, Connection conn) {
  168. close(null, stat, conn);
  169. }
  170.  
  171. }
Add Comment
Please, Sign In to add comment