首页 > 代码库 > 可变参数和preparestatement 结合使用
可变参数和preparestatement 结合使用
public static void updat(String sql,Object...args) throws Exception{
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection=getconnetion();
preparedStatement=(PreparedStatement) connection.prepareStatement(sql);
for(int i=0;i<args.length;i++){
preparedStatement.setObject(i+1, args[i]);
}
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
-----------------------------------------
测试
@Test
public void test2(){
Student student=new Student();
String sql="insert into users values(?,?,?)";
student.setId(5211314);
student.setNameString("Myongyuan");
student.setSconString("LaiShan");
try {
Tools.updat(sql, student.getId(),student.getNameString(),student.getSconString());
} catch (Exception e) {
e.printStackTrace();
}
}
可变参数和preparestatement 结合使用