JDBCUtil

  1. public class JDBCUtils {
  2. private static String url;
  3. private static String user;
  4. private static String password;
  5. private static String driver;
  6. static {
  7. try {
  8. /**1.加载配置文件*/
  9. Properties pro = new Properties();
  10. /**配置文件与工具类在不同包下
  11. pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
  12. */
  13. pro.load(JDBCUtils.class.getResourceAsStream("jdbc.properties"));
  14. url = pro.getProperty("url");
  15. user = pro.getProperty("user");
  16. password = pro.getProperty("password");
  17. driver = pro.getProperty("driver");
  18. Class.forName(driver);
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. /**
  26. * 获取连接Connection对象
  27. */
  28. public static Connection getConnection() throws SQLException{
  29. return DriverManager.getConnection(url,user,password);
  30. }
  31. }

CRUDUtil

  1. public class CRUDUtils {
  2. private static Connection conn = null;
  3. private static PreparedStatement ps = null;
  4. private static ResultSet rs = null;
  5. //insert, update, delete
  6. /**
  7. * @Function commonUpdate
  8. * @Description 插入,更新,删除
  9. * @param sql 执行的SQL语句
  10. * @param objects SQL语句中的字段值
  11. */
  12. public static int commonUpdate(String sql, Object...objects) {
  13. try {
  14. conn = JDBCUtils.getConnection();
  15. ps = conn.prepareStatement(sql);
  16. if(objects != null && objects.length > 0) {
  17. for(int i = 0; i < objects.length; i++) {
  18. ps.setObject(i+1, objects[i]);
  19. }
  20. }
  21. return ps.executeUpdate();
  22. } catch (SQLException e) {
  23. e.printStackTrace();
  24. } finally {
  25. JDBCUtils.close(conn,ps,rs);
  26. }
  27. return 0;
  28. }
  29. //selectOne
  30. /**
  31. * @Function commonQueryOne
  32. * @Description 查找单条记录
  33. * @param sql 执行的SQL语句
  34. * @param cls 实体类对象
  35. * @param objects SQL语句中的限制条件
  36. */
  37. public static <E> E commonQueryOne(String sql, Class<E> cls, Object...objects) {
  38. E entity = null;
  39. try {
  40. conn = JDBCUtils.getConnection();
  41. ps = conn.prepareStatement(sql);
  42. if(objects != null && objects.length > 0) {
  43. for(int i = 0; i < objects.length; i++) {
  44. ps.setObject(i+1, objects[i]);
  45. }
  46. }
  47. //获取结果集
  48. rs = ps.executeQuery();
  49. /**
  50. * 以下通过数据库表中字段去查找实体类中的属性名
  51. */
  52. //获取结果集中对象的数量、列名等
  53. ResultSetMetaData rsmd = rs.getMetaData();
  54. //获取字段数
  55. int columnCount = rsmd.getColumnCount();
  56. while(rs.next()) {
  57. //ͨ通过反射获取实体类对象
  58. entity = cls.newInstance();
  59. for(int i = 0; i < columnCount; i++) {
  60. //获取字段名称
  61. String columnName = rsmd.getColumnName(i+1);
  62. //获取该字段对应的值ֵ
  63. Object columnValue = rs.getObject(columnName);
  64. //通过字段名获取属性,try{名称不匹配}catch{到配置文件查找对应属性名}
  65. Field field = null;
  66. try{
  67. field = cls.getDeclaredField(columnName);
  68. }catch (Exception e){
  69. Properties p = new Properties();
  70. String mappingFile = cls.getSimpleName() + "Mapping.properties";
  71. System.out.println(mappingFile);
  72. InputStream is = CRUDUtils.class.getClassLoader().getResourceAsStream(mappingFile);
  73. try{
  74. p.load(is);
  75. String fieldName = p.getProperty(columnName); //key=value -> user_name=username
  76. field = cls.getDeclaredField(fieldName);
  77. }catch(IOException ioe){
  78. ioe.printStackTrace();
  79. }
  80. }
  81. //将私有属性非可访问设置为可访问
  82. field.setAccessible(true);
  83. //给实体类中的属性赋值ֵ
  84. field.set(entity, columnValue);
  85. }
  86. }
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. } finally {
  90. JDBCUtils.close(conn, ps, rs);
  91. }
  92. return entity;
  93. }
  94. //selectAll
  95. /**
  96. * @Function commonQueryList
  97. * @Description 查找多条记录
  98. * @param sql 执行的SQL语句
  99. * @param cls 实体类对象
  100. * @param objects SQL语句中的限制条件
  101. */
  102. public static <E> List<E> commonQueryList(String sql, Class<E> cls, Object...objects) {
  103. List<E> list = new ArrayList<E>();
  104. E entity = null;
  105. try {
  106. conn = JDBCUtils.getConnection();
  107. ps = conn.prepareStatement(sql);
  108. if(objects != null && objects.length > 0) {
  109. for(int i = 0; i < objects.length; i++) {
  110. ps.setObject(i+1, objects[i]);
  111. }
  112. }
  113. rs = ps.executeQuery();
  114. ResultSetMetaData rsmd = rs.getMetaData();
  115. int columnCount = rsmd.getColumnCount();
  116. while(rs.next()) {
  117. entity = cls.newInstance();
  118. for(int i = 0; i < columnCount; i++) {
  119. String columnName = rsmd.getColumnName(i+1);
  120. Object columnValue = rs.getObject(columnName);
  121. //通过字段名获取属性,try{名称不匹配}catch{到配置文件查找对应名称}
  122. Field field = null;
  123. try{
  124. field = cls.getDeclaredField(columnName);
  125. }catch (Exception e){
  126. Properties p = new Properties();
  127. String mappingFile = cls.getSimpleName() + "Mapping.properties";
  128. InputStream is = CRUDUtils.class.getClassLoader().getResourceAsStream(mappingFile);
  129. try{
  130. p.load(is);
  131. String fieldName = p.getProperty(columnName); //key=value -> user_name=username
  132. field = cls.getDeclaredField(fieldName);
  133. }catch(IOException ioe){
  134. ioe.printStackTrace();
  135. }
  136. }
  137. field.setAccessible(true);
  138. field.set(entity, columnValue);
  139. }
  140. list.add(entity);
  141. }
  142. } catch (SQLException e) {
  143. e.printStackTrace();
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. } finally {
  147. JDBCUtils.close(conn, ps, rs);
  148. }
  149. return list;
  150. }
  151. }