Guest User

Untitled

a guest
Dec 12th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import java.sql.Date;
  2.  
  3. class Customer {
  4. private String first_name;
  5. private String last_name;
  6. private String gedner;
  7. private Date age;
  8. private String address;
  9.  
  10.  
  11. Customer(String first_name, String last_name, String gedner, Date age, String address) {
  12. this.first_name = first_name;
  13. this.last_name = last_name;
  14. this.gedner = gedner;
  15. this.age = age;
  16. this.address = address;
  17. }
  18.  
  19. public String getLast_name() {
  20. return last_name;
  21. }
  22.  
  23. public String getFirst_name() {
  24. return first_name;
  25. }
  26.  
  27. public String getGedner() {
  28. return gedner;
  29. }
  30.  
  31. public Date getAge() {
  32. return age;
  33. }
  34.  
  35. public String getAddress() {
  36. return address;
  37. }
  38. }
  39.  
  40. import java.sql.*;
  41. import java.util.Scanner;
  42.  
  43.  
  44. public class Bank {
  45. private static final String URL = "jdbc:mysql://localhost/bank?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  46. private static final String USER = "root";
  47. private static final String PASSWORD = "root";
  48.  
  49. Connection connection = null;
  50. PreparedStatement preparedStatement = null;
  51. Scanner in = new Scanner(System.in);
  52.  
  53. public void connect() {
  54. try {
  55. connection = DriverManager.getConnection(URL, USER, PASSWORD);
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60.  
  61. Customer readCustomer() {
  62. Customer customer = null;
  63. try {
  64. preparedStatement = connection.prepareStatement("SELECT * FROM customers WHERE id_customer = ?");
  65. System.out.print("Введите id пользователя: ");
  66. preparedStatement.setInt(1, Integer.parseInt(in.next()));
  67. preparedStatement.execute();
  68. ResultSet rs = preparedStatement.executeQuery();
  69. String first_name = rs.getString("first_name");
  70. String last_name = rs.getString("last_name");
  71. String gender = rs.getString("gender");
  72. Date age = rs.getDate("age");
  73. String address = rs.getString("address");
  74. customer = new Customer(first_name, last_name, gender, age, address);
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. } finally {
  78. try {
  79. preparedStatement.close();
  80. connection.close();
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. return customer;
  86. }
  87. }
Add Comment
Please, Sign In to add comment