[英]How to execute multiple SQL statements from java
I want to execute the multiple queries or job in one execute. 我想一次执行多个查询或作业。 Something like this eg: 像这样的东西:
String query="select * from tab1;insert into tab1 values(...);update tab1..;delete from tab1...;"
Statement st = con1.createStatement();
ResultSet rs = st.executeQuery(query);
Or multiple select queries.Queries will be dynamic. 或多个选择查询。查询将是动态的。
But I am not able to do this.What is the way to run multiple queries separated by semi colon. 但是我无法做到这一点,以半冒号分隔的多个查询的运行方式是什么?
you can achieve that using Following example uses addBatch & executeBatch commands to execute multiple SQL commands simultaneously. 您可以使用以下示例来实现这一点,该示例使用addBatch和executeBatch命令同时执行多个SQL命令。
Batch Processing allows you to group related SQL statements into a batch and submit them with one call to the database. 批处理允许您将相关的SQL语句分组为一个批处理,并通过一次调用将其提交给数据库。 reference 参考
When you send several SQL statements to the database at once, you reduce the amount of communication overhead, thereby improving performance. 当您一次向数据库发送多个SQL语句时,可以减少通信开销,从而提高性能。
DatabaseMetaData.supportsBatchUpdates()
method to determine if the target database supports batch update processing. 您应该使用DatabaseMetaData.supportsBatchUpdates()
方法来确定目标数据库是否支持批量更新处理。 The method returns true if your JDBC driver supports this feature. 如果您的JDBC驱动程序支持此功能,则该方法返回true。 executeBatch()
is used to start the execution of all the statements grouped together. executeBatch()
用于开始执行分组在一起的所有语句。 addBatch()
method. 此方法删除使用addBatch()
方法添加的所有语句。 However, you cannot selectively choose which statement to remove. 但是,您不能有选择地选择要删除的语句。 EXAMPLE: 例:
import java.sql.*;
public class jdbcConn {
public static void main(String[] args) throws Exception{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/testDb","name","pass");
Statement stmt = con.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String insertEmp1 = "insert into emp values
(10,'jay','trainee')";
String insertEmp2 = "insert into emp values
(11,'jayes','trainee')";
String insertEmp3 = "insert into emp values
(12,'shail','trainee')";
con.setAutoCommit(false);
stmt.addBatch(insertEmp1);//inserting Query in stmt
stmt.addBatch(insertEmp2);
stmt.addBatch(insertEmp3);
ResultSet rs = stmt.executeQuery("select * from emp");
rs.last();
System.out.println("rows before batch execution= "
+ rs.getRow());
stmt.executeBatch();
con.commit();
System.out.println("Batch executed");
rs = stmt.executeQuery("select * from emp");
rs.last();
System.out.println("rows after batch execution= "
+ rs.getRow());
}
}
refer http://www.tutorialspoint.com/javaexamples/jdbc_executebatch.htm 请参阅http://www.tutorialspoint.com/javaexamples/jdbc_executebatch.htm
I'm not sure that you want to send two SELECT statements in one request statement because you may not be able to access both ResultSet
s. 我不确定您是否要在一个请求语句中发送两个SELECT语句,因为您可能无法访问两个ResultSet
。 The database may only return the last result set. 数据库可能仅返回最后的结果集。
Multiple ResultSets 多个结果集
However, if you're calling a stored procedure that you know can return multiple resultsets something like this will work 但是,如果您正在调用一个知道可以返回多个结果集的存储过程,则可以执行以下操作
CallableStatement stmt = con.prepareCall(...);
try {
...
boolean results = stmt.execute();
while (results) {
ResultSet rs = stmt.getResultSet();
try {
while (rs.next()) {
// read the data
}
} finally {
try { rs.close(); } catch (Throwable ignore) {}
}
// are there anymore result sets?
results = stmt.getMoreResults();
}
} finally {
try { stmt.close(); } catch (Throwable ignore) {}
}
Multiple SQL Statements 多个SQL语句
If you're talking about multiple SQL statements and only one SELECT then your database should be able to support the one String
of SQL. 如果您正在谈论多个SQL语句和一个SELECT,那么您的数据库应该能够支持一个SQL String
。 For example I have used something like this on Sybase 例如,我在Sybase上使用了类似的方法
StringBuffer sql = new StringBuffer( "SET rowcount 100" );
sql.append( " SELECT * FROM tbl_books ..." );
sql.append( " SET rowcount 0" );
stmt = conn.prepareStatement( sql.toString() );
This will depend on the syntax supported by your database. 这将取决于数据库支持的语法。 In this example note the addtional spaces
padding the statements so that there is white space between the staments. 在此示例中,请注意在语句之间填充了其他spaces
,以便在各步之间有空白。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.