全部产品

删除数据

更新时间:2019-04-26 14:19:45

删除某个uid为1001的所有数据。

  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 = "DELETE FROM tsdb_test WHERE uid=1001;";
  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("delete successfully");
  34. }
  35. }