Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. package com.flats;
  2.  
  3. import java.sql.*;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * Created by Andrey on 07.02.2016.
  8. */
  9. public class Main {
  10. static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb";
  11. static final String DB_USER = "root";
  12. static final String DB_PASSWORD = "admin";
  13.  
  14. static Connection conn;
  15.  
  16. public static void main(String[] args) {
  17. Scanner scanner = new Scanner(System.in);
  18. try {
  19. try{
  20. conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  21. while (true){
  22. System.out.println("1: search flats by district");
  23. System.out.println("2: search flats by address");
  24. System.out.println("3: search flats by area");
  25. System.out.println("4: search flats by rooms quantity");
  26. System.out.println("5: search flats by prices");
  27. System.out.println("->");
  28. String command = scanner.nextLine();
  29. switch (command){
  30. case "1":
  31. searchByString(scanner, command);
  32. break;
  33. case "2":
  34. searchByString(scanner, command);
  35. break;
  36. case "3":
  37. searchByNumeric(scanner, command);
  38. break;
  39. case "4":
  40. searchByNumeric(scanner, command);
  41. break;
  42. case "5":
  43. searchByNumeric(scanner, command);
  44. break;
  45. default: return;
  46. }
  47. }
  48. }
  49. finally {
  50. conn.close();
  51. }
  52.  
  53. }
  54. catch (SQLException ex){
  55. ex.printStackTrace();
  56. return;
  57. }
  58. }
  59.  
  60. private static void searchByString(Scanner scanner, String option) {
  61. String startStatement = "SELECT * FROM Flats WHERE "+ (option.equals("1") ? "district " : "address ");
  62. String condition, endStatement;
  63. System.out.println("Select the search option:");
  64. System.out.println("1: exact match");
  65. System.out.println("2: starting from...");
  66. System.out.println("3: by entry");
  67. String select = scanner.nextLine();
  68. switch (select){
  69. case "1":
  70. System.out.println("Input exact "+(option.equals("1") ? " name of district:" : "address:"));
  71. condition = scanner.nextLine();
  72. endStatement = "= '"+condition+"'";
  73. break;
  74. case "2":
  75. System.out.println("Input starting of "+(option.equals("1") ? " name of district:" : "address:"));
  76. condition = scanner.nextLine();
  77. endStatement = "LIKE '"+condition+"%'";
  78. break;
  79. case "3":
  80. System.out.println("Input part of "+(option.equals("1") ? " name of district:" : "address:"));
  81. condition = scanner.nextLine();
  82. endStatement = "LIKE '%"+condition+"%'";
  83. break;
  84. default: return;
  85. }
  86. String statement = startStatement.concat(endStatement);
  87. try {
  88. showResult(statement);
  89. }
  90. catch (SQLException ex){
  91. ex.printStackTrace();
  92. }
  93. }
  94.  
  95. private static void searchByNumeric (Scanner scanner, String option) {
  96. String startStatement = "SELECT * FROM Flats WHERE "+ (option.equals("3") ? "square " : (option.equals("4") ? "rooms " : "price "));
  97. String condition, endStatement;
  98. System.out.println("Select the search option:");
  99. System.out.println("1: equals");
  100. System.out.println("2: more then");
  101. System.out.println("3: less then");
  102. System.out.println("4: between");
  103. String select = scanner.nextLine();
  104. switch (select){
  105. case "1":
  106. System.out.println("Input exact "+(option.equals("3") ? "area:" : (option.equals("4") ? "rooms:" : "price:")));
  107. condition = scanner.nextLine();
  108. endStatement = "= "+condition;
  109. break;
  110. case "2":
  111. System.out.println("More then ...");
  112. condition = scanner.nextLine();
  113. endStatement = " > "+condition;
  114. break;
  115. case "3":
  116. System.out.println("Less then...");
  117. condition = scanner.nextLine();
  118. endStatement = " < "+condition;
  119. break;
  120. case "4":
  121. System.out.println("More then...");
  122. condition = scanner.nextLine();
  123. System.out.println("and less then...");
  124. String condition2 = scanner.nextLine();
  125. endStatement = " BETWEEN "+condition+" AND "+condition2;
  126. break;
  127. default: return;
  128. }
  129. String statement = startStatement.concat(endStatement);
  130. try {
  131. showResult(statement);
  132. }
  133. catch (SQLException ex){
  134. ex.printStackTrace();
  135. }
  136. }
  137. static void showResult(String statement) throws SQLException{
  138. PreparedStatement ps = conn.prepareStatement(statement);
  139. try {
  140. ResultSet rs = ps.executeQuery();
  141. try {
  142. ResultSetMetaData md = rs.getMetaData();
  143. for (int i = 1; i <= md.getColumnCount(); i++) {
  144. System.out.print(md.getColumnName(i)+"\t\t");
  145. }
  146. System.out.println();
  147. while (rs.next()){
  148. for (int i = 1; i <= md.getColumnCount() ; i++) {
  149. System.out.print(rs.getString(i)+"\t\t");
  150. }
  151. System.out.println();
  152. }
  153. }
  154. finally {
  155. rs.close();
  156. }
  157.  
  158. }
  159. finally {
  160. ps.close();
  161. }
  162.  
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement