写一个方法可以执行多个SQL语句吗?jsp,mysql

来源:百度知道 编辑:UC知道 时间:2024/05/30 19:25:42
就是定义一个方法,让里边执行多个SQL语句,可以写吗?怎么写,不能,为什么?

方法里可以写,但是必须注意要正确使用和关闭Statement和ResultSet,否则可能会出现游标用尽的情况.

楼上的可以试下,但是好象这种情况好象没有这样写的吧!另外,这样插入的话,肯定是相关的表中,提倡使用存储过程,那样安全些!

。。。。。。批处理很好。。。 比存储过程的效率高很多。。。

用批处理:
Class.forName("sqldriver");
Connection conn = DriverManager.getConnection(URL,USER,PASS)
Statement stmt = conn.createStatement();
stmt.addBatch("insert into authors(firstName,lastName) values(''fegor'',''hack'')");
stmt.addBatch("insert into authors(firstName,lastName) values(''fegors'',''hacks'')");
stmt.addBatch("insert into authors(firstName,lastName) values(''fegorsr'',''hacksr'')");
stmt.executeBatch();
stmt.close();
conn.close();

把sql 做为参数放进去不就可以了吗