diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 00000000..74f5db79
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/../../../../../../:\Users\sleng\IdeaProjects\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..24736517
--- /dev/null
+++ b/.idea/.name
@@ -0,0 +1 @@
+TaskJDBC
\ No newline at end of file
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/dataSources.xml b/.idea/dataSources.xml
new file mode 100644
index 00000000..7314ba48
--- /dev/null
+++ b/.idea/dataSources.xml
@@ -0,0 +1,15 @@
+
+
+
+
+ mysql.8
+ true
+ com.mysql.cj.jdbc.Driver
+ jdbc:mysql://localhost:3306/mydb
+ $ProjectFileDir$
+
+
+
+
+
+
\ 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__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__mysql_mysql_connector_java_8_0_22.xml b/.idea/libraries/Maven__mysql_mysql_connector_java_8_0_22.xml
new file mode 100644
index 00000000..4de6b45f
--- /dev/null
+++ b/.idea/libraries/Maven__mysql_mysql_connector_java_8_0_22.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/mysql-connector-java-8.0.22.jar b/.idea/libraries/mysql-connector-java-8.0.22.jar
new file mode 100644
index 00000000..412d81fa
Binary files /dev/null and b/.idea/libraries/mysql-connector-java-8.0.22.jar differ
diff --git a/.idea/libraries/mysql_connector_java_8_0_22.xml b/.idea/libraries/mysql_connector_java_8_0_22.xml
new file mode 100644
index 00000000..3aae4731
--- /dev/null
+++ b/.idea/libraries/mysql_connector_java_8_0_22.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 00000000..32b0695a
--- /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..f6e26690
--- /dev/null
+++ b/TaskJDBC.iml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 0605a4c0..16ab4324 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,6 +20,7 @@
+
@@ -47,6 +48,13 @@
hibernate-core
4.3.10.Final
-
+
+
+ mysql
+ mysql-connector-java
+ 8.0.22
+
+
+
\ No newline at end of file
diff --git a/src/main/java/jm/task/core/jdbc/Main.java b/src/main/java/jm/task/core/jdbc/Main.java
index f6e43b0f..61ff6cf1 100644
--- a/src/main/java/jm/task/core/jdbc/Main.java
+++ b/src/main/java/jm/task/core/jdbc/Main.java
@@ -1,7 +1,36 @@
package jm.task.core.jdbc;
+import jm.task.core.jdbc.dao.UserDaoJDBCImpl;
+import jm.task.core.jdbc.model.User;
+import jm.task.core.jdbc.service.UserService;
+import jm.task.core.jdbc.service.UserServiceImpl;
+import jm.task.core.jdbc.util.Util;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.List;
+
+
public class Main {
- public static void main(String[] args) {
- // реализуйте алгоритм здесь
+
+
+ public static void main(String[] args) throws SQLException, ClassNotFoundException {
+
+ UserService userServ = new UserServiceImpl();
+ userServ.createUsersTable();
+ userServ.saveUser("John", "Nikson", (byte) 56);
+ userServ.saveUser("Sarah", "Konor", (byte) 34);
+ userServ.saveUser("Peter", "Mexes", (byte) 33);
+ userServ.saveUser("Leam", "Howlet", (byte) 51);
+
+ System.out.println(userServ.getAllUsers());
+
+ userServ.removeUserById(2);
+ userServ.cleanUsersTable();
+ userServ.dropUsersTable();
+
+ Util.getConnectionToDatabase().close();
}
}
+
+
diff --git a/src/main/java/jm/task/core/jdbc/dao/UserDao.java b/src/main/java/jm/task/core/jdbc/dao/UserDao.java
index 27069baf..d2561062 100644
--- a/src/main/java/jm/task/core/jdbc/dao/UserDao.java
+++ b/src/main/java/jm/task/core/jdbc/dao/UserDao.java
@@ -2,18 +2,19 @@
import jm.task.core.jdbc.model.User;
+import java.sql.SQLException;
import java.util.List;
public interface UserDao {
- void createUsersTable();
+ void createUsersTable() throws SQLException, ClassNotFoundException;
- void dropUsersTable();
+ void dropUsersTable() throws SQLException, ClassNotFoundException;
- void saveUser(String name, String lastName, byte age);
+ void saveUser(String name, String lastName, byte age) throws SQLException, ClassNotFoundException;
- void removeUserById(long id);
+ void removeUserById(long id) throws SQLException, ClassNotFoundException;
- List getAllUsers();
+ List getAllUsers() throws SQLException, ClassNotFoundException;
- void cleanUsersTable();
+ void cleanUsersTable() throws SQLException, ClassNotFoundException;
}
diff --git a/src/main/java/jm/task/core/jdbc/dao/UserDaoJDBCImpl.java b/src/main/java/jm/task/core/jdbc/dao/UserDaoJDBCImpl.java
index a6c6c359..d505c7f7 100644
--- a/src/main/java/jm/task/core/jdbc/dao/UserDaoJDBCImpl.java
+++ b/src/main/java/jm/task/core/jdbc/dao/UserDaoJDBCImpl.java
@@ -1,35 +1,98 @@
package jm.task.core.jdbc.dao;
import jm.task.core.jdbc.model.User;
+import jm.task.core.jdbc.util.Util;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
import java.util.List;
public class UserDaoJDBCImpl implements UserDao {
+ private String create = "CREATE TABLE IF NOT EXISTS user" +
+ "(id BIGINT not NULL AUTO_INCREMENT , /n " +
+ " name VARCHAR(50) not NULL, /n" +
+ " lastname VARCHAR(50) not NULL, /n " + // так ?
+ " age TINYINT not NULL, /n" +
+ " PRIMARY KEY (id)) /n";
+ private String insert = "INSERT INTO user (name, last name, age) Values(?, ?, ?);";
+ private String removeUserById = "DELETE FROM user WHERE id = ?";
+ private String delete = "DELETE FROM tb_name;";
+
public UserDaoJDBCImpl() {
}
- public void createUsersTable() {
+ public void createUsersTable() throws SQLException, ClassNotFoundException {
+ Statement statement2 = Util.getConnectionToDatabase().createStatement();
+ statement2.executeUpdate("CREATE TABLE IF NOT EXISTS user2" +
+ "(id BIGINT not NULL AUTO_INCREMENT ," +
+ " name VARCHAR(50) not NULL," +
+ " lastname VARCHAR(50) not NULL, " +
+ " age TINYINT not NULL," +
+ " PRIMARY KEY (id))");
+ System.out.println("Таблица создана успешно");
+ statement2.close();
+ System.out.println("statement закрыт");
}
- public void dropUsersTable() {
+ public void dropUsersTable() throws SQLException, ClassNotFoundException {
+ Statement statement2 = Util.getConnectionToDatabase().createStatement(); // Создаем соединение с БД
+ statement2.executeUpdate("TRUNCATE user2;");
+ System.out.println("Таблица удалена");
+ statement2.close();
+ System.out.println("Соединение закрыто");
- }
- public void saveUser(String name, String lastName, byte age) {
}
- public void removeUserById(long id) {
-
+ public void saveUser(String name, String lastName, byte age) throws SQLException, ClassNotFoundException {
+ Statement statement2 = Util.getConnectionToDatabase().createStatement();// Создаем соединение с БД
+// statement2.executeUpdate(String.format("INSERT INTO FROM user2 WHERE name=%s, lastname=%s, age=%d;", name, lastName, age));
+ statement2.executeUpdate("insert user2 (name, lastName, age) values " +
+ "(\'" + name + "\', \'" + lastName +"\', " + age + ");");
+ statement2.close();
+ System.out.println("Соединение закрыто");
}
- public List getAllUsers() {
- return null;
+ public void removeUserById(long id) throws SQLException, ClassNotFoundException {
+ Statement statement2 = Util.getConnectionToDatabase().createStatement();// Создаем соединение с БД
+ statement2.executeUpdate(String.format("DELETE FROM user2 WHERE id=%d;", id)); // созд. стэйтмент для раработы с данными
+ System.out.println("удаление юзера под id= " + id + " выполнено");
+ statement2.close();
+ System.out.println("Соединение закрыто");
}
- public void cleanUsersTable() {
+ public List getAllUsers() throws SQLException, ClassNotFoundException {
+ String showALL = "SELECT * FROM user2;";
+
+ Statement statement2 = Util.getConnectionToDatabase().createStatement();
+ ResultSet resultSet = statement2.executeQuery(showALL);
+ List list = new ArrayList<>();
+
+ while (resultSet.next()) {
+ String name;
+ String lastname;
+ byte age;
+ name = resultSet.getString("name");
+ lastname = resultSet.getString("lastname");
+ age = resultSet.getByte("age");
+ list.add(new User(name, lastname, age));
+ }
+ statement2.close();
+ Util.getConnectionToDatabase().close();
+ System.out.println("Соединение закрыто");
+ return list;
+ }
+ public void cleanUsersTable() throws SQLException, ClassNotFoundException {
+ Statement statement2 = Util.getConnectionToDatabase().createStatement();// Создаем соединение с БД
+ statement2.executeUpdate("DELETE FROM user2;");
+ System.out.println("Удаление выполнено");
+ statement2.close();
+ System.out.println("Соединение закрыто");
}
}
diff --git a/src/main/java/jm/task/core/jdbc/model/User.java b/src/main/java/jm/task/core/jdbc/model/User.java
index 21b054f6..db92d334 100644
--- a/src/main/java/jm/task/core/jdbc/model/User.java
+++ b/src/main/java/jm/task/core/jdbc/model/User.java
@@ -40,6 +40,16 @@ public String getName() {
return name;
}
+ @Override
+ public String toString() {
+ return "User{" +
+ "id=" + id +
+ ", name='" + name + '\'' + // переопределенный метод toString
+ ", lastName='" + lastName + '\'' +
+ ", age=" + age +
+ '}';
+ }
+
public void setName(String name) {
this.name = name;
}
diff --git a/src/main/java/jm/task/core/jdbc/service/UserService.java b/src/main/java/jm/task/core/jdbc/service/UserService.java
index 80542c68..5a263590 100644
--- a/src/main/java/jm/task/core/jdbc/service/UserService.java
+++ b/src/main/java/jm/task/core/jdbc/service/UserService.java
@@ -2,18 +2,19 @@
import jm.task.core.jdbc.model.User;
+import java.sql.SQLException;
import java.util.List;
public interface UserService {
- void createUsersTable();
+ void createUsersTable() throws SQLException, ClassNotFoundException;
- void dropUsersTable();
+ void dropUsersTable() throws SQLException, ClassNotFoundException;
- void saveUser(String name, String lastName, byte age);
+ void saveUser(String name, String lastName, byte age) throws SQLException, ClassNotFoundException;
- void removeUserById(long id);
+ void removeUserById(long id) throws SQLException, ClassNotFoundException;
- List getAllUsers();
+ List getAllUsers() throws SQLException, ClassNotFoundException;
- void cleanUsersTable();
+ void cleanUsersTable() throws SQLException, ClassNotFoundException;
}
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..59b4c5b0 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,43 @@
package jm.task.core.jdbc.service;
+import jm.task.core.jdbc.dao.UserDao;
+import jm.task.core.jdbc.dao.UserDaoJDBCImpl;
import jm.task.core.jdbc.model.User;
+import jm.task.core.jdbc.util.Util;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
import java.util.List;
-public class UserServiceImpl implements UserService {
- public void createUsersTable() {
+public class UserServiceImpl implements UserService {
+ UserDao usd = new UserDaoJDBCImpl();
+ public void createUsersTable() throws SQLException, ClassNotFoundException {
+ usd.createUsersTable();
}
- public void dropUsersTable() {
+ public void dropUsersTable() throws SQLException, ClassNotFoundException {
+ usd.dropUsersTable();
}
- public void saveUser(String name, String lastName, byte age) {
-
+ public void saveUser(String name, String lastName, byte age) throws SQLException, ClassNotFoundException {
+ usd.saveUser(name,lastName, age);
}
- public void removeUserById(long id) {
-
+ public void removeUserById(long id) throws SQLException, ClassNotFoundException {
+ usd.removeUserById(id);
}
- public List getAllUsers() {
- return null;
+ public List getAllUsers() throws SQLException, ClassNotFoundException {
+
+ return usd.getAllUsers();
}
- public void cleanUsersTable() {
+ public void cleanUsersTable() throws SQLException, ClassNotFoundException {
+ usd.cleanUsersTable();
}
}
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..7bccbc57 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,19 @@
package jm.task.core.jdbc.util;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
public class Util {
- // реализуйте настройку соеденения с БД
+ public static Connection getConnectionToDatabase() throws ClassNotFoundException, SQLException {
+
+ final String DB_URL = "jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC";
+ final String DB_Driver = "java.sql.Driver";
+
+ Connection connection = DriverManager.getConnection(DB_URL, "root", "Wusleng567706"); //соединениесБД
+ System.out.println("Соединение с СУБД выполнено.");
+
+ return connection;
+ }
}