[英]Reading row from one table and copy it to another table of database in Java
我的Java数据库中有2个表。 我想从table1加载行并将它们放在table2中。 怎么可以呢? 我正在使用ResultSet res = stmt.executeQuery(SQL)
从表I中读取。我可以使用什么来将此数据放到表II中。 ?
这个给你:
String sql = "insert into table1 select * from table2 [where conditions]";
Statement stmt = connection.prepareStatement(sql);
stmt.executeUpdate();
您可以尝试这样的事情:
Statement st1 = con1.createStatement();
ResultSet rs = st1.executeQuery("select * from table1");
PreparedStatement ps = null;
while(rs.next())
{
ps = con2.prepareStatement("insert into table2 values(?,?)");
ps.setInt(rs.getInt());
ps.setString(rs.getString());
ps.executeUpdate();
}
假设您的表中有两列
您可以使用PreparedStatments
示例代码在这里
int result;
Connection connection = null; // manages connection
PreparedStatement insert = null;
connection = DriverManager.getConnection(DB_URL,"root","password");
insert = connection.prepareStatement("INSERT into table2" +
"(f1,f2,f3)" +
"VALUES(?,?,?)" );
insert.setString(1, Yourvaluehere);
insert.setInt(2, Yourvaluehere);
insert.setDouble(3, YourValueHere);
result = insert.executeUpdate();
// Yourvaluehere - 应该来自对象的getmethods - 这就是我所做的,如果你记住它,你可以做得更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.