Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app;
- import java.io.InputStream;
- import java.io.Reader;
- import java.math.BigDecimal;
- import java.net.URL;
- import java.sql.*;
- import java.util.*;
- import java.util.concurrent.Executor;
- public class Engine implements Runnable {
- private Connection connection;
- public Engine(Connection connection) {
- this.connection = connection;
- }
- public void run() {
- Scanner scanner = new Scanner(System.in);
- String countryName = scanner.nextLine();
- try {
- this.getTown(countryName);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- this.setTown(countryName);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- this.printTown();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- List<String> tempList;
- private void getTown(String countryName) throws SQLException {
- List<String> tempList = new ArrayList();
- String query = String.format("SELECT name FROM towns WHERE country = '%s'",countryName);
- PreparedStatement preparedStatement = this.connection.prepareStatement(query);
- ResultSet resultSet = preparedStatement.executeQuery();
- if (resultSet == null) {
- System.out.println("No town names were affected.");
- }
- while (resultSet.next()){
- this.tempList.add(resultSet.getString(1));
- }
- }
- private void setTown(String countryName) throws SQLException {
- for (String item:tempList) {
- String query = String.format("REPLACE(name,'%s','%s') FROM towns WHERE country = %s",item,item.toUpperCase(),countryName);
- PreparedStatement preparedStatement = this.connection.prepareStatement(query);
- preparedStatement.execute();
- }
- }
- private void printTown() throws SQLException {
- String query = String.format("SELECT name, count(id) FROM towns");
- PreparedStatement preparedStatement = this.connection.prepareStatement(query);
- ResultSet resultSet = preparedStatement.executeQuery();
- System.out.println(String.format("%d town names were affected.",resultSet.getInt(2)));
- System.out.print("[");
- while (resultSet.next()){
- System.out.print(String.format(resultSet.getString(1)));
- if (resultSet.next()) {
- System.out.print(",");
- }
- }
- System.out.println("]");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement