我有一个使用 MySql 和 Java JDBC 的程序。
我的问题是:
我有一个表 (TEMP
),其中 ID
仅包含 1 列,另一个表包含用户个人详细信息,如 ID、姓名、年龄等。
我正在尝试从 TEMP
表中检索 ID
并更新 USER
表中未填写的信息,例如姓名、年龄、地址等。
这是我写的查询:
update m_auth_info
set name = '"+name+"',
addr = '"+addr+"',
email = '"+email+"',
affiliation = '"+affil+"',
status = '"+1+"'
where a_id = '"+ResultSet+"'";
当我的getIdMethod
从TEMP
表中检索ID到ResultSet
时。我无法更新USER
TABLE。
但是如果我直接提供 ID 就可以了......例如。
update m_auth_info
set name = '"+name+"',
addr = '"+addr+"',
email = '"+email+"',
affiliation = '"+affil+"',
status = '"+1+"'
where a_id = '"+8989+"'";
请告诉我是否必须在 getIdMethod
中写入哪个步骤,以便将值获取到 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+"'";*/
}
关于Java、MySQL 使用 ID(主键)更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205689/