嗨在Java中運行sql腳本的最佳方式是什麼?我有我的連接到數據庫,並拿起一些代碼,我認爲可能會運行該腳本目前得到一些錯誤。如何在java中運行sql腳本
public class DatabaseConnection {
private String userName = "";
private String password = "";
private String serverName = "";
private String portNumber = "";
public Connection getConnection() throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", this.userName);
connectionProps.put("password", this.password);
String connectionString = "jdbc:mysql://" +
this.serverName +
":" + this.portNumber;
System.out.println("Connection String: " + connectionString);
conn = DriverManager.getConnection(connectionString, connectionProps);
System.out.println("Connected to database");
return conn;
**ScriptRunner runner = new ScriptRunner(conn, connectionString, connectionProps)
String file = "resources/cleanDatabaseScript.sql";
System.out.println("retreived script");
runner.runScript(new BufferedReader(new FileReader(file)));**
}
ScriptRunner部分是im正在努力與當前的區域。
你得到了什麼錯誤? – Janar
目前這在scriptRunner行,但我確實改變了我複製的原始代碼以匹配我的。 錯誤:(47,88)java:';'預計 我添加了conn,connectionString,connectionProps – Peter
爲什麼在'ScriptRunner runner ...'行最後沒有';'? –