Guest User

Untitled

a guest
Dec 4th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. package db.test;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.Scanner;
  11.  
  12. public class DBTest {
  13.  
  14. static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mysql";
  15. static final String DB_USER = "root";
  16. static final String DB_PASSWORD = "root";
  17. static Connection conn;
  18.  
  19. private static void addApatament(String command) throws SQLException {
  20.  
  21. try (PreparedStatement ps = conn.prepareStatement(command)) {
  22.  
  23. ps.executeUpdate();
  24. }
  25. }
  26. //Формируем запрос на поиск нужной квартиры
  27. private static void viewRequest(String cm) throws SQLException {
  28. String command = "SELECT * FROM Apart WHERE square = " + cm;
  29. try (PreparedStatement ps = conn.prepareStatement(command);
  30. ResultSet rs = ps.executeQuery()) {
  31. ResultSetMetaData md = rs.getMetaData();
  32. System.out.println("Результат поиска:");
  33. for (int i = 1; i <= md.getColumnCount(); i++) {
  34. System.out.print(md.getColumnName(i) + "\t\t");
  35. }
  36. System.out.println();
  37. while (rs.next()) {
  38. for (int i = 1; i <= md.getColumnCount(); i++) {
  39. System.out.print(rs.getString(i) + "\t\t");
  40. }
  41. System.out.println();
  42.  
  43. }
  44. }
  45. }
  46.  
  47. private static void viewApatament() throws SQLException {
  48. try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM Apart");
  49. ResultSet rs = ps.executeQuery()) {
  50. ResultSetMetaData md = rs.getMetaData();
  51. System.out.println("Содержание БД:");
  52. for (int i = 1; i <= md.getColumnCount(); i++) {
  53. System.out.print(md.getColumnName(i) + "\t\t");
  54. }
  55. System.out.println();
  56. while (rs.next()) {
  57. for (int i = 1; i <= md.getColumnCount(); i++) {
  58. System.out.print(rs.getString(i) + "\t\t");
  59. }
  60. System.out.println();
  61. }
  62. }
  63. }
  64.  
  65. private static void initDB() throws SQLException {
  66. try (Statement st = conn.createStatement()) {
  67. st.execute("DROP TABLE IF EXISTS Apart");
  68. st.execute("CREATE TABLE Apart (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, region VARCHAR(20)"+
  69. "NOT NULL, adress VARCHAR(20) NOT NULL, square INT, quantity_room INT, price FLOAT)");
  70. }
  71. }
  72.  
  73. public static void main(String[] args) {
  74. Scanner sc = new Scanner(System.in);
  75. try {
  76. try {
  77. //Соеденение с БД
  78. conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  79. initDB(); //Создам и заплоняем БД
  80. addApatament("INSERT INTO Apart (region, adress, square, quantity_room, price) VALUES('Kiev', 'Obolon #17/4', 65, 4, 125600.00)");
  81. addApatament("INSERT INTO Apart (region, adress, square, quantity_room, price) VALUES('Odesa', 'Arkadia #25', 56, 3, 120600.00)");
  82. addApatament("INSERT INTO Apart (region, adress, square, quantity_room, price) VALUES('Lviv', 'Banders str. #4', 67, 3, 105600.00)");
  83. addApatament("INSERT INTO Apart (region, adress, square, quantity_room, price) VALUES('Dnepr', 'Parus #18', 60, 3, 128600.00)");
  84. viewApatament();
  85. viewRequest("65"); //Ищем квартиру полщадью 65 квадратных метров
  86. } finally {
  87. sc.close();
  88. if (conn != null) {
  89. conn.close();
  90. }
  91. }
  92. } catch (SQLException ex) {
  93. System.out.println(ex);
  94. }
  95. }
  96.  
  97. }
Add Comment
Please, Sign In to add comment