利用PreparedStatement如何执行多条SQL语句 如题: 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public void addBatch(String[] sql) throws CommDAOException{ Connection conn = null; Statement stmt = null; try { conn = getConnection(); stmt = conn.createStatement(); if (sql != null) { for (int i = 0; i < sql.length; i++) { stmt.addBatch( (String) sql[i]); } stmt.executeBatch(); } } catch (Exception e) { throw new CommDAOException(e.getMessage()); } finally { DataBase.closeCSR(conn, stmt, null); } } 不对吧,Statement有批处理,PreparedStatement没有~! PreparedStatement处理无参数sql方法与2楼基本一致,要处理带参数的sql时,可以这样:PreparedStatement pstmt = conn.prepareStatement(sql)pstmt.setXXX();...pstmt.addBatch();pstmt.setXXX();...pstmt.addBatch();...pstmt.executeBatch();注意的是,它还无法处理多条不同的带参数的sql语句。 String sql= insert into Student(id,name) value(?,?);Student s=new Student();s.setId(1);s.setName(张三);Connection conn=null;PreparedStatement ps=null;try{ Class.forName(driver); conn=DriverManager.getConnection(url,username,password); ps=conn.prepareStatement(sql); ps.setInt(1,s.getId()); ps.setString(2,s.getName()); ps.executeUpdate(); }catch(){} java编个小工具求帮助!! java能编译不能运行出现这样的错误 JAVA C/S 结构的数据库问题 一条sql语句? 只有几行代码,大家帮看看.看不懂 关于java网络的一个不知道问题在哪儿的问题。 如何实现float 到 int 的类型转换,string 到 int 的类型转换? 明天我去应聘----大家帮我出出注意 哪里有rose下载,除了rational公司的和umlchina的连接?? rmi编程问题,SOS 有C++和一些.Net 基础,应该怎样学Java? 帮忙看一段Java生成彩色验证码的程序把
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn.createStatement();
if (sql != null) {
for (int i = 0; i < sql.length; i++) {
stmt.addBatch( (String) sql[i]);
}
stmt.executeBatch();
} }
catch (Exception e) {
throw new CommDAOException(e.getMessage());
}
finally {
DataBase.closeCSR(conn, stmt, null);
} }
PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setXXX();
...
pstmt.addBatch();
pstmt.setXXX();
...
pstmt.addBatch();
...
pstmt.executeBatch();注意的是,它还无法处理多条不同的带参数的sql语句。
Student s=new Student();
s.setId(1);
s.setName(张三);
Connection conn=null;
PreparedStatement ps=null;
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,username,password);
ps=conn.prepareStatement(sql);
ps.setInt(1,s.getId());
ps.setString(2,s.getName());
ps.executeUpdate();
}catch(){}