Advertisement
Guest User

Untitled

a guest
Jun 11th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5. static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/orders";
  6. static final String DB_USER = "root";
  7. static final String DB_PASSWORD = "root";
  8.  
  9. static Connection conn;
  10.  
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. try {
  14. try {
  15. conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  16. initDB();
  17.  
  18. while (true) {
  19. System.out.println("1: add new product");
  20. System.out.println("2: make an order");
  21. System.out.println("3: view orders");
  22. System.out.print("-> ");
  23.  
  24. String s = sc.nextLine();
  25. switch (s) {
  26. case "1":
  27. addProduct();
  28. break;
  29. case "2":
  30. makeAnOrder();
  31. break;
  32. case "3":
  33. viewOrders();
  34. break;
  35. default:
  36. return;
  37. }
  38. }
  39. } finally {
  40. sc.close();
  41. if (conn != null) conn.close();
  42. }
  43. } catch (SQLException ex) {
  44. ex.printStackTrace();
  45. return;
  46. }
  47. }
  48.  
  49.  
  50. private static void initDB() throws SQLException {
  51. Statement st = conn.createStatement();
  52. try {
  53. st.execute("DROP TABLE IF EXISTS Orders");
  54. st.execute("DROP TABLE IF EXISTS Clients");
  55. st.execute("DROP TABLE IF EXISTS Products");
  56. st.execute("CREATE TABLE Clients (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  57. username VARCHAR(50) NOT NULL, email VARCHAR(30),
  58. phone VARCHAR (20) NOT NULL UNIQUE) ");
  59. st.execute("CREATE TABLE Products (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  60. name VARCHAR(50) NOT NULL,
  61. price DOUBLE NOT NULL)");
  62. st.execute("CREATE TABLE Orders(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  63. client_id INT,
  64. product_id INT)");
  65. } finally {
  66. st.close();
  67. }
  68. }
  69.  
  70. private static void addProduct() throws SQLException {
  71. Scanner sc = new Scanner(System.in);
  72. System.out.println("Enter product name:");
  73. String name = sc.nextLine();
  74. System.out.println("Enter product price:");
  75. double price = sc.nextDouble();
  76.  
  77. PreparedStatement ps = conn.prepareStatement("INSERT INTO Products (name,price) VALUES(?, ?)");
  78. try {
  79. ps.setString(1, name);
  80. ps.setDouble(2, price);
  81. ps.executeUpdate();
  82. } finally {
  83. ps.close();
  84. }
  85. }
  86.  
  87. private static void makeAnOrder() throws SQLException {
  88. Scanner sc = new Scanner(System.in);
  89. System.out.println("Enter client's name:");
  90. String username = sc.nextLine();
  91. System.out.println("Enter client's email:");
  92. String email = sc.nextLine();
  93. System.out.println("Enter client's phone:");
  94. String phone = sc.nextLine();
  95. viewAllProducts();
  96. System.out.println("Choose a product. Enter product's id:");
  97. int productID = sc.nextInt();
  98.  
  99. int usernameID = findUserID(username, email, phone);
  100. addOrder(usernameID, productID);
  101. }
  102.  
  103.  
  104. private static void addClient(String username, String email, String phone) throws SQLException {
  105. PreparedStatement ps = conn.prepareStatement("INSERT INTO Clients (username, email, phone) VALUES(?, ?, ?)");
  106. try {
  107. ps.setString(1, username);
  108. ps.setString(2, email);
  109. ps.setString(3, phone);
  110. ps.executeUpdate();
  111. } finally {
  112. ps.close();
  113. }
  114.  
  115. }
  116.  
  117. private static int findUserID(String username, String email, String phone) throws SQLException {
  118. PreparedStatement ps = conn.prepareStatement("SELECT id FROM Clients WHERE phone = ?");
  119. ResultSet rs = null;
  120. int userID = 0;
  121. try {
  122. ps.setString(1, phone);
  123. rs = ps.executeQuery();
  124.  
  125. while (true) {
  126. if (rs.next()) {
  127. userID = Integer.parseInt(rs.getString("id"));
  128. break;
  129. } else {
  130. addClient(username, email, phone);
  131. rs = ps.executeQuery();
  132. }
  133. }
  134.  
  135. } finally
  136.  
  137. {
  138. rs.close();
  139. ps.close();
  140. }
  141.  
  142. return userID;
  143. }
  144.  
  145. private static void viewAllProducts() throws SQLException {
  146. PreparedStatement ps = conn.prepareStatement("SELECT * FROM Products");
  147. printTable(ps);
  148. }
  149.  
  150. private static void addOrder(int usernameID, int productID) throws SQLException {
  151. PreparedStatement ps = conn.prepareStatement("INSERT INTO Orders (client_id, product_id) VALUES(?, ?)");
  152.  
  153. try {
  154. ps.setInt(1, usernameID);
  155. ps.setInt(2, productID);
  156. ps.executeUpdate();
  157. } finally {
  158. ps.close();
  159. }
  160. }
  161.  
  162. private static void viewOrders() throws SQLException {
  163. PreparedStatement ps = conn.prepareStatement("SELECT p.name,p.price, cl.username, cl.email, cl.phone FROM Products p, Orders ord, Clients cl WHERE cl.id = ord.client_id AND p.id = ord.product_id");
  164. printTable(ps);
  165.  
  166. }
  167.  
  168. private static void printTable(PreparedStatement ps) throws SQLException {
  169. try {
  170. ResultSet rs = ps.executeQuery();
  171. try {
  172. ResultSetMetaData md = rs.getMetaData();
  173.  
  174. for (int i = 1; i <= md.getColumnCount(); i++) {
  175. System.out.print(md.getColumnName(i) + "\t\t");
  176. }
  177. System.out.println();
  178.  
  179. while (rs.next()) {
  180. for (int i = 1; i <= md.getColumnCount(); i++) {
  181. System.out.print(rs.getString(i) + "\t\t");
  182. }
  183. System.out.println();
  184. }
  185. } finally {
  186. rs.close();
  187. }
  188. } finally {
  189. ps.close();
  190.  
  191. }
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement