我的下面代码片段将连接到oracle数据库但不执行sql文件(包含db上的查询列表)
我有一个主类,它接受来自用户的与数据库相关的输入,并通过传递值来调用下面的类。 下面的类将连接到db,然后执行由变量SQLPATH提供的sql文件。
我能够连接到db,但它没有在db上执行sql文件。 任何人都可以告诉我哪里出错了。不会抛出任何错误。
下面的是片段
public class SQLRunner {
private static String driverName;
//private static String con;
private static Connection con;
private static String url;
private static String script_location = "";
private static String file_extension = ".sql";
private static ProcessBuilder processBuilder =null;
public static void runSQLFile(String DBType, String userName, String password, String dbsid, String host, String port, String SQLFilePath) {
System.out.println("invoked");
if(DBType.equals("ORACLE")){
url = "jdbc:oracle:thin:@"+host+":"+port+":"+dbsid;
System.out.println(url);
driverName = "oracle.jdbc.OracleDriver";
/*}else if(DBType.equals("DB2")){
url = "jdbc:db2://"+host+":"+port+"/"+dbsid;
driverName = "com.ibm.db2.jcc.DB2Driver" ;
}else if(DBType.equals("MSSQL")){
url = "jdbc:sqlserver://"+host+":"+port+";databaseName="+dbsid;
driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}else{
System.out.println("Database not supported@!");
}*/
try {
Class.forName(driverName);
try {
//System.out.println(url+"\n"+userName+"\n"+password);
con = DriverManager.getConnection(url, userName, password);
System.out.println("DB connected");
} catch (SQLException ex) {
// log an exception. fro example:
System.out.println("Failed to create the database connection.");
}
} catch (ClassNotFoundException ex) {
// log an exception. for example:
System.out.println("Driver not found.");
}
}
try {
script_location = "@" + SQLFilePath; //ORACLE
System.out.println(script_location);
processBuilder = new ProcessBuilder("sqlplus", userName+"/"+password+"@"+dbsid, script_location);
System.out.println("running sql");
//ORACLE
//script_location = "-i" + list_files[i].getAbsolutePath();
// processBuilder = new ProcessBuilder("sqlplus", "-Udeep-Pdumbhead-Spc-de-deep\\sqlexpress-de_com",script_location);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String currentLine = null;
while ((currentLine = in.readLine()) != null) {
System.out.println(" " + currentLine);
}
} catch (IOException e) {
}
}
}