Advertisement
Tane4kaG

Untitled

Nov 16th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.sql.*;
  3.  
  4. public class DateBase {
  5.  
  6. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  7. static final String DB_URL = "jdbc:mysql://localhost/addressbook?autoReconnect=true&useSSL=false";
  8. static final String USER = "root";
  9. static final String PASS = "333666666";
  10.  
  11. public static void readFromDB(AddressBook AddressBook) {
  12. Connection conn = null;
  13. Statement stmt = null;
  14.  
  15. try {
  16. Class.forName("com.mysql.jdbc.Driver");
  17. System.out.println("Connecting to database...");
  18. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  19.  
  20. System.out.println("Creating statement...");
  21. stmt = conn.createStatement();
  22. String sql;
  23. sql = "select first_name, last_name, age, gender, number_phone, address from person";
  24. ResultSet rs = stmt.executeQuery(sql);
  25.  
  26. while (rs.next()) {
  27. String firstName = rs.getString("first_name");
  28. String lastName = rs.getString("last_name");
  29. String age = rs.getString("age");
  30. String gender = rs.getString("gender");
  31. String numberPhone = rs.getString("number_phone");
  32. String address = rs.getString("address");
  33. Person person = new Person(firstName, lastName, age, gender, numberPhone, address);
  34. AddressBook.add(person);
  35. }
  36. rs.close();
  37. stmt.close();
  38. conn.close();
  39. } catch (SQLException se) {
  40. se.printStackTrace();
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. } finally {
  44. try {
  45. if (stmt != null)
  46. stmt.close();
  47. } catch (SQLException se2) {
  48. }
  49. try {
  50. if (conn != null)
  51. conn.close();
  52. } catch (SQLException se) {
  53. se.printStackTrace();
  54. }
  55. }
  56. System.out.println("Goodbye!");
  57.  
  58. }
  59.  
  60. public static void cleanDB() {
  61. Connection conn = null;
  62. Statement stmt = null;
  63.  
  64. try {
  65. Class.forName("com.mysql.jdbc.Driver");
  66. System.out.println("Connecting to database...");
  67. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  68.  
  69. System.out.println("Creating statement...");
  70. stmt = conn.createStatement();
  71. String sql1;
  72. sql1 = "truncate person";
  73. stmt.executeUpdate(sql1);
  74.  
  75. stmt.close();
  76. conn.close();
  77. } catch (SQLException se) {
  78. se.printStackTrace();
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. } finally {
  82. try {
  83. if (stmt != null)
  84. stmt.close();
  85. } catch (SQLException se2) {
  86. }
  87. try {
  88. if (conn != null)
  89. conn.close();
  90. } catch (SQLException se) {
  91. se.printStackTrace();
  92. }
  93. }
  94. System.out.println("Goodbye!");
  95.  
  96. }
  97.  
  98. public static void saveToDB(AddressBook addressBook) {
  99. Connection conn = null;
  100. Statement stmt = null;
  101. try {
  102. Class.forName("com.mysql.jdbc.Driver");
  103. System.out.println("Connecting to database...");
  104. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  105. System.out.println("Creating statement...");
  106. stmt = conn.createStatement();
  107. for (int i = 0; i < addressBook.size(); i++) {
  108. Person person = new Person();
  109. person = addressBook.get(i);
  110. String firstName = person.getFirstName();
  111. String lastName = person.getLastName();
  112. String age = person.getAge();
  113. String gender = person.getGender();
  114. String numberPhone = person.getNumberPhone();
  115. String address = person.getAddress();
  116. String sql2 = "insert into person (first_name, last_name, age, gender, number_phone, address) values ('"
  117. + firstName + "', '" + lastName + "', '" + age + "', '" + gender + "', '" + numberPhone + "', '"
  118. + address + "')";
  119. stmt.executeUpdate(sql2);
  120. }
  121.  
  122. stmt.close();
  123. conn.close();
  124. } catch (SQLException se) {
  125. se.printStackTrace();
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. } finally {
  129. try {
  130. if (stmt != null)
  131. stmt.close();
  132. } catch (SQLException se2) {
  133. }
  134. try {
  135. if (conn != null)
  136. conn.close();
  137. } catch (SQLException se) {
  138. se.printStackTrace();
  139. }
  140. }
  141. System.out.println("Goodbye!");
  142. }
  143.  
  144. /*
  145. * public static void writeToDB(AddressBook addressBook) {
  146. * cleanDB();
  147. * saveToDB(addressBook); }
  148. */
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement