全部产品

更新数据

更新时间:2019-12-30 10:22:55

更新uid为1002的记录的位置信息:

  1. public class UpdateJDBC {
  2. public static void main(String args[]) {
  3. Connection conn = null;
  4. Statement stmt = null;
  5. try{
  6. Class.forName("org.postgresql.Driver");
  7. conn = DriverManager.getConnection("jdbc:postgresql://139.224.168.86:5432/doc","citybrain", "Tsdb2gaia");
  8. conn.setAutoCommit(false);
  9. stmt = conn.createStatement();
  10. String sql = "UPDATE tsdb_test set position = ST_MakePoint(12.1,23.2) WHERE uid=1002;";
  11. stmt.executeUpdate(sql);
  12. conn.commit();
  13. // 数据库默认是1s刷新
  14. Thread.sleep(2000);
  15. ResultSet rs = stmt.executeQuery( "SELECT uid, time, ST_AsText(position) AS pos FROM tsdb_test;" );
  16. while ( rs.next() ) {
  17. long id = rs.getLong("uid");
  18. String time = rs.getString("time");
  19. String position = rs.getString("pos");
  20. System.out.println( "uid = " + id + ", time = " + time + ", position = " + position );
  21. System.out.println();
  22. }
  23. rs.close();
  24. stmt.close();
  25. conn.commit();
  26. conn.close();
  27. } catch (Exception e) {
  28. System.err.println(e.getClass().getName() + " : " + e.getMessage());
  29. System.exit(0);
  30. }
  31. System.out.println("update successfully");
  32. }
  33. }