全部产品
云市场

删除数据

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

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

  1. public class DelJDBC {
  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 = "DELETE FROM tsdb_test WHERE uid=1001;";
  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("delete successfully");
  32. }
  33. }