diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..71c4a755 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../:\java\CoreTaskTemplate\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000..e69de29b diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..9acf478f --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 00000000..712ab9d9 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__antlr_antlr_2_7_7.xml b/.idea/libraries/Maven__antlr_antlr_2_7_7.xml new file mode 100644 index 00000000..b8d93d8e --- /dev/null +++ b/.idea/libraries/Maven__antlr_antlr_2_7_7.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_protobuf_protobuf_java_3_11_4.xml b/.idea/libraries/Maven__com_google_protobuf_protobuf_java_3_11_4.xml new file mode 100644 index 00000000..d60deb07 --- /dev/null +++ b/.idea/libraries/Maven__com_google_protobuf_protobuf_java_3_11_4.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__dom4j_dom4j_1_6_1.xml b/.idea/libraries/Maven__dom4j_dom4j_1_6_1.xml new file mode 100644 index 00000000..14681ee8 --- /dev/null +++ b/.idea/libraries/Maven__dom4j_dom4j_1_6_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 00000000..d4110417 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__mysql_mysql_connector_java_8_0_21.xml b/.idea/libraries/Maven__mysql_mysql_connector_java_8_0_21.xml new file mode 100644 index 00000000..85958a17 --- /dev/null +++ b/.idea/libraries/Maven__mysql_mysql_connector_java_8_0_21.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 00000000..f58bbc11 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hibernate_common_hibernate_commons_annotations_4_0_5_Final.xml b/.idea/libraries/Maven__org_hibernate_common_hibernate_commons_annotations_4_0_5_Final.xml new file mode 100644 index 00000000..9ed6d297 --- /dev/null +++ b/.idea/libraries/Maven__org_hibernate_common_hibernate_commons_annotations_4_0_5_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hibernate_hibernate_core_4_3_10_Final.xml b/.idea/libraries/Maven__org_hibernate_hibernate_core_4_3_10_Final.xml new file mode 100644 index 00000000..0f9bf7c2 --- /dev/null +++ b/.idea/libraries/Maven__org_hibernate_hibernate_core_4_3_10_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hibernate_javax_persistence_hibernate_jpa_2_1_api_1_0_2_Final.xml b/.idea/libraries/Maven__org_hibernate_javax_persistence_hibernate_jpa_2_1_api_1_0_2_Final.xml new file mode 100644 index 00000000..1d8ce405 --- /dev/null +++ b/.idea/libraries/Maven__org_hibernate_javax_persistence_hibernate_jpa_2_1_api_1_0_2_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_javassist_javassist_3_18_1_GA.xml b/.idea/libraries/Maven__org_javassist_javassist_3_18_1_GA.xml new file mode 100644 index 00000000..1f907308 --- /dev/null +++ b/.idea/libraries/Maven__org_javassist_javassist_3_18_1_GA.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jboss_jandex_1_1_0_Final.xml b/.idea/libraries/Maven__org_jboss_jandex_1_1_0_Final.xml new file mode 100644 index 00000000..d3cd0897 --- /dev/null +++ b/.idea/libraries/Maven__org_jboss_jandex_1_1_0_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jboss_logging_jboss_logging_3_1_3_GA.xml b/.idea/libraries/Maven__org_jboss_logging_jboss_logging_3_1_3_GA.xml new file mode 100644 index 00000000..815ab4d8 --- /dev/null +++ b/.idea/libraries/Maven__org_jboss_logging_jboss_logging_3_1_3_GA.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jboss_logging_jboss_logging_annotations_1_2_0_Beta1.xml b/.idea/libraries/Maven__org_jboss_logging_jboss_logging_annotations_1_2_0_Beta1.xml new file mode 100644 index 00000000..715be298 --- /dev/null +++ b/.idea/libraries/Maven__org_jboss_logging_jboss_logging_annotations_1_2_0_Beta1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jboss_spec_javax_transaction_jboss_transaction_api_1_2_spec_1_0_0_Final.xml b/.idea/libraries/Maven__org_jboss_spec_javax_transaction_jboss_transaction_api_1_2_spec_1_0_0_Final.xml new file mode 100644 index 00000000..2ab91298 --- /dev/null +++ b/.idea/libraries/Maven__org_jboss_spec_javax_transaction_jboss_transaction_api_1_2_spec_1_0_0_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__xml_apis_xml_apis_1_0_b2.xml b/.idea/libraries/Maven__xml_apis_xml_apis_1_0_b2.xml new file mode 100644 index 00000000..c36e7171 --- /dev/null +++ b/.idea/libraries/Maven__xml_apis_xml_apis_1_0_b2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..c0572aa0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..882550cd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/TaskJDBC.iml b/TaskJDBC.iml new file mode 100644 index 00000000..979ee32c --- /dev/null +++ b/TaskJDBC.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0605a4c0..bba0255e 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ mysql mysql-connector-java - 5.1.38 + 8.0.21 diff --git a/src/main/java/jm/task/core/jdbc/Main.java b/src/main/java/jm/task/core/jdbc/Main.java index f6e43b0f..f22f207a 100644 --- a/src/main/java/jm/task/core/jdbc/Main.java +++ b/src/main/java/jm/task/core/jdbc/Main.java @@ -1,7 +1,29 @@ package jm.task.core.jdbc; +import jm.task.core.jdbc.service.UserService; +import jm.task.core.jdbc.service.UserServiceImpl; +import jm.task.core.jdbc.util.Util; +import java.lang.reflect.InvocationTargetException; +import java.sql.SQLException; + +/* версия 1.0 - добавляем в новый репозиторий */ + public class Main { - public static void main(String[] args) { - // реализуйте алгоритм здесь + public static void main(String[] args) throws SQLException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + + Util util = new Util(); + // util.setConnection(); + UserService userService = new UserServiceImpl(); + userService.createUsersTable(); + //userService.dropUsersTable(); + //userService.cleanUsersTable(); + + userService.saveUser("Arny", "Schwartz", (byte) 65); + userService.saveUser("Jhony", "Depp", (byte) 45); + userService.saveUser("Kianu ", "Rivs", (byte) 47); + userService.getAllUsers(); + userService.removeUserById(2); + } } + diff --git a/src/main/java/jm/task/core/jdbc/service/UserServiceImpl.java b/src/main/java/jm/task/core/jdbc/service/UserServiceImpl.java index b05bd191..968c857a 100644 --- a/src/main/java/jm/task/core/jdbc/service/UserServiceImpl.java +++ b/src/main/java/jm/task/core/jdbc/service/UserServiceImpl.java @@ -1,31 +1,154 @@ package jm.task.core.jdbc.service; import jm.task.core.jdbc.model.User; +import jm.task.core.jdbc.util.Util; +import java.sql.*; +import java.util.ArrayList; import java.util.List; public class UserServiceImpl implements UserService { - public void createUsersTable() { + private String tableName = "users"; + Util util = new Util(); + Connection connection = null; + Statement statement = null; + public void createUsersTable() { + String createUsersTable = "CREATE TABLE IF NOT EXISTS " + + tableName + + "(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY , " + + "name VARCHAR(30) NOT NULL, " + + "lastName VARCHAR(30) NOT NULL, " + + "age MEDIUMINT NOT NULL)"; + try { + connection = util.setConnection(); + statement = connection.createStatement(); + statement.execute(createUsersTable); + System.out.println("Таблица создана"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + connection.close(); + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public void dropUsersTable() { - + String dropUsersTable = "DROP TABLE IF EXISTS " + + tableName; + try { + connection = util.setConnection(); + statement = connection.createStatement(); + statement.execute(dropUsersTable); + System.out.println("Таблица удалена"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + connection.close(); + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public void saveUser(String name, String lastName, byte age) { + String saveUser = "INSERT INTO " + + tableName + + "(name , lastName, age) " + + " values ('" + name + "', '" + lastName + "', '" + age + "')"; + try { + connection = util.setConnection(); + statement = connection.createStatement(); + statement.executeUpdate(saveUser); + System.out.println("user " + name + " добавлен"); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + connection.close(); + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public void removeUserById(long id) { + String removeUserById = "DELETE FROM " + + tableName + + " WHERE id = " + Long.toString(id); + try { + connection = util.setConnection(); + statement = connection.createStatement(); + statement.executeUpdate(removeUserById); + System.out.println("user id = " + id +" удален"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + connection.close(); + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public List getAllUsers() { - return null; + List users = new ArrayList<>(); + + try { + connection = util.setConnection(); + statement = connection.createStatement(); + + ResultSet resultSet = statement.executeQuery("SELECT * FROM " + tableName); + + while (resultSet.next()) { + long id = resultSet.getInt(1); + String name = resultSet.getString(2); + String lastName = resultSet.getString(3); + byte age = resultSet.getByte(4); + User user = new User(name, lastName, age); + users.add(user); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return users; } public void cleanUsersTable() { - + String cleanUsersTable = "TRUNCATE TABLE " + + tableName; + try { + connection = util.setConnection(); + statement = connection.createStatement(); + statement.execute(cleanUsersTable); + System.out.println("Таблица очищена"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } } diff --git a/src/main/java/jm/task/core/jdbc/util/Util.java b/src/main/java/jm/task/core/jdbc/util/Util.java index 59e462ba..6bb4397c 100644 --- a/src/main/java/jm/task/core/jdbc/util/Util.java +++ b/src/main/java/jm/task/core/jdbc/util/Util.java @@ -1,5 +1,28 @@ package jm.task.core.jdbc.util; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + public class Util { - // реализуйте настройку соеденения с БД + private String userName = "root"; + private String password = "java-mentor"; + private String connectionUrl = "jdbc:mysql://localhost:3306/lesson113?serverTimezone=Europe/Moscow&useSSL=false"; + Connection connection = null; + + public Connection setConnection() { + + try { + Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance(); + connection = DriverManager.getConnection(connectionUrl, userName, password); + System.out.println("We're connected"); + } catch (SQLException e) { + System.out.println("Didn't connected (class Util)"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + return connection; + } }