diff --git a/src/ru/milandr/courses/polishchuk/Application.java b/src/ru/milandr/courses/polishchuk/Application.java new file mode 100644 index 0000000..0ebdb0a --- /dev/null +++ b/src/ru/milandr/courses/polishchuk/Application.java @@ -0,0 +1,263 @@ +package ru.milandr.courses.polishchuk; + +import com.sun.org.apache.bcel.internal.generic.ARETURN; + +import javax.jws.soap.SOAPBinding; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.*; +import java.util.ArrayList; +import java.util.Comparator; + +class SQLDemo { + private Connection connection; + + private class User { + BigDecimal id; + String first_name; + String last_name; + String phone_number; + + User(BigDecimal id, String first_name, String last_name, String phone_number) { + this.id = id; + this.first_name = first_name; + this.last_name = last_name; + this.phone_number = phone_number; + } + + User(User user) { + this.id = user.id; + this.last_name = user.last_name; + this.first_name = user.first_name; + this.phone_number = user.phone_number; + } + + void printUser() { + System.out.print("user id: " + this.id + '\n' + + "first_name: " + this.first_name + '\n' + + "last_name: " + this.last_name + '\n' + + "phone_number: " + this.phone_number + '\n'); + } + } + + private class Address { + BigDecimal id; + String address; + String city; + String postal_code; + + Address(BigDecimal id, String address, String city, String postal_code) { + this.id = id; + this.address = address; + this.city = city; + this.postal_code = postal_code; + } + + void printAddress() { + System.out.print("address id: " + this.id + '\n' + + "address: " + this.address + '\n' + + "city: " + this.city + '\n' + + "postal_code: " + this.postal_code + '\n'); + } + } + + private class AddressedUser extends User { + Address address; + + AddressedUser(User user, Address address) { + super(user); + this.address = address; + } + + AddressedUser(BigDecimal id, String first_name, String last_name, String phone_number, Address address) { + super(id, first_name, last_name, phone_number); + this.address = address; + } + + @Override + void printUser() { + super.printUser(); + this.address.printAddress(); + } + } + + public SQLDemo(Connection connection) { + this.connection = connection; + } + + public Connection getConnection() { + return connection; + } + + private void printUsers(ArrayList users) { + for (User user : users) { + user.printUser(); + } + } + + private User initUserFromResultSet(ResultSet user) throws SQLException { + return new User( + user.getBigDecimal("id"), + user.getString("first_name"), + user.getString("last_name"), + user.getString("phone_number") + ); + } + + private Address initAddressFromResultSet(ResultSet address) throws SQLException { + return new Address( + address.getBigDecimal("address_id"), + address.getString("address"), + address.getString("city"), + address.getString("postal_code") + ); + } + + private AddressedUser initAddressedUserFromResultSet(ResultSet user) throws SQLException { + return new AddressedUser( + initUserFromResultSet(user), + initAddressFromResultSet(user) + ); + } + + private void printUserWithIdJava(BigDecimal id) { + try (PreparedStatement stmtSelectUsersIds = connection.prepareStatement( + "select users.*, addresses.id as addr_id, address, city, postal_code " + + "from users " + + "join addresses on users.address_id = addresses.id " + + "where users.id = ?" + )) { + stmtSelectUsersIds.setBigDecimal(1, id); + + ResultSet user = stmtSelectUsersIds.executeQuery(); + if (!user.next()) + throw new SQLException("Zero users found!"); + if (!user.isLast()) + throw new SQLException("Duplicate user id found!"); + AddressedUser userWithId = initAddressedUserFromResultSet(user); + userWithId.printUser(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void printUserWithMaxIdJava() { + try (PreparedStatement stmtSelectUsersIds = connection.prepareStatement("select id from users")) { + BigDecimal maxId = new BigDecimal(-1); + ResultSet userIds = stmtSelectUsersIds.executeQuery(); + + while (userIds.next()) { + BigDecimal id = userIds.getBigDecimal("id"); + if (maxId.compareTo(id) < 0) { + maxId = new BigDecimal(String.valueOf(id)); + } + } + printUserWithIdJava(maxId); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void printUsersFromAddress(String address) { + try (PreparedStatement stmtSelectUsersFromAddress = connection.prepareStatement( + "select users.*, a.id as address_id, address, city, postal_code " + + "from users join addresses a on users.address_id = a.id " + + "where address = ?" + )) { + stmtSelectUsersFromAddress.setString(1, address); + ResultSet resultSet = stmtSelectUsersFromAddress.executeQuery(); + + while (resultSet.next()) { + AddressedUser addressedUser = initAddressedUserFromResultSet(resultSet); + addressedUser.printUser(); + System.out.println(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void printUsersSortedByLastName() { + try (PreparedStatement stmtSelectUsers = connection.prepareStatement("select * from users")) { + ResultSet resultSet = stmtSelectUsers.executeQuery(); + + ArrayList usersArrayList = new ArrayList<>(); + while (resultSet.next()) { + User user = initUserFromResultSet(resultSet); + usersArrayList.add(user); + } + usersArrayList.sort((o1, o2) -> o1.last_name.compareTo(o2.last_name)); + usersArrayList.forEach(u -> System.out.println(u.last_name + ' ' + u.first_name)); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public double calcAvgPostalCode() { + double sum = 0; + int cnt = 0; + + try (PreparedStatement stmtSelectPostalCodes = connection.prepareStatement( + "select postal_code from addresses" + )) { + ResultSet resultSet = stmtSelectPostalCodes.executeQuery(); + + while (resultSet.next()) { + String code = resultSet.getString("postal_code"); + if (code.matches("^[0-9]+$")) { + sum += Integer.parseInt(code); + cnt++; + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + if (cnt == 0) + return 0.; + return sum / cnt; + } + + public void findUnusedAddr() { + try (PreparedStatement stmt = connection.prepareStatement( + "select address from addresses left join users on users.address_id = addresses.id where users.id is null" + )) { + ResultSet resultSet = stmt.executeQuery(); + if (resultSet.next()){ + System.out.println(resultSet.getString(1)); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} + +public class Application { + public static void main(String[] args) throws ClassNotFoundException { + try { + Class.forName("org.postgresql.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return; + } + try (Connection dbConn = DriverManager.getConnection( + "jdbc:postgresql://localhost:5432/users_database", + "postgres", "postgres")) { + SQLDemo demo = new SQLDemo(dbConn); + + demo.printUserWithMaxIdJava(); + System.out.println(); + + demo.printUsersFromAddress("P.O. Box 677, 8665 Ante Road"); + System.out.println(); + + demo.printUsersSortedByLastName(); + System.out.println(); + + System.out.println(demo.calcAvgPostalCode()); + + demo.findUnusedAddr(); + } catch (SQLException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/ru/milandr/courses/polishchuk/TablesEqualityMain.java b/src/ru/milandr/courses/polishchuk/TablesEqualityMain.java new file mode 100644 index 0000000..9eac5a6 --- /dev/null +++ b/src/ru/milandr/courses/polishchuk/TablesEqualityMain.java @@ -0,0 +1,38 @@ +package ru.milandr.courses.polishchuk; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class TablesEqualityMain { + public static void main(String[] args) { + try { + Class.forName("org.postgresql.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + try ( + Connection conn1 = DriverManager.getConnection( + "jdbc:postgresql://localhost:5432/users_database", + "postgres", "postgres" + ); + Connection conn2 = DriverManager.getConnection( + "jdbc:postgresql://localhost:5432/users_database", + "postgres", "postgres" + ) + ) { + TablesEqualityTester tester = new TablesEqualityTester( + conn1, conn2, + "users4test", "users4test1", + "id,first_name,last_name" + ); + if (tester.equalityTest()) { + System.out.println("Equals"); + } else { + System.out.println("Not equal"); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/ru/milandr/courses/polishchuk/TablesEqualityTester.java b/src/ru/milandr/courses/polishchuk/TablesEqualityTester.java new file mode 100644 index 0000000..319aaa0 --- /dev/null +++ b/src/ru/milandr/courses/polishchuk/TablesEqualityTester.java @@ -0,0 +1,96 @@ +package ru.milandr.courses.polishchuk; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; +import java.util.stream.IntStream; + +public class TablesEqualityTester { + private Connection db1Connection; + private Connection db2Connection; + private String table1Name; + private String table2Name; + private String columnNames; + private int columnsNum; + + public TablesEqualityTester(Connection db1Connection, Connection db2Connection, + String table1Name, String table2Name, + String columnNames) { + this.db1Connection = db1Connection; + this.db2Connection = db2Connection; + this.table1Name = table1Name; + this.table2Name = table2Name; + this.columnNames = columnNames; + this.columnsNum = columnNames.split(",").length; + } + + private ArrayList> initArrayFromResultSet(ResultSet resultSet, int size) throws SQLException { + ArrayList> dbEntries = new ArrayList>(); + + IntStream.range(0, size).forEach(i -> { + try { + resultSet.next(); + } catch (SQLException e) { + e.printStackTrace(); + System.exit(-1); + } + ArrayList list = new ArrayList(); + IntStream.range(1, columnsNum + 1).forEach( + col -> { + String value = null; + try { + value = resultSet.getString(col); + } catch (SQLException e) { + e.printStackTrace(); + System.exit(-1); + } + list.add(value); + } + ); + dbEntries.add(list); + }); + return dbEntries; + } + + public boolean equalityTest() throws SQLException { + ResultSet count1 = db1Connection.prepareStatement("select count(*) from " + table1Name).executeQuery(); + ResultSet count2 = db2Connection.prepareStatement("select count(*) from " + table2Name).executeQuery(); + count1.next(); + count2.next(); + int size1 = Integer.parseInt(count1.getString(1)); + int size2 = Integer.parseInt(count2.getString(1)); + + if (size1 != size2) { + return false; + } + if (size1 == 0) { + return true; + } + + PreparedStatement stmtDB1 = db1Connection.prepareStatement("select " + columnNames + " from " + table1Name); + PreparedStatement stmtDB2 = db2Connection.prepareStatement("select " + columnNames + " from " + table2Name); + ResultSet resultSet1 = stmtDB1.executeQuery(); + ResultSet resultSet2 = stmtDB2.executeQuery(); + + ArrayList> db1Entries = initArrayFromResultSet(resultSet1, size1); + ArrayList> db2Entries = initArrayFromResultSet(resultSet2, size2); + + db1Entries.sort((t1, t2) -> + IntStream.range(1, columnsNum).reduce(0, (acc, j) -> acc == 0 ? t1.get(j).compareTo(t2.get(j - 1)) : acc) + ); + db2Entries.sort((t1, t2) -> + IntStream.range(1, columnsNum).reduce(0, (acc, j) -> acc == 0 ? t1.get(j).compareTo(t2.get(j - 1)) : acc) + ); + + int result = IntStream.range(0, size1).reduce(0, + (acc, i) -> acc == 0 ? + IntStream.range(0, columnsNum).reduce(0, + (innerAcc, j) -> innerAcc == 0 ? + db1Entries.get(i).get(j).compareTo(db2Entries.get(i).get(j)) : innerAcc + ) : acc + ); + return result == 0; + } +} \ No newline at end of file