Advertisement
kocev

Untitled

Oct 30th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. package app;
  2.  
  3. import java.io.InputStream;
  4. import java.io.Reader;
  5. import java.math.BigDecimal;
  6. import java.net.URL;
  7. import java.sql.*;
  8. import java.util.*;
  9. import java.util.concurrent.Executor;
  10.  
  11. public class Engine implements Runnable {
  12.  
  13. private Connection connection;
  14.  
  15. public Engine(Connection connection) {
  16. this.connection = connection;
  17. }
  18.  
  19. public void run() {
  20. Scanner scanner = new Scanner(System.in);
  21. String countryName = scanner.nextLine();
  22. try {
  23. this.getTown(countryName);
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. try {
  28. this.setTown(countryName);
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }
  32. try {
  33. this.printTown();
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. List<String> tempList;
  39. private void getTown(String countryName) throws SQLException {
  40. List<String> tempList = new ArrayList();
  41. String query = String.format("SELECT name FROM towns WHERE country = '%s'",countryName);
  42. PreparedStatement preparedStatement = this.connection.prepareStatement(query);
  43. ResultSet resultSet = preparedStatement.executeQuery();
  44. if (resultSet == null) {
  45. System.out.println("No town names were affected.");
  46. }
  47. while (resultSet.next()){
  48. this.tempList.add(resultSet.getString(1));
  49. }
  50. }
  51. private void setTown(String countryName) throws SQLException {
  52. for (String item:tempList) {
  53. String query = String.format("REPLACE(name,'%s','%s') FROM towns WHERE country = %s",item,item.toUpperCase(),countryName);
  54. PreparedStatement preparedStatement = this.connection.prepareStatement(query);
  55. preparedStatement.execute();
  56. }
  57. }
  58. private void printTown() throws SQLException {
  59. String query = String.format("SELECT name, count(id) FROM towns");
  60. PreparedStatement preparedStatement = this.connection.prepareStatement(query);
  61. ResultSet resultSet = preparedStatement.executeQuery();
  62. System.out.println(String.format("%d town names were affected.",resultSet.getInt(2)));
  63. System.out.print("[");
  64. while (resultSet.next()){
  65. System.out.print(String.format(resultSet.getString(1)));
  66. if (resultSet.next()) {
  67. System.out.print(",");
  68.  
  69. }
  70. }
  71. System.out.println("]");
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement