Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.sql.*;
- public class DateBase {
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost/addressbook?autoReconnect=true&useSSL=false";
- static final String USER = "root";
- static final String PASS = "333666666";
- public static void readFromDB(AddressBook AddressBook) {
- Connection conn = null;
- Statement stmt = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- sql = "select first_name, last_name, age, gender, number_phone, address from person";
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- String firstName = rs.getString("first_name");
- String lastName = rs.getString("last_name");
- String age = rs.getString("age");
- String gender = rs.getString("gender");
- String numberPhone = rs.getString("number_phone");
- String address = rs.getString("address");
- Person person = new Person(firstName, lastName, age, gender, numberPhone, address);
- AddressBook.add(person);
- }
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- }
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- System.out.println("Goodbye!");
- }
- public static void cleanDB() {
- Connection conn = null;
- Statement stmt = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql1;
- sql1 = "truncate person";
- stmt.executeUpdate(sql1);
- stmt.close();
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- }
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- System.out.println("Goodbye!");
- }
- public static void saveToDB(AddressBook addressBook) {
- Connection conn = null;
- Statement stmt = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- for (int i = 0; i < addressBook.size(); i++) {
- Person person = new Person();
- person = addressBook.get(i);
- String firstName = person.getFirstName();
- String lastName = person.getLastName();
- String age = person.getAge();
- String gender = person.getGender();
- String numberPhone = person.getNumberPhone();
- String address = person.getAddress();
- String sql2 = "insert into person (first_name, last_name, age, gender, number_phone, address) values ('"
- + firstName + "', '" + lastName + "', '" + age + "', '" + gender + "', '" + numberPhone + "', '"
- + address + "')";
- stmt.executeUpdate(sql2);
- }
- stmt.close();
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- }
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- System.out.println("Goodbye!");
- }
- /*
- * public static void writeToDB(AddressBook addressBook) {
- * cleanDB();
- * saveToDB(addressBook); }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement