我正在编写一个程序,它将接收一个学生ID并验证该ID是否存在于一个mysql表中。如果它确实存在,我想将它存在的整个行并将该行复制到另一个表中。目前,该程序只是将表中的所有行复制到另一个表中。任何帮助赞赏。我在下面插入了一段代码。从一个mysql表插入信息到另一个
try {
String compareText = IDField.getText().trim();
if(compareText.length() > 0){
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/simlab","root","password");
System.out.println("Connected to database");
Statement stmt1 = conn.createStatement();
ResultSet rs1 = stmt1.executeQuery("select * from students where LUID='"+IDField.getText()+"' ");
boolean isPresent = rs1.next();
if (isPresent)
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/simlab","root","password");
System.out.println("Connected to database");
int rows = stmt1.executeUpdate("INSERT INTO skills(ID_Student,LUID_Student)SELECT ID, LUID FROM students");
if (rows == 0)
{
System.out.println("Don't add any row!");
}
else
{
System.out.println(rows + " row(s)affected.");
conn.close();
}
//System.out.println("Already exists!!");
}
一举两得表驻留在同一个数据库,或者是他们在不同的数据库,如标题所暗示? – Geier 2013-03-23 18:58:04
这两个表都在同一个数据库中。感谢您指出了这一点。 – user2188777 2013-03-23 18:59:03