Guest User

Untitled

a guest
Apr 8th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package worker;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6. * Created by admin on 31.03.2018.
  7. */
  8. public class DBWorker {
  9. private final String HOST = "jdbc:mysql://localhost:3306/timur_base?autoReconnect=true&useSSL=false";
  10. private final String USER_NAME = "root";
  11. private final String PASSWORD = "root";
  12. private Connection connection;
  13. private PreparedStatement preStatement;
  14.  
  15. public DBWorker() {
  16. try {
  17. connection = DriverManager.getConnection(HOST,USER_NAME,PASSWORD);
  18. }catch (SQLException e){
  19. e.printStackTrace();
  20. }
  21. }
  22.  
  23. public Connection getConnection() {
  24. return connection;
  25. }
  26.  
  27. public PreparedStatement getPreStatement() {
  28. return preStatement;
  29. }
  30.  
  31. public void addToTable(PassportOfPerson person){
  32. try {
  33. preStatement = connection.prepareStatement("INSERT INTO date_table( name, pasport, birthday) VALUES (?,?,?)");
  34. preStatement.setString(1,person.getName());
  35. preStatement.setString(2,person.getPasport());
  36. preStatement.setDate(3,person.getBirthday());
  37. preStatement.execute();
  38. }catch (SQLException e){
  39. e.printStackTrace();
  40. }
  41. }
  42. public void showAll(){
  43. try {
  44. preStatement = connection.prepareStatement("SELECT * FROM date_table");
  45. ResultSet set = preStatement.executeQuery();
  46. for(;set.next();){
  47. PassportOfPerson person = new PassportOfPerson();
  48. person.setId(set.getInt("id"));
  49. person.setName(set.getString("name"));
  50. person.setBirthday(set.getDate("birthday"));
  51. person.setPasport(set.getString("pasport"));
  52. System.out.println(person);
  53. }
  54. }catch (SQLException e){
  55. e.printStackTrace();
  56. }
  57. }
  58. public void closeAll(){
  59. try {
  60. connection.close();
  61. preStatement.close();
  62. }catch (SQLException e){
  63. System.out.println("can not close");
  64. }
  65. }
  66.  
  67. }
Add Comment
Please, Sign In to add comment