批处理
public static Integer addBatch(String[] sqls){
init();
try {
//设置关闭自动提交
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
for (int i = 0; i < sqls.length; i++) {
stmt.addBatch(sqls[i]);
}
//批量执行
stmt.executeBatch();
//提交事务
conn.commit();
return 1;
} catch (SQLException e) {
e.printStackTrace();
return 0;
}finally {
close();
}
}
批处理在main方法中调用
String[] sql3 = new String[3];
sql3[0] = "insert into student(s_name, s_age)values('zs',11);";
sql3[1] = "insert into student(s_name, s_age)values('lis',22);";
sql3[2] = "insert into student(s_name, s_age)values('ww',33);";
int res = addBatch(sql3);
System.out.println(res);
通用泛型查询方法
public static <T> List<T> query1(String sql, Class<T> cls, Object...params){
init();
try {
//编译sql语句,返回PrepareStatement对象
ps = conn.prepareStatement(sql);
//参数替换sql中的占位符
if (params != null) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i+1,params[i]);
}
}
//执行sql语句,结果集封装给ResultSet
rs = ps.executeQuery();
List<T> list = new ArrayList<T>();
//检索resultSet对象中的列数,类型和字段(结果集元数据)
ResultSetMetaData resultSetMetaData = rs.getMetaData();
while(rs.next()){
T t = cls.newInstance();//泛型实例化
//获取结果集的列数
int count = resultSetMetaData.getColumnCount();
for (int i = 0; i < count; i++) {
//获取每一列的别名,如果没有别名就返回resultSetMetaData.getColumnName();
String cName = resultSetMetaData.getColumnLabel(i + 1);
Object cValue = rs.getObject(cName);//通过获取的别名或者列名获取列的值
try {
Field field = cls.getDeclaredField(cName);//通过获取的别名或者列名获取类的属性
field.setAccessible(true);//开启私有访问权限
field.set(t, cValue);//变量赋值
} catch (NoSuchFieldException e) {
}
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
close();
}
}
下划线转驼峰命名法
//下划线转驼峰命名
public static void conversion(String column){
// String[] conName = name.split("");
// String string = "";
// for (int i = 0; i < conName.length; i++) {
// if(conName[i].equals("_")){
// string = string + conName[i+1].toUpperCase();
// i++;
// }else {
// string = string +conName[i];
// }
// }
// System.out.println(string);
String[] arr = column.split("_");
StringBuffer sf = new StringBuffer(arr[0]);
for (int i = 1; i < arr.length; i++) {
String string = arr[i];
String firstStr = string.substring(0, 1).toUpperCase();
}
}
标签:conName,JDBC,下划线,int,day24,sql3,++,String,string
From: https://www.cnblogs.com/xiaoto9426/p/16850330.html