"SELECT FROM * TABLE;INSERT INTO TABLE;"
我确实意识到可以拆分SQL查询字符串并执行两次语句,但是我想知道是否存在一次性方法。
>
#1 楼
我想知道是否可以使用JDBC执行类似的操作。"SELECT FROM * TABLE;INSERT INTO TABLE;"
是可以的。据我所知,有两种方法。它们是
通过设置数据库连接属性以允许多个查询,
默认情况下用分号分隔。
通过调用存储过程返回隐式游标。
以下示例演示了上述两种可能性。
示例1 :(要允许多个查询):
在发送连接请求时,您需要将连接属性
allowMultiQueries=true
附加到数据库URL。这是autoReConnect=true
等已经存在的属性的附加连接属性。allowMultiQueries
属性的可接受值为true
,false
,yes
和no
。在运行时使用SQLException
拒绝任何其他值。 String dbUrl = "jdbc:mysql:///test?allowMultiQueries=true";
除非通过此指令,否则将引发
SQLException
。查询执行。 boolean hasMoreResultSets = stmt.execute( multiQuerySqlString );
要迭代并处理结果,您需要执行以下步骤:
READING_QUERY_RESULTS: // label
while ( hasMoreResultSets || stmt.getUpdateCount() != -1 ) {
if ( hasMoreResultSets ) {
Resultset rs = stmt.getResultSet();
// handle your rs here
} // if has rs
else { // if ddl/dml/...
int queryResult = stmt.getUpdateCount();
if ( queryResult == -1 ) { // no more queries processed
break READING_QUERY_RESULTS;
} // no more queries processed
// handle success, failure, generated keys, etc here
} // if ddl/dml/...
// check to continue in the loop
hasMoreResultSets = stmt.getMoreResults();
} // while results
示例2:步骤按照:
使用一个或多个
execute( String sql )
和select
查询创建一个过程。使用
DML
从Java调用它。 DML结果无法捕获,但可以发出另一个
CallableStatement
以查找表中行的影响方式。
示例表和过程:
ResultSet
从Java:
CallableStatement cstmt = con.prepareCall( "call multi_query()" );
boolean hasMoreResultSets = cstmt.execute();
READING_QUERY_RESULTS:
while ( hasMoreResultSets ) {
Resultset rs = stmt.getResultSet();
// handle your rs here
} // while has more rs
评论
不幸的是,这不适用于嵌入式版本的Derby。
–user2428118
15年4月24日在9:07
@ user2428118:原因?是否发现任何错误?您是否检查了驱动程序是否支持此功能?
–破坏者雷迪
15年4月24日在9:12
我添加了allowMultiQueries = true并可以正常工作:)
–哈兹姆
16-09-19在12:20
谢谢@RavinderReddy
– Siva R
17年8月4日在8:45
#2 楼
您可以使用批处理更新,但查询必须是操作(即插入,更新和删除)查询Statement s = c.createStatement();
String s1 = "update emp set name='abc' where salary=984";
String s2 = "insert into emp values ('Osama',1420)";
s.addBatch(s1);
s.addBatch(s2);
s.executeBatch();
评论
您不能将这种方法用于“调用sprocname('abc',984)”查询吗?
–sebnukem
16-10-29在0:51
#3 楼
提示:如果您具有多个连接属性,请使用以下命令将它们分开:&
为您提供类似的东西: >
希望这对您有所帮助。
,
Glyn
#4 楼
根据我的测试,正确的标志是“ allowMultiQueries = true”#5 楼
您为什么不尝试为此写一个Stored Procedure
?您可以得到
Result Set
,而在同一个Stored Procedure
中可以得到您想要的东西。唯一的是您如果在
Insert
之后添加Result Set
,则可能不会在Insert
中获得新插入的行。评论
您并非总是具有编写存储过程的权限。例如。这是您只能选择的客户数据库。
–pedram bashiri
18-10-24在15:52
#6 楼
我认为这是多种选择/更新/插入/删除的最简单方法。选择后,您可以根据需要运行任意数量的更新/插入/删除(您必须先使用selectUpdate(str)进行选择(如果需要,则为虚拟对象))(只需使用new int(count1,count2等))如果您需要新的选择,请关闭“语句”和“连接”,并为下一个选择做新的选择。例如:String str1 = "select * from users";
String str9 = "INSERT INTO `port`(device_id, potition, port_type, di_p_pt) VALUE ('"+value1+"', '"+value2+"', '"+value3+"', '"+value4+"')";
String str2 = "Select port_id from port where device_id = '"+value1+"' and potition = '"+value2+"' and port_type = '"+value3+"' ";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
theConnection=(Connection) DriverManager.getConnection(dbURL,dbuser,dbpassword);
theStatement = theConnection.prepareStatement(str1);
ResultSet theResult = theStatement.executeQuery();
int count8 = theStatement.executeUpdate(str9);
theStatement.close();
theConnection.close();
theConnection=DriverManager.getConnection(dbURL,dbuser,dbpassword);
theStatement = theConnection.prepareStatement(str2);
theResult = theStatement.executeQuery();
ArrayList<Port> portList = new ArrayList<Port>();
while (theResult.next()) {
Port port = new Port();
port.setPort_id(theResult.getInt("port_id"));
portList.add(port);
}
希望对您有所帮助
评论
打开数据库连接非常昂贵。每次都这样做不是一个好习惯。您提供的应当使n个查询的数据库命中次数达到n个,从而导致性能下降。
–阿伦·库马尔(Arun Kumar Mudraboyina)
17年7月25日在4:36
评论
放入存储过程中,调用该存储过程。意味着当您要进行更改时,您也不必部署代码。您必须在连接字符串allowMultiQueries = true中设置一个属性。
可能重复:如何在Java中执行复合SQL查询?[1] [1]:stackoverflow.com/questions/6773393/…
嗨,Rahul,对于这个项目,我正在使用一个普通的旧连接对象,您知道应该在哪里设置“ allowMultiQueries = true”。在问题中添加了连接对象代码