Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.util.Scanner;
  6.  
  7. /**
  8. * @author Ihor Solohub.
  9. * Создать проект «База данных заказов». Создать
  10. таблицы «Товары» , «Клиенты» и «Заказы».
  11. Написать код для добавления новых клиентов,
  12. товаров и оформления заказов.
  13. */
  14. public class Main {
  15. static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/OrdersDatabase";
  16. static final String DB_USER = "root";
  17. static final String DB_PASSWORD = "15935724865";
  18.  
  19. static Connection conn;
  20.  
  21. public static void main(String[] args) {
  22. Scanner sc = new Scanner(System.in);
  23. try {
  24. try {
  25. conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  26.  
  27. while (true) {
  28. System.out.println("1: add new Product");
  29. System.out.println("2: add new Client");
  30. System.out.println("3: create Order");
  31. System.out.print("->");
  32.  
  33. String choise = sc.nextLine();
  34. switch (choise) {
  35. case "1":
  36. DbUtils.addProduct();
  37. break;
  38. case "2":
  39. DbUtils.addClient(sc);
  40. break;
  41. case "3":
  42. DbUtils.createOrder();
  43. break;
  44. default:
  45. return;
  46. }
  47. }
  48. } finally {
  49. sc.close();
  50. }
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56. private static class DbUtils {
  57.  
  58. /*------- Add product -------*/
  59. private static void addProduct() throws SQLException {
  60. Scanner sc = new Scanner(System.in);
  61. System.out.println("Enter product name: ");
  62. String productName = sc.nextLine();
  63. System.out.println("Enter price: ");
  64. int productPrice = sc.nextInt();
  65.  
  66. PreparedStatement ps = conn.prepareStatement("INSERT INTO Products " +
  67. "(Name, Price) VALUES (?,?)");
  68. try {
  69. ps.setString(1, productName);
  70. ps.setInt(2, productPrice);
  71. ps.executeUpdate();
  72. } finally {
  73. ps.close();
  74. }
  75. }
  76.  
  77. /*------- Add client -------*/
  78. private static void addClient(Scanner scanner) throws SQLException {
  79. Scanner sc = new Scanner(System.in);
  80. System.out.println("Enter client's name: ");
  81. String clientsName = sc.nextLine();
  82. System.out.println("Enter phone: ");
  83. String clientsPhone = sc.nextLine();
  84.  
  85. PreparedStatement ps = conn.prepareStatement("INSERT INTO Clients " +
  86. "(Name, Phone) VALUES (?,?)");
  87. try {
  88. ps.setString(1, clientsName);
  89. ps.setString(2, clientsPhone);
  90. ps.executeUpdate();
  91. } finally {
  92. ps.close();
  93. }
  94. }
  95.  
  96. /*------- Create order -------*/
  97. private static void createOrder() throws SQLException {
  98. Scanner sc = new Scanner(System.in);
  99. System.out.println("Enter product name: ");
  100. String productName = sc.nextLine();
  101. System.out.println("Enter client's phone: ");
  102. String clientsPhone = sc.nextLine();
  103. String info = "New order";
  104. PreparedStatement ps = conn.prepareStatement("INSERT INTO Orders (Pid, Cid, Info) VALUES " +
  105. "((SELECT id FROM Products WHERE Name = ?), " +
  106. "(SELECT Id FROM Clients WHERE Phone = ?), \"New order\");");
  107. try {
  108. ps.setString(1, productName);
  109. ps.setString(2, clientsPhone);
  110. ps.executeUpdate();
  111. } finally {
  112. ps.close();
  113. }
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement