import java.io.*;
import java.sql.*;
public class ExecuteDDL {
Connection conn;
public ExecuteDDL() {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
}
catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args)
throws Exception, IOException {
if (args.length < 1) {
System.err.println("Usage: java ExecuteDDL <dml file>");
System.exit(1);
}
new ExecuteDDL().process(args[0]);
}
public void process(String fileName) throws IOException, SQLException {
boolean rslt = false;
BufferedReader in = new BufferedReader(new FileReader(fileName));
Statement stmt = null;
StringBuffer sql = new StringBuffer(1024);
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
if (line.length() == 1 && line.indexOf("/") > -1) {
try {
stmt = conn.createStatement();
rslt = stmt.execute(sql.toString());
System.out.println("OK");
System.out.println(" ");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (stmt != null)
try { stmt.close(); } catch (SQLException ignore) { }
}
sql = new StringBuffer(1024);
}
else {
sql.append(line);
sql.append(" ");
}
}
System.out.println(sql);
in.close();
}
protected void finalize()
throws Throwable {
if (conn != null)
try { conn.close(); } catch (SQLException ignore) { }
super.finalize();
}
} |