Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Scanner;
- public class Main {
- private static final Scanner SCANNER = new Scanner(System.in);
- public static final String OP1 = "Get all names and phone numbers by char.";
- public static final String OP2 = "Get contact info by specific name.";
- public static final String OP3 = "Add contact.";
- public static final String OP4 = "Remove contact.";
- public static final String EXIT = "Exit";
- //to create a connection to our Data Base
- private static Connection myDataBase;
- //will convert our SQL request to a statement that the DB will recognize
- private static PreparedStatement statement;
- //address of our mysql
- final static String URL = "jdbc:mysql://localhost/";
- //init time zone UTC
- final static String HOLD_JDBC = "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
- //name of the data base
- final static String DB_NAME = "phone_book";
- //name of user (in our case administrator)
- final static String DB_USER = "root";
- //administrator password
- final static String DB_PASS = "";
- public static void main(String[] args) throws IOException, SQLException {
- Contact_SQL contact_sql = new Contact_SQL(myDataBase, statement, URL, DB_NAME, HOLD_JDBC, DB_USER, DB_PASS);
- phoneBook_SQL(contact_sql);
- }
- static boolean phoneBook_SQL(Contact_SQL contact_sql) throws IOException, SQLException {
- String[] options = {OP1, OP2, OP3, OP4, EXIT};
- switch (Utils.menu(options)) {
- case 1:
- if (!(initSearchCharRequest(contact_sql)))
- return phoneBook_SQL(contact_sql);
- break;
- case 2:
- if (!(initSearchNameRequest(contact_sql)))
- return phoneBook_SQL(contact_sql);
- break;
- case 3:
- if (!(insertContactToTable(Utils.getNewContactInformation())))
- return phoneBook_SQL(contact_sql);
- break;
- case 4:
- initRemoveContactByName(contact_sql);
- return phoneBook_SQL(contact_sql);
- }
- return false;
- }
- private static boolean initRemoveContactByName(Contact_SQL contact_sql) throws IOException, SQLException {
- System.out.println("Type contact name to remove : ");
- String name = SCANNER.nextLine();
- contact_sql.removeContactByName(name);
- return true;
- }
- private static boolean insertContactToTable(String[] info) throws SQLException {
- Contact_SQL contactSql = new Contact_SQL(myDataBase, statement, URL, DB_NAME, HOLD_JDBC, DB_USER, DB_PASS, info[0], info[1], Integer.parseInt(info[2]), info[3], info[4]);
- contactSql.saferInsert(contactSql);
- return false;
- }
- private static boolean initSearchNameRequest(Contact_SQL contact_sql) throws IOException, SQLException {
- System.out.print("Type Contact name : ");
- String userName = SCANNER.nextLine();
- if (userName.equals("back")) {
- return false;
- }
- if (contact_sql.getByName(userName)) {
- return true;
- }
- return initSearchNameRequest(contact_sql);
- }
- static boolean initSearchCharRequest(Contact_SQL contact_sql) throws SQLException, IOException {
- System.out.print("Type your char : ");
- String uChar = SCANNER.nextLine();
- if (Utils.isChar(uChar)) {
- contact_sql.getDataByChar(uChar);
- return initSearchCharRequest(contact_sql);
- } else if (uChar.equals("back")) {
- return false;
- } else
- throw new IOException("Please NOTE : you chose to search by CHAR found : STRING");
- }
- }
- =======================================================================================================================
- package com.company;
- import java.io.IOException;
- import java.sql.*;
- import java.util.Vector;
- public abstract class SQL_Handler {
- //to create a connection to our Data Base
- private Connection myDataBase;
- //will convert our SQL request to a statement that the DB will recognize
- private PreparedStatement statement;
- //my localHost url
- private String URL;
- //init time zone UTC
- private String HOLD_JDBC;
- //name of the data base
- private String DB_NAME;
- //name of user (in our case administrator)
- private String DB_USER;
- //administrator password
- private String DB_PASS;
- private static final String SQL_CREATE_TABLE_COMMAND = "CREATE TABLE IF NOT EXISTS contacts_information " +
- "(char_Id TEXT (16) NOT NULL, " +
- "contact_name TEXT (16) NOT NULL, " +
- "phone_number INT PRIMARY KEY AUTO_INCREMENT, " +
- "city TEXT (16) NOT NULL, " +
- "address TEXT (16) NOT NULL)";
- public static final String SQL_INSERT_STATEMENT = "INSERT INTO contacts_information (char_Id,contact_name,phone_number,city,address) VALUES (?,?,?,?,?)";
- public SQL_Handler(Connection myDataBase, PreparedStatement statement, String URL, String DB_NAME, String HOLD_JDBC, String DB_USER, String DB_PASS) throws SQLException {
- this.myDataBase = myDataBase;
- this.statement = statement;
- this.URL = URL;
- this.DB_NAME = DB_NAME;
- this.HOLD_JDBC = HOLD_JDBC;
- this.DB_USER = DB_USER;
- this.DB_PASS = DB_PASS;
- this.myDataBase = DriverManager.getConnection(URL + DB_NAME + HOLD_JDBC, DB_USER, DB_PASS);
- initContactsTable();
- }
- void initContactsTable() throws SQLException {
- statement = myDataBase.prepareStatement(SQL_CREATE_TABLE_COMMAND);
- statement.execute();
- }
- boolean getDataByChar(String charId) throws SQLException {
- String sql = "SELECT * FROM contacts_information WHERE char_ID='" + charId + "'";
- ResultSet result = myDataBase.prepareStatement(sql).executeQuery();
- Vector<Contact_SQL> contact_sqls = new Vector<>();
- if (!result.next()) {
- System.out.println("No results for that char");
- return false;
- } else {
- for (result.first(); !result.isAfterLast(); result.next()) {
- Contact_SQL contactSql = new Contact_SQL(this.myDataBase, this.statement, this.URL, this.DB_NAME, this.HOLD_JDBC, this.DB_USER, this.DB_PASS, result.getString("char_Id"),
- result.getString("contact_name"), result.getInt("phone_number"), result.getString("city"), result.getNString("address"));
- contact_sqls.add(contactSql);
- }
- for (Contact_SQL contact_sql : contact_sqls) {
- System.out.println(contact_sql);
- }
- }
- return true;
- }
- void saferInsert(Contact_SQL contact_sql) throws SQLException {
- statement = myDataBase.prepareStatement(SQL_INSERT_STATEMENT);
- statement.setString(1, contact_sql.getCharId());
- statement.setString(2, contact_sql.getName());
- statement.setInt(3, contact_sql.getPhone_number());
- statement.setString(4, contact_sql.getCity());
- statement.setString(5, contact_sql.getAddress());
- statement.execute();
- System.out.println("contact was add ! ");
- }
- boolean getByName(String name) throws IOException, SQLException {
- String sql = "SELECT * FROM contacts_information WHERE contact_name='" + name + "'";
- ResultSet result = myDataBase.prepareStatement(sql).executeQuery();
- Vector<Contact_SQL> contact_sqls = new Vector<>();
- if (!result.next()) {
- System.out.println("No results for that name");
- return false;
- } else {
- for (result.first(); !result.isAfterLast(); result.next()) {
- Contact_SQL contactSql = new Contact_SQL(this.myDataBase, this.statement, this.URL, this.DB_NAME, this.HOLD_JDBC, this.DB_USER, this.DB_PASS, result.getString("char_Id"),
- result.getString("contact_name"), result.getInt("phone_number"), result.getString("city"), result.getNString("address"));
- contact_sqls.add(contactSql);
- }
- for (Contact_SQL contact_sql : contact_sqls) {
- System.out.println(contact_sql);
- }
- return true;
- }
- }
- void removeContactByName(String name) throws SQLException, IOException {
- if (getByName(name)){
- String sql = "DELETE FROM contacts_information WHERE contact_name='" +name + "'";
- statement = myDataBase.prepareStatement(sql);
- statement.execute();
- System.out.println("^^ Contact was deleted ^^");
- }
- }
- }
- ===========================================================================================================================
- package com.company;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class Contact_SQL extends SQL_Handler {
- private String charId;
- private String name;
- private int phone_number;
- private String city;
- private String address;
- public Contact_SQL(Connection myDataBase, PreparedStatement statement, String URL, String DB_NAME, String HOLD_JDBC, String DB_USER, String DB_PASS) throws SQLException {
- super(myDataBase,statement,URL,DB_NAME,HOLD_JDBC,DB_USER,DB_PASS);
- }
- public Contact_SQL(Connection myDataBase, PreparedStatement statement, String URL, String DB_NAME, String HOLD_JDBC, String DB_USER, String DB_PASS, String charId, String name, int phone_number, String city, String address) throws SQLException {
- super(myDataBase, statement, URL, DB_NAME, HOLD_JDBC, DB_USER, DB_PASS);
- this.charId = charId;
- this.name = name;
- this.phone_number = phone_number;
- this.city = city;
- this.address = address;
- }
- @Override
- public String toString() {
- return "Contact_SQL{" + "charId='" + charId + '\'' + ", name='" + name + '\'' + ", phone_number=" + phone_number + ", city='" + city + '\'' + ", address='" + address + '\'' + '}';
- }
- public String getCharId() {
- return charId;
- }
- public String getName() {
- return name;
- }
- public int getPhone_number() {
- return phone_number;
- }
- public String getCity() {
- return city;
- }
- public String getAddress() {
- return address;
- }
- }
- ===========================================================================================================================
- package com.company;
- import java.io.IOException;
- import java.util.Scanner;
- public class Utils {
- static final Scanner SCANNER = new Scanner(System.in);
- static final int CHAR_ZERO_VALUE = 48;
- static final int CHAR_NINE_VALUE = 57;
- public static int menu(String[] options) throws IOException {
- System.out.println("* MENU *");
- for (int i = 0; i < options.length; i++) {
- System.out.println((i + 1) + ". " + options[i]);
- }
- System.out.print("Type your choice : ");
- String choice = SCANNER.nextLine();
- if (!isNumber(choice.charAt(0)))
- throw new IOException("Must type only digits between " + 1 + " AND " + options.length);
- if (Integer.parseInt(choice) < 1 || Integer.parseInt(choice) > options.length) {
- throw new IOException("You choose Index OUT OF BOUND");
- } else
- return Integer.parseInt(choice);
- }
- static boolean isNumber(char userChar) {
- return userChar >= CHAR_ZERO_VALUE && userChar <= CHAR_NINE_VALUE;
- }
- static boolean isChar(String s){
- return s.length()==1;
- }
- static String[] getNewContactInformation() {
- String[] result = new String[5];
- System.out.print("Type name : ");
- result[1] = SCANNER.nextLine();
- result[0] = String.valueOf(result[1].charAt(0));
- System.out.print("Type phone number : ");
- result[2] = SCANNER.nextLine();
- System.out.print("Type city : ");
- result[3] = SCANNER.nextLine();
- System.out.print("Type address : ");
- result[4] = SCANNER.nextLine();
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement