Advertisement
Guest User

Untitled

a guest
Jun 11th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Main {
  6. static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/Flats";
  7. static final String DB_USER = "root";
  8. static final String DB_PASSWORD = "root";
  9.  
  10. static Connection conn;
  11.  
  12. public static void main(String[] args) {
  13. Scanner sc = new Scanner(System.in);
  14. try {
  15. try {
  16. conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  17. initDB();
  18.  
  19. while (true) {
  20. System.out.println("1: add appartment");
  21. System.out.println("2: filter appartments by area and rooms count");
  22. System.out.println("3: delete appartment");
  23. System.out.println("4: change appartment");
  24. System.out.println("5: view appartments");
  25. System.out.print("-> ");
  26.  
  27. String s = sc.nextLine();
  28. switch (s) {
  29. case "1":
  30. addAppartments();
  31. break;
  32. case "2":
  33. filterAppartmentsByAreaAndRooms();
  34. break;
  35. case "3":
  36. deleteAppartment();
  37. break;
  38. case "4":
  39. changeAppartment();
  40. break;
  41. case "5":
  42. viewAllAppartments();
  43. break;
  44. default:
  45. return;
  46. }
  47. }
  48. } finally {
  49. sc.close();
  50. if (conn != null) conn.close();
  51. }
  52. } catch (SQLException ex) {
  53. ex.printStackTrace();
  54. return;
  55. }
  56. }
  57.  
  58.  
  59. private static void initDB() throws SQLException {
  60. Statement st = conn.createStatement();
  61. try {
  62.  
  63. st.execute("DROP TABLE IF EXISTS Appartments");
  64. st.execute("DROP TABLE IF EXISTS Areas");
  65. st.execute("CREATE TABLE Areas (a_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  66. area VARCHAR(50) NOT NULL)");
  67. st.execute("CREATE TABLE Appartments (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  68. address VARCHAR(50) NOT NULL,
  69. square DOUBLE NOT NULL, rooms INT NOT NULL,
  70. price DOUBLE NOT NULL, area_id INT NOT NULL ,
  71. FOREIGN KEY (area_id) REFERENCES Areas(a_id))");
  72.  
  73. PreparedStatement ps = conn.prepareStatement("INSERT INTO Areas (area) VALUES(?)");
  74. try {
  75. ps.setString(1, "Goloseevo");
  76. ps.executeUpdate();
  77. ps.setString(1, "Svyatoshin");
  78. ps.executeUpdate();
  79. ps.setString(1, "Obolon'");
  80. ps.executeUpdate();
  81.  
  82. } finally {
  83. ps.close();
  84. }
  85. } finally {
  86. st.close();
  87. }
  88. }
  89.  
  90. private static void addAppartments() throws SQLException {
  91. Scanner sc = new Scanner(System.in);
  92. System.out.println("For adding new appartment enter address:");
  93. String address = sc.nextLine();
  94. System.out.println("Enter square:");
  95. double square = sc.nextDouble();
  96. System.out.println("Enter count of rooms:");
  97. int rooms = sc.nextInt();
  98. System.out.println("Enter price:");
  99. double price = sc.nextDouble();
  100. System.out.println("1->Goloseevo\n2->Svyatoshin\n3->Obolon'");
  101. System.out.println("Enter id of area:");
  102. int area_id = sc.nextInt();
  103.  
  104. PreparedStatement ps = conn.prepareStatement("INSERT INTO Appartments (address, square, rooms, price, area_id) VALUES(?, ?, ?, ?, ?)");
  105. try {
  106. ps.setString(1, address);
  107. ps.setDouble(2, square);
  108. ps.setInt(3, rooms);
  109. ps.setDouble(4, price);
  110. ps.setInt(5, area_id);
  111. ps.executeUpdate();
  112. } finally {
  113. ps.close();
  114. }
  115. }
  116.  
  117. private static void filterAppartmentsByAreaAndRooms() throws SQLException {
  118. Scanner sc = new Scanner(System.in);
  119. System.out.println("1->Goloseevo\n2->Svyatoshin\n3->Obolon'");
  120. System.out.println("Enter id of area:");
  121. int area_id = sc.nextInt();
  122. System.out.println("Enter count of rooms:");
  123. int rooms = sc.nextInt();
  124.  
  125. PreparedStatement ps = conn.prepareStatement("SELECT ar.area, app.address, app.square, app.rooms, app.price
  126. FROM Appartments app INNER JOIN Areas ar
  127. ON app.area_id=ar.a_id WHERE app.rooms=? AND ar.a_id=?");
  128. try {
  129. ps.setInt(1, rooms);
  130. ps.setInt(2, area_id);
  131. ps.execute();
  132.  
  133. ResultSet rs = ps.executeQuery();
  134. if (rs.getFetchSize() == 0) {
  135. System.out.println("There are no such appartments in this area. Try to change filter conditions!");
  136. } else {
  137. try {
  138. ResultSetMetaData md = rs.getMetaData();
  139.  
  140. for (int i = 1; i <= md.getColumnCount(); i++) {
  141. System.out.print(md.getColumnName(i) + "\t\t");
  142. }
  143. System.out.println();
  144.  
  145. while (rs.next()) {
  146. for (int i = 1; i <= md.getColumnCount(); i++) {
  147. System.out.print(rs.getString(i) + "\t\t");
  148. }
  149. System.out.println();
  150. }
  151. } finally {
  152. rs.close();
  153. }
  154. }
  155. } finally {
  156. ps.close();
  157. }
  158. }
  159.  
  160.  
  161. private static void deleteAppartment() throws SQLException {
  162. Scanner sc = new Scanner(System.in);
  163. System.out.println("Enter id of appartment you need to delete");
  164. int deleteId = sc.nextInt();
  165. PreparedStatement ps = conn.prepareStatement("DELETE FROM Appartments WHERE id =?");
  166. try {
  167. ps.setInt(1, deleteId);
  168. ps.executeUpdate();
  169. } finally {
  170. ps.close();
  171.  
  172. }
  173.  
  174. }
  175.  
  176. private static void changeAppartment() throws SQLException {
  177. Scanner sc = new Scanner(System.in);
  178. System.out.println("Enter id of appartment you need to change");
  179. int changeId = Integer.valueOf(sc.nextLine());
  180. System.out.println("Enter new address:");
  181. String address = sc.nextLine();
  182. System.out.println("Enter new square:");
  183. double square = sc.nextDouble();
  184. System.out.println("Enter new count of rooms:");
  185. int rooms = sc.nextInt();
  186. System.out.println("Enter new price:");
  187. double price = sc.nextDouble();
  188.  
  189. PreparedStatement ps = conn.prepareStatement("UPDATE Appartments SET address = ?, square= ?, rooms = ?, price = ? WHERE id=?");
  190. try {
  191. ps.setString(1, address);
  192. ps.setDouble(2, square);
  193. ps.setInt(3, rooms);
  194. ps.setDouble(4, price);
  195. ps.setInt(5, changeId);
  196. ps.executeUpdate();
  197. } finally {
  198. ps.close();
  199.  
  200. }
  201. }
  202.  
  203. private static void viewAllAppartments() throws SQLException {
  204. PreparedStatement ps = conn.prepareStatement("SELECT ar.area, app.address, app.square, app.rooms, app.price FROM Appartments app INNER JOIN Areas ar ON app.area_id=ar.a_id");
  205. try {
  206. ResultSet rs = ps.executeQuery();
  207. try {
  208. ResultSetMetaData md = rs.getMetaData();
  209.  
  210. for (int i = 1; i <= md.getColumnCount(); i++) {
  211. System.out.print(md.getColumnName(i) + "\t\t");
  212. }
  213. System.out.println();
  214.  
  215. while (rs.next()) {
  216. for (int i = 1; i <= md.getColumnCount(); i++) {
  217. System.out.print(rs.getString(i) + "\t\t");
  218. }
  219. System.out.println();
  220. }
  221. } finally {
  222. rs.close();
  223. }
  224. } finally {
  225. ps.close();
  226. }
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement