Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. package com.gmail.shvetsova2015.inna;
  2.  
  3. import java.sql.*;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7. static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb2";
  8. static final String DB_USER = "root";
  9. static final String DB_PASSWORD = "password";
  10.  
  11. static Connection conn;
  12.  
  13. public static void main(String[] args) {
  14. Scanner sc = new Scanner(System.in);
  15. try {
  16. try {
  17. conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  18. initDB();
  19. while (true) {
  20. System.out.println("1: view all flats");
  21. System.out.println("2: add flat");
  22. System.out.println("3: delete flat");
  23. System.out.println("4: change price");
  24. System.out.println("5: view flats according to roomcount");
  25. System.out.println("6: view flats according to price");
  26. System.out.println("7: view flats according to district");
  27. System.out.print("-> ");
  28.  
  29. String s = sc.nextLine();
  30. switch (s) {
  31. case "1":
  32. viewFlats(sc);
  33. break;
  34. case "2":
  35. addFlat(sc);
  36. break;
  37. case "3":
  38. deleteFlat(sc);
  39. break;
  40. case "4":
  41. changePrice(sc);
  42. break;
  43. case "5":
  44. viewFlatRoomCount(sc);
  45. break;
  46. case "6":
  47. viewFlatPrice(sc);
  48. break;
  49. case "7":
  50. viewFlatDistrict(sc);
  51. break;
  52. default:
  53. return;
  54. }
  55. }
  56. } finally {
  57. sc.close();
  58. if (conn != null) conn.close();
  59. }
  60. } catch (SQLException ex) {
  61. ex.printStackTrace();
  62. return;
  63. }
  64.  
  65. }
  66.  
  67. private static void initDB() throws SQLException {
  68. Statement st = conn.createStatement();
  69. try {
  70. st.execute("DROP TABLE IF EXISTS Flats");
  71. st.execute("CREATE TABLE flats (\n" +
  72. "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,\n" +
  73. "district CHAR(30) DEFAULT NULL,\n" +
  74. "street CHAR (30) DEFAULT NULL,\n" +
  75. "building INT(5) DEFAULT NULL,\n" +
  76. "flat INT(5) DEFAULT NULL,\n" +
  77. "square DOUBLE(6,2) DEFAULT NULL,\n" +
  78. "roomcount INT(2) DEFAULT NULL,\n" +
  79. "price DOUBLE(10,2) DEFAULT NULL\n" +
  80. ")");
  81. } finally {
  82. st.close();
  83. }
  84. }
  85.  
  86. private static void viewFlats(Scanner sc) throws SQLException {
  87. PreparedStatement ps = conn.prepareStatement("SELECT * FROM Flats");
  88. try {
  89. ResultSet rs = ps.executeQuery();
  90. print(rs);
  91. } finally {
  92. ps.close();
  93. }
  94. }
  95.  
  96. private static void addFlat(Scanner sc) throws SQLException {
  97. System.out.print("Enter district: ");
  98. String district = sc.nextLine();
  99. System.out.print("Enter street: ");
  100. String street = sc.nextLine();
  101. System.out.print("Enter building number: ");
  102. String sBuilding = sc.nextLine();
  103. int building = Integer.parseInt(sBuilding);
  104. System.out.print("Enter flat number: ");
  105. String sFlat = sc.nextLine();
  106. int flat = Integer.parseInt(sFlat);
  107. System.out.print("Enter square: ");
  108. String sSquare = sc.nextLine();
  109. double square = Double.parseDouble(sSquare);
  110. System.out.print("Enter room count: ");
  111. String sRoomCount = sc.nextLine();
  112. int roomcount = Integer.parseInt(sRoomCount);
  113. System.out.print("Enter price: ");
  114. String sPrice = sc.nextLine();
  115. double price = Double.parseDouble(sPrice);
  116.  
  117. PreparedStatement ps = conn.prepareStatement("INSERT INTO Flats (district, street, building, flat, square, roomcount, price) VALUES(?, ?, ?, ?, ?, ?, ?)");
  118. try {
  119. ps.setString(1, district);
  120. ps.setString(2, street);
  121. ps.setInt(3, building);
  122. ps.setInt(4, flat);
  123. ps.setDouble(5, square);
  124. ps.setInt(6, roomcount);
  125. ps.setDouble(7, price);
  126. ps.executeUpdate();
  127. } finally {
  128. ps.close();
  129. }
  130. }
  131.  
  132. private static void deleteFlat(Scanner sc) throws SQLException {
  133. System.out.println("Enter information about flat you want to delete:");
  134. System.out.print("Enter flat street: ");
  135. String street = sc.nextLine();
  136. System.out.print("Enter flat building: ");
  137. String sBuilding = sc.nextLine();
  138. int building = Integer.parseInt(sBuilding);
  139. System.out.print("Enter flat number: ");
  140. String sFlat = sc.nextLine();
  141. int flat = Integer.parseInt(sFlat);
  142.  
  143. PreparedStatement ps = conn.prepareStatement("DELETE FROM Flats WHERE street = ? AND building = ? AND flat = ?");
  144. try {
  145. ps.setString(1, street);
  146. ps.setInt(2, building);
  147. ps.setInt(3, flat);
  148. ps.executeUpdate();
  149. } finally {
  150. ps.close();
  151. }
  152. }
  153.  
  154. private static void changePrice(Scanner sc) throws SQLException { // change age
  155. System.out.println("Enter information about flat you want to change:");
  156. System.out.print("Enter flat street: ");
  157. String street = sc.nextLine();
  158. System.out.print("Enter flat building: ");
  159. String sBuilding = sc.nextLine();
  160. int building = Integer.parseInt(sBuilding);
  161. System.out.print("Enter flat number: ");
  162. String sFlat = sc.nextLine();
  163. int flat = Integer.parseInt(sFlat);
  164. System.out.print("Enter NEW price: ");
  165. String sPrice = sc.nextLine();
  166. int price = Integer.parseInt(sPrice);
  167.  
  168. PreparedStatement ps = conn.prepareStatement("UPDATE Flats SET price = ? WHERE street = ? AND building = ? AND flat = ?");
  169. try {
  170. ps.setInt(1, price);
  171. ps.setString(2, street);
  172. ps.setInt(3, building);
  173. ps.setInt(4, flat);
  174. ps.executeUpdate();
  175. } finally {
  176. ps.close();
  177. }
  178. }
  179.  
  180. private static void viewFlatRoomCount(Scanner sc) throws SQLException {
  181. System.out.print("Enter room count: ");
  182. String sRoomcount = sc.nextLine();
  183. int roomcount = Integer.parseInt(sRoomcount);
  184.  
  185. PreparedStatement ps = conn.prepareStatement("SELECT * FROM Flats WHERE roomcount = ?");
  186. try {
  187. ps.setInt(1, roomcount);
  188. ResultSet rs = ps.executeQuery();
  189. print(rs);
  190. } finally {
  191. ps.close();
  192. }
  193. }
  194.  
  195. private static void viewFlatPrice(Scanner sc) throws SQLException {
  196. System.out.println("Enter price: ");
  197. System.out.print("from: ");
  198. String sPrice1 = sc.nextLine();
  199. int price1 = Integer.parseInt(sPrice1);
  200. System.out.print("to: ");
  201. String sPrice2 = sc.nextLine();
  202. int price2 = Integer.parseInt(sPrice2);
  203.  
  204. PreparedStatement ps = conn.prepareStatement("SELECT * FROM Flats WHERE price > ? AND price < ?");
  205. try {
  206. ps.setInt(1, price1);
  207. ps.setInt(2, price2);
  208. ResultSet rs = ps.executeQuery();
  209. print(rs);
  210. } finally {
  211. ps.close();
  212. }
  213. }
  214.  
  215. private static void viewFlatDistrict(Scanner sc) throws SQLException {
  216. System.out.print("Enter district: ");
  217. String district = sc.nextLine();
  218.  
  219. PreparedStatement ps = conn.prepareStatement("SELECT * FROM Flats WHERE district = ?");
  220. try {
  221. ps.setString(1, district);
  222. ResultSet rs = ps.executeQuery();
  223. print(rs);
  224. } finally {
  225. ps.close();
  226. }
  227. }
  228.  
  229. private static void print(ResultSet rs) throws SQLException {
  230. try {
  231. ResultSetMetaData md = rs.getMetaData();
  232. for (int i = 1; i <= md.getColumnCount(); i++){
  233. System.out.print(md.getColumnName(i) + "\t\t");
  234. }
  235. System.out.println();
  236. while (rs.next()) {
  237. for (int i = 1; i <= md.getColumnCount(); i++) {
  238. System.out.print(rs.getString(i) + "\t\t");
  239. }
  240. System.out.println();
  241. }
  242. } finally {
  243. rs.close();
  244. }
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement