首页 > 其他 > 详细

可变参数和preparestatement 结合使用

时间:2016-11-28 08:33:57      阅读:207      评论:0      收藏:0      [点我收藏+]

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 结合使用

原文:http://www.cnblogs.com/afterhours/p/6108052.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!