可以使用MySQL的事务机制来同时更新两个表格,保证数据的一致性。以下是一个示例代码:
```java
public void updateData(int id, String name, String value) {
Connection conn = null;
PreparedStatement stmt1 = null;
PreparedStatement stmt2 = null;
try {
// 获取数据库连接
conn = dataSource.getConnection();
// 开启事务
conn.setAutoCommit(false);
// 更新t_config_list表格
stmt1 = conn.prepareStatement("UPDATE t_config_list SET name = ? WHERE id = ?");
stmt1.setString(1, name);
stmt1.setInt(2, id);
stmt1.executeUpdate();
// 更新t_header表格
stmt2 = conn.prepareStatement("UPDATE t_header SET value = ? WHERE config_id = ?");
stmt2.setString(1, value);
stmt2.setInt(2, id);
stmt2.executeUpdate();
// 提交事务
conn.commit();
} catch (SQLException e) {
// 回滚事务
if (conn != null) {
try {
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
} finally {
// 关闭资源
if (stmt1 != null) {
try {
stmt1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt2 != null) {
try {
stmt2.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
上述代码中,首先获取数据库连接并开启事务,然后分别更新两个表格,并在第二个更新语句中使用`config_id = ?`作为条件来更新t_header表格。最后提交事务或回滚事务,并关闭资源。