我使用JDBC连接到远程数据库,然后使用ibatis ScriptRunner来执行查询。
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(url3);
ScriptRunner runner = new ScriptRunner(con);
runner.setSendFullScript(true);
runner.setStopOnError(true);
runner.runScript(new BufferedReader(new FileReader("C:/Script/Test.sql")));
此代码为我执行脚本,数据库中有2行受影响。有没有办法获取和打印受影响的行数,类似于在SQL Server Management Studio中执行脚本时在Messages中看到的内容?
编辑:这是我试图跑的脚本
USE Dashboard;
DECLARE @userName1 varchar(MAX);
SET @userName1 = 'jrambo';
DECLARE @ExternalUserID1 int;
SET @ExternalUserID1 = (SELECT ExternalUserID FROM ExternalUsers WHERE username = @userName1);
DELETE FROM DBO.INTERESTPREFS WHERE InterestID in (SELECT InterestID FROM DBO.INTERESTS WHERE externaluserid IN (@ExternalUserID1));
答案 0 :(得分:0)
您可以使用JDBC executeBatch()方法而不是使用ScriptRunner来执行此操作:
conn.setAutoCommit(false);
String line = null;
BufferedReader in =
new BufferedReader(new FileReader("C:/Script/Test.sql")));
// Read your sql file line by line
while((line = in.readLine()) != null){
// and add SQL statements in the batch.
stmt.addBatch(line);
}
// int[] to hold returned values
int[] count = stmt.executeBatch();
//commit statements
conn.commit();
executeBatch将返回一个Java doc:
中描述的整数数组executeBatch方法返回的数组中的元素可能是 以下之一:
大于或等于零的数字 - 表示命令已成功处理,并且是给出的更新计数 数据库中受命令影响的行数 执行
值SUCCESS_NO_INFO - 表示命令已成功处理但受影响的行数未知