2013-05-13 95 views
-2

SQL語句層返回多個值,我想用所有這些值更新數據庫中的列這裏有什麼問題?從結果集更新數據庫

 String cust_code = session.getAttribute("Cust_Code").toString(); 
     String video_typ = session.getAttribute("video_typ").toString(); 
     int personal_p = Integer.parseInt(session.getAttribute("personal_p").toString()); 
     int layers_p = Integer.parseInt(session.getAttribute("layers_p").toString()); 
     String parent_cod = session.getAttribute("parent_code").toString(); 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ontube", "root", "123456"); 
     Statement st = con.createStatement(); 
     String personal_po = "update customers set personal_p =personal_p + '"+personal_p+"' where membership_num='"+cust_code+"' "; 
     String first_layer="update customers set layers_p = layers_p+'"+layers_p+"' where membership_num='"+parent_cod+"'"; 
     int pers_points = st.executeUpdate(personal_po); 
     int first_lyr = st.executeUpdate(first_layer); 
     String layers="select membership_num from customers where parent_num='"+parent_cod+"'" ; 
     // i want to update all values comes from these resultset 
     ResultSet rs = st.executeQuery(layers); 

     while(rs.next()){ 
     String names =rs.getString(1); 

     String update_layers = "update customers set layers_p = layers_p + '"+layers_p+"' where membership_num='"+names+"' "; 

     int nms = st.executeUpdate(update_layers); 
+0

什麼是錯誤? – CloudyMarble 2013-05-13 07:57:11

回答

0

您應該更改這些行:

String personal_po = "update customers set personal_p =personal_p + '"+personal_p+"' where membership_num='"+cust_code+"' "; 
String first_layer="update customers set layers_p = layers_p+'"+layers_p+"' where membership_num='"+parent_cod+"'"; 
String update_layers = "update customers set layers_p = layers_p + '"+layers_p+"' where membership_num='"+names+"' "; 

更換:
...set xxxxx_p = xxxxx_p + '" + xxxxx_p + "'...
...set xxxxx_p = '" + xxxxx_p + "'...

UPDATE
另外,不要使用更新相同Statement,同時還在讀書ResultSet

... 
Statement st2 = con.createStatement(); 
while(rs.next()){ 
    ... 
    int nms = st2.executeUpdate(update_layers); 
} 
... 
+0

我cahnged他們,沒有任何改變 – user2325383 2013-05-13 08:04:54

+0

sql statment是正確的我認爲結果集更新中的錯誤,但我不知道我能做什麼 – user2325383 2013-05-13 08:06:31

+0

@ user2325383:看到我的更新的答案。 – gkalpak 2013-05-13 08:09:59

1
String update_layers = "update customers set layers_p = layers_p + '" + layers_p + "' where membership_num='" + names + "' "; 

應該是:

String update_layers = "update customers set layers_p = '" + layers_p + "' where membership_num='" + names + "' "; 
+0

它給了我這個錯誤ResultSet關閉後不允許操作 – user2325383 2013-05-13 07:58:32

+1

與personal_po語句相同。 – CloudyMarble 2013-05-13 08:02:00

+0

什麼都沒有改變相同的錯誤錯誤不是在sql sytanx – user2325383 2013-05-13 08:03:51