JDBC工具类
public <T> List<T> selectQuery(String sql,Class<T> c,Object...args){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:///db1?useSSL=false&serverTimezone=UTC&characterEncoding=utf8", "root", "123456");
PreparedStatement ps=con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1,args[i]);
}
ResultSet rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
ArrayList<T> ts = new ArrayList<>();
while (rs.next()){
T t=c.newInstance();
for (int i = 1; i <= columnCount; i++) {
Object value = rs.getObject(i);
String columnName = md.getColumnName(i);
Field f = c.getDeclaredField(columnName);
f.setAccessible(true);
f.set(t,value);
}
ts.add(t);
}
return ts;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return null;
}
public int update(String sql,Object...args){
try {
Connection con = DriverManager.getConnection("jdbc:mysql:///db1?useSSL=false&serverTimezone=UTC&characterEncoding=utf8", "root", "123456");
PreparedStatement ps = con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1,args[i]);
}
int i = ps.executeUpdate();
return i;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public <T>T selectQueryForObject(String sql,Class<T> c,Object...args){
try {
PreparedStatement ps=con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1,args[i]);
}
ResultSet rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
T t=null;
if (rs.next()){
if ((sql.toLowerCase().contains("count")||sql.toLowerCase().contains("sum")||
sql.toLowerCase().contains("min")|| sql.toLowerCase().contains("max")||
sql.toLowerCase().contains("avg"))&& columnCount==1){
return (T) rs.getObject(1);
}
t=c.newInstance();
for (int i = 1; i <= columnCount; i++) {
Object value = rs.getObject(i);
String columnName = md.getColumnName(i);
Field f = c.getDeclaredField(columnName);
f.setAccessible(true);
f.set(t,value);
}
}
return t;
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return null;
}
public Map<String ,Object> Queryformap(String sql,Class c,Object...args) throws Exception {
PreparedStatement ps=con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1,args[i]);
}
ResultSet rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
HashMap<String, Object> msp = new HashMap<>();
if (rs.next()){
for (int i = 1; i <= columnCount; i++) {
Object object = rs.getObject(i);
String columnName = md.getColumnName(i);
msp.put(columnName,object);
}
return msp;
}
return null;
}
public List<Map<String ,Object>> QueryForlist(String sql,Object...args){
try {
PreparedStatement pstm = con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pstm.setObject(i+1,args[i]);
}
ResultSet rs = pstm.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
ArrayList<Map<String,Object>> list = new ArrayList<>();
while (rs.next()){
Map<String,Object> map = new HashMap();
for (int i = 1; i <= columnCount; i++) {
Object value = rs.getObject(i);
String columnName = md.getColumnName(i).toLowerCase();
map.put(columnName,value);
}
list.add(map);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}