我需要从复制的数据库中打印四列,并且我只需要使用JDBC。打印所有格式整齐的列的工作是我认为我可以处理的。它创建表的副本,然后插入给我带来困难的所需行。是否可以编写executeQuery()字符串来实现这一目标?我知道要插入我需要使用executeUpdate()的行。我将不胜感激任何帮助或指导。感谢。
public static void main(String args[]) throws SQLException {
// define common JDBC objects
Connection connection = null;
Statement create = null;
Statement insert = null;
ResultSet rs = null;
// Connect to the database
String dbUrl =
"jdbc:oracle:xxxxxxxxxxxxxxxxxxxxxxxxxx";
String username = args[0];
String password = args[1];
Collection<String> result = new ArrayList<String>();
connection = DriverManager.getConnection(
dbUrl, username, password);
create = connection.createStatement();
String query =
"CREATE TABLE newTable AS "
+ "SELECT * "
+ "FROM oldTable; ";
rs = create.executeQuery(query);
insert = connection.createStatement();
int rows = insert.executeUpdate("INSERT INTO newTable SELECT * FROM oldTable ");
if (rows == 0) {
System.out.println("No rows");
} else {
System.out.println(rows + " row(s)affected.");
connection.close();
}
}
}
//and here is the combined sql statement with CREATE and INSERT:
statement = connection.createStatement();
sql =
"CREATE TABLE newTable AS SELECT * FROM oldTable "
+ "INSERT INTO newTable "
+ "(" + " col01,col02,col03,col04, "
+ "col05,col06,col07,col08,col09, "
+ "col10,col11,col12 " + ") "
+ "VALUES " + "(" + "'data01','data02','data03','data04', "
+ "data05,data06,data07,'data08',data09,data10,data11,data12 " +")" ;
statement.executeUpdate(sql);