我想用
Java执行查询.
我创建了一个连接.然后我想执行一个INSERT语句,完成后,连接关闭,但我想通过连接执行一些insert语句,当循环完成然后关闭连接.
我能做什么 ?
我的示例代码是:
public NewClass() throws SQLException {
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
} catch (ClassNotFoundException e) {
System.out.println(“Where is your Oracle JDBC Driver?”);
return;
}
System.out.println(“Oracle JDBC Driver Registered!”);
Connection connection = null;
try {
connection = DriverManager.getConnection(
“jdbc:oracle:thin:@localhost:1521:orcl1”, “test”,
“oracle”);
} catch (SQLException e) {
System.out.println(“Connection Failed! Check output console”);
return;
}
if (connection != null) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(“SELECT * from test.special_columns”);
while (rs.next()) {
this.ColName = rs.getNString(“column_name”);
this.script = “insert into test.alldata (colname) ( select ” + ColName + ” from test.alldata2 ) ” ;
stmt.executeUpdate(“” + script);
}
}
else {
System.out.println(“Failed to make connection!”);
}
}
当执行select语句(“SELECT * from test.special_columns”)时,循环必须是两次,但是当执行并完成(stmt.executeUpdate(“”script)),然后关闭连接并从类返回.