为什么updateSql不能在while循环中执行?
Why can‘t updateSql be excuted in while-loop?
当我有 select 时,这有效:
public static void main(String[] args){
try{
Connection conn = DriverManager.getConnection("jdbc:oracle:XXXXX", "username", "psw");
Statement stm = conn.createStatement();
String selectSql="select h.id, h.user_id,h.start_time,h.end_time,h.start_date,h.end_date, a.time_zone from hos_driving_log h left join user_info ua on h.user_id= ua.user_id left JOIN account a on a.acct_id = ua.acct_id";
ResultSet ress = stm.executeQuery(selectSql);
while(ress.next()){
int id = ress.getInt("id");
String user_id = ress.getString("user_id");
long start_time = ress.getLong("start_time");
long end_time = ress.getLong("end_time");
int start_date = ress.getInt("start_date");
int end_date = ress.getInt("end_date");
String time_zone = ress.getString("time_zone");
System.out.println(id+" "+user_id+" "+start_time+" "+end_time+" "+start_date+" "+end_date+" "+time_zone);
}
}catch(SQLException e){
e.printStackTrace();
}
//Close the resources - Done automatically by try-with-resources
}
输出:
1 testuser 20151025021324 20151026024135 0 0 GMT-12:00
2 hostest 20151026024135 20151022080352 0 0 GMT-07:00
24 stress0101 20151208075641 20151208075717 0 0 null
88 123 c 20151224085208 20151224130803 0 0 null
89 123 c 20151224130916 20151224130922 0 0 null
90 123 c 20151224130917 20151224130923 0 0 null
91 123 c 20151224130918 20151224130924 0 0 null
92 123 c 20151224130919 20151224130925 0 0 null
但是当我想在循环中更新时:
try{
Connection conn = DriverManager.getConnection("jdbc:oracle:XXXXX", "username", "psw");
Statement stm = conn.createStatement();
String selectSql="select h.id, h.user_id,h.start_time,h.end_time,h.start_date,h.end_date, a.time_zone from hos_driving_log h left join user_info ua on h.user_id= ua.user_id left JOIN account a on a.acct_id = ua.acct_id";
ResultSet ress = stm.executeQuery(selectSql);
while(ress.next()){
int id = ress.getInt("id");
String user_id = ress.getString("user_id");
long start_time = ress.getLong("start_time");
long end_time = ress.getLong("end_time");
int start_date = ress.getInt("start_date");
int end_date = ress.getInt("end_date");
String time_zone = ress.getString("time_zone");
System.out.println(id+" "+user_id+" "+start_time+" "+end_time+" "+start_date+" "+end_date+" "+time_zone);
start_date = getDrivingLogShortTypeDate(start_time, time_zone);
end_date = getDrivingLogShortTypeDate(end_time, time_zone);
String updateSql = "update hos_driving_log set start_date = "+ start_date+",end_date = "+end_date+" where id="+id;
int i = stm.executeUpdate(updateSql);
System.out.println(updateSql);
}
}catch(SQLException e){
e.printStackTrace();
}
//Close the resources - Done automatically by try-with-resources
...它只进行一次更新:
45 co2-stress0096 20151222090816 20151222091436 20151222 20151222 GMT+08:00
update hos_driving_log set start_date = 20151222,end_date = 20151222 where id=45
为什么一更新就停了?我怎样才能更新所有的行?
因为当生成它的 Statement 对象关闭、重新执行或用于从多个结果序列中检索下一个结果时,ResultSet 对象会自动关闭。
您正在重新执行生成结果集的语句对象 stm
。您需要使用单独的语句对象进行更新。
此外,不要通过将文字连接到 SQL 来构建 SQL 更新语句。使用绑定变量,您的用户和 DBA 都会感谢您。
见https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
当我有 select 时,这有效:
public static void main(String[] args){
try{
Connection conn = DriverManager.getConnection("jdbc:oracle:XXXXX", "username", "psw");
Statement stm = conn.createStatement();
String selectSql="select h.id, h.user_id,h.start_time,h.end_time,h.start_date,h.end_date, a.time_zone from hos_driving_log h left join user_info ua on h.user_id= ua.user_id left JOIN account a on a.acct_id = ua.acct_id";
ResultSet ress = stm.executeQuery(selectSql);
while(ress.next()){
int id = ress.getInt("id");
String user_id = ress.getString("user_id");
long start_time = ress.getLong("start_time");
long end_time = ress.getLong("end_time");
int start_date = ress.getInt("start_date");
int end_date = ress.getInt("end_date");
String time_zone = ress.getString("time_zone");
System.out.println(id+" "+user_id+" "+start_time+" "+end_time+" "+start_date+" "+end_date+" "+time_zone);
}
}catch(SQLException e){
e.printStackTrace();
}
//Close the resources - Done automatically by try-with-resources
}
输出:
1 testuser 20151025021324 20151026024135 0 0 GMT-12:00
2 hostest 20151026024135 20151022080352 0 0 GMT-07:00
24 stress0101 20151208075641 20151208075717 0 0 null
88 123 c 20151224085208 20151224130803 0 0 null
89 123 c 20151224130916 20151224130922 0 0 null
90 123 c 20151224130917 20151224130923 0 0 null
91 123 c 20151224130918 20151224130924 0 0 null
92 123 c 20151224130919 20151224130925 0 0 null
但是当我想在循环中更新时:
try{
Connection conn = DriverManager.getConnection("jdbc:oracle:XXXXX", "username", "psw");
Statement stm = conn.createStatement();
String selectSql="select h.id, h.user_id,h.start_time,h.end_time,h.start_date,h.end_date, a.time_zone from hos_driving_log h left join user_info ua on h.user_id= ua.user_id left JOIN account a on a.acct_id = ua.acct_id";
ResultSet ress = stm.executeQuery(selectSql);
while(ress.next()){
int id = ress.getInt("id");
String user_id = ress.getString("user_id");
long start_time = ress.getLong("start_time");
long end_time = ress.getLong("end_time");
int start_date = ress.getInt("start_date");
int end_date = ress.getInt("end_date");
String time_zone = ress.getString("time_zone");
System.out.println(id+" "+user_id+" "+start_time+" "+end_time+" "+start_date+" "+end_date+" "+time_zone);
start_date = getDrivingLogShortTypeDate(start_time, time_zone);
end_date = getDrivingLogShortTypeDate(end_time, time_zone);
String updateSql = "update hos_driving_log set start_date = "+ start_date+",end_date = "+end_date+" where id="+id;
int i = stm.executeUpdate(updateSql);
System.out.println(updateSql);
}
}catch(SQLException e){
e.printStackTrace();
}
//Close the resources - Done automatically by try-with-resources
...它只进行一次更新:
45 co2-stress0096 20151222090816 20151222091436 20151222 20151222 GMT+08:00
update hos_driving_log set start_date = 20151222,end_date = 20151222 where id=45
为什么一更新就停了?我怎样才能更新所有的行?
因为当生成它的 Statement 对象关闭、重新执行或用于从多个结果序列中检索下一个结果时,ResultSet 对象会自动关闭。
您正在重新执行生成结果集的语句对象 stm
。您需要使用单独的语句对象进行更新。
此外,不要通过将文字连接到 SQL 来构建 SQL 更新语句。使用绑定变量,您的用户和 DBA 都会感谢您。
见https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html