我可以使用jdbc
代码在单个方法中执行两个不同的查询吗?
答案 0 :(得分:0)
尝试使用JDBC语句接口的 executeBatch()方法。
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();
}
}
答案 1 :(得分:-1)
是,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);
请您详细说明,您希望实现的目标。