BaseDao.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.sf.jdbctest;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import com.alibaba.druid.pool.DruidDataSourceFactory;
  4. import com.alibaba.druid.util.JdbcUtils;
  5. import org.apache.commons.dbutils.DbUtils;
  6. import org.junit.Test;
  7. import javax.sql.DataSource;
  8. import java.io.IOException;
  9. import java.lang.reflect.Field;
  10. import java.sql.*;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Properties;
  14. import static sun.plugin.javascript.navig.JSType.URL;
  15. /**
  16. * 通过数据库的操作类
  17. * @param <T>
  18. */
  19. public class BaseDao<T> {
  20. /**
  21. * 通过的增删改
  22. */
  23. public void update(Connection conn,String sql,Object...args) throws SQLException {
  24. PreparedStatement ps = conn.prepareStatement(sql);
  25. //赋值
  26. if(args != null && args.length>0){
  27. for (int i = 0;i<args.length;i++){
  28. ps.setObject(i+1,args[i]);
  29. }
  30. }
  31. ps.executeUpdate();
  32. }
  33. /**
  34. * 查询列表
  35. * 原生反射
  36. * @return
  37. */
  38. public List<T> getList(Connection connection , String sql ,Class<T> clazz ,Object...args)throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
  39. List<T> list = new ArrayList<>();
  40. T t = null;
  41. //获取PreparedStatement
  42. PreparedStatement ps = connection.prepareStatement(sql);
  43. //赋值
  44. if(args!=null && args.length>0) {
  45. for(int i=0;i<args.length;i++) {
  46. ps.setObject(i+1, args[i]);
  47. }
  48. }
  49. //执行
  50. ResultSet rs = ps.executeQuery();
  51. //获取数据库表的元数据信息
  52. ResultSetMetaData rsmd = rs.getMetaData();
  53. //获取数据库表的列总数
  54. int columnCount = rsmd.getColumnCount();
  55. while(rs.next()) {
  56. //实例化对象
  57. t=clazz.newInstance();
  58. //循环获取列名
  59. for(int i=0;i<columnCount;i++) {
  60. //通过列的下标获取列名
  61. String columnLabel = rsmd.getColumnLabel(i+1);
  62. //通过列标签获取列值
  63. Object columnValue = rs.getObject(columnLabel);
  64. //反射通过名称获取java对象中描述属性的对象
  65. Field field = clazz.getDeclaredField(columnLabel);//dept_id as deptId
  66. //取消私有权限
  67. field.setAccessible(true);
  68. //给属性对象赋值
  69. field.set(t, columnValue);
  70. }
  71. list.add(t);
  72. }
  73. return list;
  74. }
  75. /**
  76. * 通用查询(单个)
  77. * 原生反射
  78. */
  79. public T getT(Connection connection,String sql,Class<T> clazz,Object ...args) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
  80. T t = null;
  81. //获取PreparedStatement
  82. PreparedStatement ps = connection.prepareStatement(sql);
  83. //赋值
  84. if(args!=null && args.length>0) {
  85. for(int i=0;i<args.length;i++) {
  86. ps.setObject(i+1, args[i]);
  87. }
  88. }
  89. //执行
  90. ResultSet rs = ps.executeQuery();
  91. //获取数据库表的元数据信息
  92. ResultSetMetaData rsmd = rs.getMetaData();
  93. //获取数据库表的列总数
  94. int columnCount = rsmd.getColumnCount();
  95. if(rs.next()) {
  96. //实例化对象
  97. t=clazz.newInstance();
  98. //循环获取列名
  99. for(int i=0;i<columnCount;i++) {
  100. //通过列的下标获取列名
  101. String columnLabel = rsmd.getColumnLabel(i+1);
  102. //通过列标签获取列值
  103. Object columnValue = rs.getObject(columnLabel);
  104. //反射通过名称获取java对象中描述属性的对象
  105. Field field = clazz.getDeclaredField(columnLabel);//dept_id as deptId
  106. //取消私有权限
  107. field.setAccessible(true);
  108. //给属性对象赋值
  109. field.set(t, columnValue);
  110. }
  111. }
  112. return t;
  113. }
  114. // /**
  115. // * 通用查询(单个)
  116. // * 使用下面工具进行反射赋值
  117. // * commons-beanutils-1.8.0.jar
  118. // * commons-logging-1.1.1.jar
  119. // */
  120. // public T getT_(Connection connection,String sql,Class<T> clazz,Object ...args) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, InvocationTargetException, NoSuchMethodException {
  121. // T t = null;
  122. // //获取PreparedStatement
  123. // PreparedStatement ps = connection.prepareStatement(sql);
  124. // //赋值
  125. // if(args!=null && args.length>0) {
  126. // for(int i=0;i<args.length;i++) {
  127. // ps.setObject(i+1, args[i]);
  128. // }
  129. // }
  130. // //执行
  131. // ResultSet rs = ps.executeQuery();
  132. // //获取数据库表的元数据信息
  133. // ResultSetMetaData rsmd = rs.getMetaData();
  134. // //获取数据库表的列总数
  135. // int columnCount = rsmd.getColumnCount();
  136. // if(rs.next()) {
  137. // //实例化对象
  138. // t=clazz.newInstance();
  139. // //循环获取列名
  140. // for(int i=0;i<columnCount;i++) {
  141. // //通过列的下标获取列名
  142. // String columnLabel = rsmd.getColumnLabel(i+1);
  143. // //通过列标签获取列值
  144. // Object columnValue = rs.getObject(columnLabel);
  145. // //反射通过名称获取java对象中描述属性的对象
  146. // //Field field = clazz.getDeclaredField(columnLabel);//dept_id as deptId
  147. // //取消私有权限
  148. // //field.setAccessible(true);
  149. // //给属性对象赋值
  150. // //field.set(t, columnValue);
  151. // PropertyUtils.setProperty(t, columnLabel, columnValue);
  152. // }
  153. // }
  154. // return t;
  155. // }
  156. /**
  157. * 数据库连接池
  158. */
  159. @Test
  160. public void t11() throws SQLException {
  161. DruidDataSource ds = new DruidDataSource();
  162. //设置初始化参数
  163. ds.setUrl("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8");
  164. ds.setUsername("root");
  165. ds.setPassword("root");
  166. ds.setDriverClassName("com.mysql.jdbc.Driver");
  167. Connection conn = ds.getConnection();
  168. String sql = "INSERT INTO user(user_name,price,create_time) VALUES(?,?,?)";
  169. // 预编译
  170. PreparedStatement pre = conn.prepareStatement(sql);
  171. pre.setString(1,"admin");
  172. pre.setDouble(2,100.21);
  173. pre.setDate(3,new Date(new java.util.Date().getTime()));
  174. // 执行进行绑定参数之后的sql语句
  175. int i = pre.executeUpdate();
  176. System.out.println(i);
  177. }
  178. /**
  179. * 连接池2
  180. */
  181. @Test
  182. public void t12() throws Exception {
  183. //听过加载外部配置文件的方式
  184. Properties p = new Properties();
  185. p.load(JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"));
  186. //采用Druid提供的工厂类创建DruidDataSource对象
  187. DataSource dataSource = DruidDataSourceFactory.createDataSource(p);
  188. Connection conn = dataSource.getConnection();
  189. String sql = "INSERT INTO user(user_name,price,create_time) VALUES(?,?,?)";
  190. // 预编译
  191. PreparedStatement pre = conn.prepareStatement(sql);
  192. pre.setString(1,"admin");
  193. pre.setDouble(2,100.21);
  194. pre.setDate(3,new Date(new java.util.Date().getTime()));
  195. // 执行进行绑定参数之后的sql语句
  196. int i = pre.executeUpdate();
  197. System.out.println(i);
  198. }
  199. }