更新数据

更新时间: 2019-04-26 10:41:17

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

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. public class PostgreSQLJDBC {
  6. public static void main(String args[]) {
  7. Connection conn = null;
  8. Statement stmt = null;
  9. try{
  10. Class.forName("org.postgresql.Driver");
  11. conn = DriverManager.getConnection("jdbc:postgresql://ts-uf6sx115kjk117156.tsdbgaia.rds.aliyuncs.com:3242/postgres","singo", "Tsdb2gaia");
  12. conn.setAutoCommit(false);
  13. stmt = conn.createStatement();
  14. String sql = "UPDATE tsdb_test set position = ST_SetSRID(ST_MakePoint(11.1,22.2),4326) WHERE uid=1002;";
  15. stmt.executeUpdate(sql);
  16. conn.commit();
  17. ResultSet rs = stmt.executeQuery( "SELECT uid, time, ST_AsText(position) AS pos FROM tsdb_test;" );
  18. while ( rs.next() ) {
  19. long id = rs.getLong("uid");
  20. String time = rs.getString("time");
  21. String position = rs.getString("pos");
  22. System.out.println( "uid = " + id + ", time = " + time + ", position = " + position );
  23. System.out.println();
  24. }
  25. rs.close();
  26. stmt.close();
  27. conn.commit();
  28. conn.close();
  29. } catch (Exception e) {
  30. System.err.println(e.getClass().getName() + " : " + e.getMessage());
  31. System.exit(0);
  32. }
  33. System.out.println("update successfully");
  34. }
  35. }
阿里云首页 相关技术圈