1 public class JDBCUtils extends Database { 2 /** 3 * General query based on different tables. 4 * 5 * @param clazz clazz 6 * @param sql sql 7 * @param args args 8 * @param <T> t 9 * @return T 10 */ 11 public <T> T getInstance(Class<T> clazz, String sql, Object... args) { 12 try (PreparedStatement ps = connection.prepareStatement(sql)) { 13 for (int i = 0; i < args.length; i++) { 14 ps.setObject(i + 1, args[i]); 15 } 16 ResultSet resultSet = ps.executeQuery(); 17 ResultSetMetaData metaData = resultSet.getMetaData(); 18 int columnCount = metaData.getColumnCount(); 19 if (resultSet.next()) { 20 T t = clazz.getDeclaredConstructor().newInstance(); 21 for (int i = 0; i < columnCount; i++) { 22 Object columnValue = resultSet.getObject(i + 1); 23 String columnLabel = metaData.getColumnLabel(i + 1); 24 Field field = clazz.getDeclaredField(columnLabel); 25 field.setAccessible(true); 26 field.set(t, columnValue); 27 } 28 return t; 29 } 30 } catch (Exception e) { 31 e.printStackTrace(); 32 } 33 return null; 34 } 35 36 /** 37 * Set query based on different tables.. 38 * 39 * @param clazz clazz 40 * @param sql sql 41 * @param args args 42 * @param <T> t 43 * @return T 44 */ 45 public <T> List<T> getForList(Class<T> clazz, String sql, Object... args) { 46 try (PreparedStatement ps = connection.prepareStatement(sql)) { 47 for (int i = 0; i < args.length; i++) { 48 ps.setObject(i + 1, args[i]); 49 } 50 ResultSet resultSet = ps.executeQuery(); 51 ResultSetMetaData metaData = resultSet.getMetaData(); 52 int columnCount = metaData.getColumnCount(); 53 List<T> list = new ArrayList<>(); 54 while (resultSet.next()) { 55 T t = clazz.getDeclaredConstructor().newInstance(); 56 for (int i = 0; i < columnCount; i++) { 57 Object columnValue = resultSet.getObject(i + 1); 58 String columnLabel = metaData.getColumnLabel(i + 1); 59 Field field = clazz.getDeclaredField(columnLabel); 60 field.setAccessible(true); 61 field.set(t, columnValue); 62 } 63 list.add(t); 64 } 65 return list; 66 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 return null; 70 } 71 72 public int update(Connection conn, String sql, Object... args) { 73 try (PreparedStatement ps = conn.prepareStatement(sql);){ 74 for (int i = 0; i < args.length; i++) { 75 ps.setObject(i + 1, args[i]); 76 } 77 return ps.executeUpdate(); 78 } catch (Exception e) { 79 e.getMessage(); 80 } 81 return 0; 82 } 83 84 public <E> E getValue(Connection conn, String sql, Object... args) { 85 try(PreparedStatement ps = conn.prepareStatement(sql);){ 86 for (int i = 0; i < args.length; i++) { 87 ps.setObject(i+1, args[i]); 88 } 89 ResultSet resultSet = ps.executeQuery(); 90 if(resultSet.next()) { 91 return (E) resultSet.getObject(1); 92 } 93 } catch (Exception e) { 94 e.printStackTrace(); 95 } 96 return null; 97 98 } 99 }
使用泛型进行clazz传递:
1 public class JDBCUtils<T> extends Database { 2 3 private Class<T> clazz = null; 4 // 获取子类泛型中的实体类 5 { 6 Type genericSuperclass = this.getClass().getGenericSuperclass(); 7 ParameterizedType param = (ParameterizedType) genericSuperclass; 8 9 Type[] actualTypeArguments = param.getActualTypeArguments(); 10 clazz = (Class<T>) actualTypeArguments[0]; 11 } 12 /** 13 * General query based on different tables. 14 * 15 * @param sql sql 16 * @param args args 17 * @param <T> t 18 * @return T 19 */ 20 public <T> T getInstance(String sql, Object... args) { 21 try (PreparedStatement ps = connection.prepareStatement(sql)) { 22 for (int i = 0; i < args.length; i++) { 23 ps.setObject(i + 1, args[i]); 24 } 25 ResultSet resultSet = ps.executeQuery(); 26 ResultSetMetaData metaData = resultSet.getMetaData(); 27 int columnCount = metaData.getColumnCount(); 28 if (resultSet.next()) { 29 T t = (T) clazz.getDeclaredConstructor().newInstance(); 30 for (int i = 0; i < columnCount; i++) { 31 Object columnValue = resultSet.getObject(i + 1); 32 String columnLabel = metaData.getColumnLabel(i + 1); 33 Field field = clazz.getDeclaredField(columnLabel); 34 field.setAccessible(true); 35 field.set(t, columnValue); 36 } 37 return t; 38 } 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 return null; 43 } 44 45 /** 46 * Set query based on different tables.. 47 * 48 * @param sql sql 49 * @param args args 50 * @param <T> t 51 * @return T 52 */ 53 public <T> List<T> getForList(String sql, Object... args) { 54 try (PreparedStatement ps = connection.prepareStatement(sql)) { 55 for (int i = 0; i < args.length; i++) { 56 ps.setObject(i + 1, args[i]); 57 } 58 ResultSet resultSet = ps.executeQuery(); 59 ResultSetMetaData metaData = resultSet.getMetaData(); 60 int columnCount = metaData.getColumnCount(); 61 List<T> list = new ArrayList<>(); 62 while (resultSet.next()) { 63 T t = (T) clazz.getDeclaredConstructor().newInstance(); 64 for (int i = 0; i < columnCount; i++) { 65 Object columnValue = resultSet.getObject(i + 1); 66 String columnLabel = metaData.getColumnLabel(i + 1); 67 Field field = clazz.getDeclaredField(columnLabel); 68 field.setAccessible(true); 69 field.set(t, columnValue); 70 } 71 list.add(t); 72 } 73 return list; 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 return null; 78 } 79 80 public int update(Connection conn, String sql, Object... args) { 81 try (PreparedStatement ps = conn.prepareStatement(sql);){ 82 for (int i = 0; i < args.length; i++) { 83 ps.setObject(i + 1, args[i]); 84 } 85 return ps.executeUpdate(); 86 } catch (Exception e) { 87 e.getMessage(); 88 } 89 return 0; 90 } 91 92 public <E> E getValue(Connection conn, String sql, Object... args) { 93 try(PreparedStatement ps = conn.prepareStatement(sql);){ 94 for (int i = 0; i < args.length; i++) { 95 ps.setObject(i+1, args[i]); 96 } 97 ResultSet resultSet = ps.executeQuery(); 98 if(resultSet.next()) { 99 return (E) resultSet.getObject(1); 100 } 101 } catch (Exception e) { 102 e.printStackTrace(); 103 } 104 return null; 105 106 } 107 108 109 }