Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. /**
  5. * Created by 1 on 13.03.2017.
  6. */
  7. public class Main {
  8. static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/flat";
  9. static final String DB_USER = "root";
  10. static final String DB_PASSWORD = "123";
  11. static Connection conn;
  12.  
  13. public static void main(String[] args) {
  14. int ch = 0;
  15. Scanner sc = new Scanner(System.in);
  16. try {
  17. conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  18. //initDB();
  19. while (true) {
  20. System.out.println("Choose: 1. Area | 2. id | 3. Rooms | 4. Price | 5. Square");
  21. ch=sc.nextInt();
  22. switch (ch) {
  23. case 1: chooseArea(sc); break;
  24. case 2: chooseId(sc); break;
  25. case 3: chooseRooms(sc); break;
  26. case 4: choosePrice(sc); break;
  27. case 5: chooseS(sc); break;
  28. default:
  29. System.out.println("Incorrect number");
  30. }
  31. }
  32. } catch (SQLException ex) {
  33. ex.printStackTrace();
  34. return;
  35. }
  36.  
  37. }
  38.  
  39. public static void chooseArea(Scanner sc) throws SQLException {
  40. String q = null;
  41. System.out.println("Enter the name of area for selection");
  42. q = sc.next();
  43. PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE Area = ?");
  44. ps.setString(1,q);
  45. try{
  46. ResultSet rs = ps.executeQuery();
  47. try{
  48. ResultSetMetaData ms = rs.getMetaData();
  49. for (int i = 1; i <= ms.getColumnCount(); i++) {
  50. System.out.println(ms.getColumnName(i) + "\t");
  51. }
  52. System.out.println();
  53. while (rs.next()) {
  54. for (int i = 1; i <= ms.getColumnCount(); i++) {
  55. System.out.println(rs.getString(i) + "\t");
  56. }
  57. System.out.println();
  58. }
  59. }finally {
  60. rs.close();
  61. }
  62. }finally {
  63. ps.close();
  64. }
  65. }
  66.  
  67. public static void chooseId(Scanner sc) throws SQLException {
  68. String q = null;
  69. System.out.println("Enter the id of flat for selection");
  70. q = sc.next();
  71. PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE idFlats = ?");
  72. ps.setInt(1,Integer.parseInt(q));
  73. try{
  74. ResultSet rs = ps.executeQuery();
  75. try{
  76. ResultSetMetaData ms = rs.getMetaData();
  77. for (int i = 1; i <= ms.getColumnCount(); i++) {
  78. System.out.println(ms.getColumnName(i) + "\t");
  79. }
  80. System.out.println();
  81. while (rs.next()) {
  82. for (int i = 1; i <= ms.getColumnCount(); i++) {
  83. System.out.println(rs.getString(i) + "\t");
  84. }
  85. System.out.println();
  86. }
  87. }finally {
  88. rs.close();
  89. }
  90. }finally {
  91. ps.close();
  92. }
  93. }
  94.  
  95. public static void chooseRooms(Scanner sc) throws SQLException {
  96. String q = null;
  97. System.out.println("Enter the number of rooms for selection");
  98. q = sc.next();
  99. PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE Rooms = ?");
  100. ps.setInt(1,Integer.parseInt(q));
  101. try{
  102. ResultSet rs = ps.executeQuery();
  103. try{
  104. ResultSetMetaData ms = rs.getMetaData();
  105. for (int i = 1; i <= ms.getColumnCount(); i++) {
  106. System.out.println(ms.getColumnName(i) + "\t");
  107. }
  108. System.out.println();
  109. while (rs.next()) {
  110. for (int i = 1; i <= ms.getColumnCount(); i++) {
  111. System.out.println(rs.getString(i) + "\t");
  112. }
  113. System.out.println();
  114. }
  115. }finally {
  116. rs.close();
  117. }
  118. }finally {
  119. ps.close();
  120. }
  121. }
  122.  
  123. public static void choosePrice(Scanner sc) throws SQLException {
  124. String q = null;
  125. System.out.println("Enter the price for selection");
  126. q = sc.next();
  127. PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE Price >= ?");
  128. ps.setInt(1,Integer.parseInt(q));
  129. try{
  130. ResultSet rs = ps.executeQuery();
  131. try{
  132. ResultSetMetaData ms = rs.getMetaData();
  133. for (int i = 1; i <= ms.getColumnCount(); i++) {
  134. System.out.println(ms.getColumnName(i) + "\t");
  135. }
  136. System.out.println();
  137. while (rs.next()) {
  138. for (int i = 1; i <= ms.getColumnCount(); i++) {
  139. System.out.println(rs.getString(i) + "\t");
  140. }
  141. System.out.println();
  142. }
  143. }finally {
  144. rs.close();
  145. }
  146. }finally {
  147. ps.close();
  148. }
  149. }
  150.  
  151. public static void chooseS(Scanner sc) throws SQLException {
  152. String q = null;
  153. System.out.println("Enter the square for selection");
  154. q = sc.next();
  155. PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE S = ?");
  156. ps.setDouble(1,Double.parseDouble(q));
  157. try{
  158. ResultSet rs = ps.executeQuery();
  159. try{
  160. ResultSetMetaData ms = rs.getMetaData();
  161. for (int i = 1; i <= ms.getColumnCount(); i++) {
  162. System.out.println(ms.getColumnName(i) + "\t");
  163. }
  164. System.out.println();
  165. while (rs.next()) {
  166. for (int i = 1; i <= ms.getColumnCount(); i++) {
  167. System.out.println(rs.getString(i) + "\t");
  168. }
  169. System.out.println();
  170. }
  171. }finally {
  172. rs.close();
  173. }
  174. }finally {
  175. ps.close();
  176. }
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement