我正在編寫一個程序,它將接收一個學生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