i want to execute the SQL Script below, therefore i used ibatis Script Runner
Connection "con = DriverManager.getConnection("jdbc:odbc:Database")"
ScriptRunner sr = new ScriptRunner(con, true, false);
// Give the input file to Reader
Reader reader = new BufferedReader(new FileReader(aSQLScriptFilePath));
// Execute script
sr.runScript(reader);
But there is the Problem, that While loops will not be completely executed. I pointed out that the problem comes because the Script Runner didn´t wait until he while loop is completely done. So there are only 386 and not 1000 rows created. I got the same Problem using the Java Statement execute method( http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html ):
stmnt.execute("DECLARE @variable int = 1 WHILE (@variable<=1000) BEGIN INSERT INTO BatchTest2 SELECT @variable SET @variable=@variable+1 END" );
But if i set
Thread.sleep(5000);
behind stmnt.execute the while loop is completeley done and i get 1000 rows like i wanted. So my question is, if it is possible to execute the complete While loop whitout using Thread.sleep.?
Thank you in advance!
IF OBJECT_ID('test') IS NOT NULL
DROP TABLE test;
CREATE TABLE test(
spalte1 int PRIMARY KEY NOT NULL);
DECLARE @variable int = 1
WHILE (@variable<=1000)
BEGIN
INSERT INTO test
SELECT @variable
SET @variable=@variable+1
END
You can refer to below example from MKYONG for same. Hope this helps you out.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.ibatis.common.jdbc.ScriptRunner;
/**
*
@author Dhinakaran Pragasam
*/
public class RunSqlScript {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
String aSQLScriptFilePath = "path/to/sql/script.sql";
// Create MySql Connection
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/database", "username", "password");
Statement stmt = null;
try {
// Initialize object for ScripRunner
ScriptRunner sr = new ScriptRunner(con, false, false);
// Give the input file to Reader
Reader reader = new BufferedReader(
new FileReader(aSQLScriptFilePath));
// Exctute script
sr.runScript(reader);
} catch (Exception e) {
System.err.println("Failed to Execute" + aSQLScriptFilePath
+ " The error is " + e.getMessage());
}
}
}
Without having ScriptRunner
at hand, a possible solution might be to use a procedure and then calling the procedure in the script.
Create Procedure insertData()
As
Begin
DECLARE @variable int = 1
WHILE (@variable<=1000)
BEGIN
INSERT INTO test
SELECT @variable
SET @variable=@variable+1
END
End
IF OBJECT_ID('test') IS NOT NULL
DROP TABLE test;
CREATE TABLE test(
spalte1 int PRIMARY KEY NOT NULL);
EXEC insertData;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.