【发布时间】:2011-08-13 17:46:16
【问题描述】:
我可以使用jdbc 代码在一个方法中执行两个不同的查询吗?
【问题讨论】:
标签: jdbc
我可以使用jdbc 代码在一个方法中执行两个不同的查询吗?
【问题讨论】:
标签: jdbc
试试executeBatch() JDBC Statement 接口的方法。
import java.sql.*;
class Demo{
public static void main(String args[])throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","password");
con.setAutoCommit(false);
Statement stmt=con.createStatement();
stmt.addBatch("insert into emp values(001,'abc',40000)");
stmt.addBatch("insert into emp values(002,'mni',50000)");
stmt.executeBatch();//executing the batch
con.commit();
con.close();
}
}
【讨论】:
是的,2 次更新作为单个语句
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc.url","jdbc.username","jdbc.password");
java.sql.Statement statement = conn.createStatement();
String sqlStr = "update tab set col1= 'X' \n update tab_not_exist set col1='X2'";
statemet.execute(sqlStr);
你能详细说明一下,你希望达到什么目标。
【讨论】: