简体   繁体   中英

Java, MySQL updating table using ID (primary key)

I have a program in which working with MySql and Java JDBC.

My question is:

I have a table ( TEMP ) with ID as only 1 column and another table with user personal details like ID, name, age etc..

Im trying to retrieve the ID from TEMP table and update unfilled information like name, age, address etc in USER table.

This is the query I wrote:

update m_auth_info 
set name  = '"+name+"', 
    addr  = '"+addr+"', 
    email = '"+email+"', 
    affiliation = '"+affil+"', 
    status = '"+1+"'  
where a_id = '"+ResultSet+"'";

and when my getIdMethod retrieves ID into ResultSet from TEMP table. I couldn't able to update USER TABLE.

But it works if I give ID directly.. example.

update m_auth_info 
set name = '"+name+"', 
    addr = '"+addr+"', 
    email = '"+email+"', 
    affiliation = '"+affil+"', 
    status = '"+1+"'  
where a_id = '"+8989+"'";

Please tell me if I have to write which step to write in my getIdMethod , so that ill get the value into ResultSet .

int id=0;
String sql = "Select ID from Temp";
ResultSet rs = statement.executeQuery(sql);

while(rs.next(){
   id = rs.getInt(1);
  /*   pass your update query here and use like this it should work for you
    update m_auth_info  set name  = '"+name+"',
    addr  = '"+addr+"',  email = '"+email+"', 
    affiliation = '"+affil+"', status = '"+1+"'  where a_id = '"+id+"'";*/
}

I believe what you are trying to do is to get the result from ResultSet , which you can do with something rs.getLong(0) , your ResultSet variable . getYourIdType ( 0 ) or columnName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM