Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.postgresql.ds.PGSimpleDataSource;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Objects;
- public class Main {
- public static void main(String[] args) throws SQLException {
- PGSimpleDataSource ds = new PGSimpleDataSource();
- ds.setServerName("db.dai.fmph.uniba.sk");
- ds.setPortNumber(5432);
- ds.setDatabaseName("playground");
- ds.setUser("horvath186@uniba.sk");
- ds.setPassword("dbPass1234");
- try (Connection c = ds.getConnection()){
- showEmployees(c, 5, 2);
- System.out.println(compare(c,
- "SELECT employee_id FROM employees WHERE salary < 2000",
- "SELECT employee_id FROM employees WHERE salary > 2000"));
- }
- }
- static void showEmployees(Connection c, int pageSize, int pageIndex) throws SQLException {
- try(Statement s = c.createStatement()) {
- //s.executeQuery("SELECT import_company()");
- String sql = "SELECT employee_id, first_name, last_name FROM employees LIMIT "
- + pageSize + " OFFSET " + pageIndex * pageSize;
- try(ResultSet r = s.executeQuery(sql)) {
- while(r.next()) {
- int employeeId = r.getInt(1);
- String firstName = r.getString(2);
- String lastName = r.getString(3);
- System.out.println(employeeId + " " + firstName + " " + lastName);
- }
- }
- }
- }
- public static boolean compare(Connection c, String sql1, String sql2) throws SQLException {
- try (Statement s1 = c.createStatement()){
- try (Statement s2 = c.createStatement()) {
- try (ResultSet r1 = s1.executeQuery(sql1)) {
- try (ResultSet r2 = s2.executeQuery(sql2)) {
- while(r1.next() && r2.next()) {
- String c1 = r1.getString(1);
- String c2 = r2.getString(1);
- boolean same = Objects.equals(c1, c2);
- if (same == false) {
- return false;
- }
- }
- }
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement