Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package worker;
- import java.sql.*;
- /**
- * Created by admin on 31.03.2018.
- */
- public class DBWorker {
- private final String HOST = "jdbc:mysql://localhost:3306/timur_base?autoReconnect=true&useSSL=false";
- private final String USER_NAME = "root";
- private final String PASSWORD = "root";
- private Connection connection;
- private PreparedStatement preStatement;
- public DBWorker() {
- try {
- connection = DriverManager.getConnection(HOST,USER_NAME,PASSWORD);
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public Connection getConnection() {
- return connection;
- }
- public PreparedStatement getPreStatement() {
- return preStatement;
- }
- public void addToTable(PassportOfPerson person){
- try {
- preStatement = connection.prepareStatement("INSERT INTO date_table( name, pasport, birthday) VALUES (?,?,?)");
- preStatement.setString(1,person.getName());
- preStatement.setString(2,person.getPasport());
- preStatement.setDate(3,person.getBirthday());
- preStatement.execute();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public void showAll(){
- try {
- preStatement = connection.prepareStatement("SELECT * FROM date_table");
- ResultSet set = preStatement.executeQuery();
- for(;set.next();){
- PassportOfPerson person = new PassportOfPerson();
- person.setId(set.getInt("id"));
- person.setName(set.getString("name"));
- person.setBirthday(set.getDate("birthday"));
- person.setPasport(set.getString("pasport"));
- System.out.println(person);
- }
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public void closeAll(){
- try {
- connection.close();
- preStatement.close();
- }catch (SQLException e){
- System.out.println("can not close");
- }
- }
- }
Add Comment
Please, Sign In to add comment