Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. import org.postgresql.ds.PGSimpleDataSource;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Objects;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) throws SQLException {
  12.         PGSimpleDataSource ds = new PGSimpleDataSource();
  13.         ds.setServerName("db.dai.fmph.uniba.sk");
  14.         ds.setPortNumber(5432);
  15.         ds.setDatabaseName("playground");
  16.         ds.setUser("horvath186@uniba.sk");
  17.         ds.setPassword("dbPass1234");
  18.  
  19.         try (Connection c = ds.getConnection()){
  20.             showEmployees(c, 5, 2);
  21.             System.out.println(compare(c,
  22.                     "SELECT employee_id FROM employees WHERE salary < 2000",
  23.                     "SELECT employee_id FROM employees WHERE salary > 2000"));
  24.         }
  25.     }
  26.  
  27.     static void showEmployees(Connection c, int pageSize, int pageIndex) throws SQLException {
  28.         try(Statement s = c.createStatement()) {
  29.             //s.executeQuery("SELECT import_company()");
  30.  
  31.             String sql = "SELECT employee_id, first_name, last_name FROM employees LIMIT "
  32.                     + pageSize + " OFFSET " + pageIndex * pageSize;
  33.  
  34.  
  35.             try(ResultSet r = s.executeQuery(sql)) {
  36.                 while(r.next()) {
  37.                     int employeeId = r.getInt(1);
  38.                     String firstName = r.getString(2);
  39.                     String lastName = r.getString(3);
  40.  
  41.                     System.out.println(employeeId + " " + firstName + " " + lastName);
  42.                 }
  43.             }
  44.         }
  45.     }
  46.     public static boolean compare(Connection c, String sql1, String sql2) throws SQLException {
  47.         try (Statement s1 = c.createStatement()){
  48.             try (Statement s2 = c.createStatement()) {
  49.                 try (ResultSet r1 = s1.executeQuery(sql1)) {
  50.                     try (ResultSet r2 = s2.executeQuery(sql2)) {
  51.                         while(r1.next() && r2.next()) {
  52.                             String c1 = r1.getString(1);
  53.                             String c2 = r2.getString(1);
  54.  
  55.                             boolean same = Objects.equals(c1, c2);
  56.  
  57.                             if (same == false) {
  58.                                 return false;
  59.                             }
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.         return true;
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement