Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;
- /**
- * Created by Aleksey on 09.08.2016.
- */
- public class Main {
- static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/orderDB";
- static final String DB_USER = "root";
- static final String DB_PASSWORD = "eniva1989";
- static Connection conn;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- try {
- try {
- // create connection
- conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
- initDB();
- while (true) {
- System.out.println("1: add client");
- System.out.println("2: add product");
- System.out.println("3: add order");
- System.out.println("4: view orders");
- System.out.print("->");
- String s = sc.nextLine();
- switch (s) {
- case "1":
- addClient(sc);
- break;
- case "2":
- addProduct(sc);
- break;
- case "3":
- addOrder(sc);
- break;
- case "4":
- viewOrders();
- break;
- default:
- return;
- }
- }
- } finally {
- sc.close();
- if (conn != null) conn.close();
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- return;
- }
- }
- private static void initDB() throws SQLException {
- Statement st = conn.createStatement();
- try {
- st.execute("DROP TABLE IF EXISTS Clients");
- st.execute("DROP TABLE IF EXISTS Products");
- st.execute("DROP TABLE IF EXISTS Orders");
- st.execute("CREATE TABLE Products(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," +
- "name VARCHAR(16)NOT NULL, price INT) ");
- st.execute("CREATE TABLE Clients (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," +
- "name VARCHAR(16)NOT NULL, age INT)");
- st.execute("CREATE TABLE Orders (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," +
- " Pid INT NOT NULL , Cid INT NOT NULL, Info VARCHAR (128))");
- } finally {
- st.close();
- }
- }
- private static void addClient(Scanner sc) throws SQLException {
- System.out.print("Enter client name: ");
- String name = sc.nextLine();
- System.out.print("Enter client age: ");
- String sAge = sc.nextLine();
- int age = Integer.parseInt(sAge);
- PreparedStatement ps = conn.prepareStatement("INSERT INTO Clients (name, age) VALUES(?, ?)");
- try {
- ps.setString(1, name);
- ps.setInt(2, age);
- ps.executeUpdate();
- } finally {
- ps.close();
- }
- }
- private static void addProduct(Scanner sc) throws SQLException {
- System.out.print("Enter product name: ");
- String name = sc.nextLine();
- System.out.print("Enter price: ");
- String sPrice = sc.nextLine();
- int price = Integer.parseInt(sPrice);
- PreparedStatement ps = conn.prepareStatement("INSERT INTO Products (name, price)VALUES (?, ?)");
- try {
- ps.setString(1, name);
- ps.setInt(2, price);
- ps.executeUpdate();
- } finally {
- ps.close();
- }
- }
- private static void addOrder(Scanner sc) throws SQLException {
- System.out.print("Enter client name: ");
- String cname = sc.nextLine();
- System.out.print("Enter product name: ");
- String pname = sc.nextLine();
- System.out.print("Enter additional information:");
- String info = sc.nextLine();
- conn.setAutoCommit(false);
- try {
- try {
- PreparedStatement ps = conn.prepareStatement("INSERT INTO Orders " +
- "(Pid, Cid, Info) VALUES " +
- "((SELECT id FROM Products WHERE name = ?), " +
- "(SELECT id FROM Clients WHERE name = ?),?);");
- try {
- ps.setString(1, pname);
- ps.setString(2, cname);
- ps.setString(3, info);
- ps.executeUpdate();
- conn.commit();
- } finally {
- ps.close();
- }
- } catch (Exception ex) {
- conn.rollback();
- }
- } finally {
- conn.setAutoCommit(true);
- }
- }
- private static void viewOrders() throws SQLException {
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM Orders");
- try {
- ResultSet rs = ps.executeQuery();
- try {
- ResultSetMetaData md = rs.getMetaData();
- for (int i = 1; i <= md.getColumnCount(); i++)
- System.out.print(md.getColumnName(i) + "\t\t");
- System.out.println();
- while (rs.next()) {
- for (int i = 1; i <= md.getColumnCount(); i++) {
- System.out.print(rs.getString(i) + "\t\t");
- }
- System.out.println();
- }
- } finally {
- rs.close();
- }
- } finally {
- ps.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement