Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import com.mysql.jdbc.Connection;
  2. import com.mysql.jdbc.Statement;
  3.  
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8.  
  9. /**
  10. * Created by User on 12.06.2016.
  11. */
  12. public class Main {
  13.  
  14. public static void main(String[] args) throws ClassNotFoundException, Exception {
  15. createTable();
  16. createFlat();
  17. searchByStreatAndPrice();
  18. searchByRegion();
  19.  
  20. }
  21. private static Connection getDBConnection(){
  22. Connection dbConnection = null;
  23. try {
  24. Class.forName("com.mysql.jdbc.Driver");
  25. } catch (ClassNotFoundException e) {
  26. System.out.println(e.getMessage());
  27. }
  28. try {
  29. dbConnection = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/flats", "root","304FF76v");
  30. return dbConnection;
  31. } catch (Exception e) {
  32. System.out.println(e.getMessage());
  33. }
  34. return dbConnection;
  35. }
  36. public static void createTable() throws Exception {
  37. Connection dbConnection = null;
  38. Statement statement = null;
  39.  
  40. String sqlCreateTable = "create table flats (id int primary key auto_increment, streat varchar(100) not null, number varchar(7) not null, region varchar(100) not null, area int not null, room int not null, price int not null)";
  41.  
  42. try {
  43. dbConnection = getDBConnection();
  44.  
  45. statement = (Statement) dbConnection.createStatement();
  46. statement.execute(sqlCreateTable);
  47. System.out.println("Create table flats");
  48. }catch (SQLException e){
  49. System.out.println(e);
  50. }finally {
  51. if (dbConnection != null){
  52. dbConnection.close();
  53. }
  54. if (statement != null){
  55. statement.close();
  56. }
  57. }
  58. }
  59. public static void createFlat() throws SQLException {
  60. Connection dbConnection = null;
  61. Statement statement = null;
  62. String sqlCreateFlat = "insert flats set streat = 'Kitaevskaua', number = '3', region = 'Goloseevskiy', area = '50', room = '4', price = '4700' ";
  63.  
  64. try {
  65. dbConnection = getDBConnection();
  66. statement = (Statement) dbConnection.createStatement();
  67. statement.execute(sqlCreateFlat);
  68. System.out.println("Create row in flats");
  69. }catch (SQLException e){
  70. System.out.println(e);
  71. }finally {
  72. if (dbConnection != null){
  73. dbConnection.close();
  74. }
  75. if (statement != null){
  76. statement.close();
  77. }
  78. }
  79. }
  80. public static void searchByStreatAndPrice() throws SQLException {
  81. Connection dbConnection = null;
  82. Statement statement = null;
  83.  
  84. String sqlSearch = "select * from flats where streat = 'Nauki' and price = '4700'";
  85.  
  86. dbConnection = getDBConnection();
  87. statement = (Statement) dbConnection.createStatement();
  88. ResultSet query = statement.executeQuery(sqlSearch);
  89. while (query.next()){
  90. String streat = query.getString("streat");
  91. String number = query.getString("number");
  92. String region = query.getString("region");
  93. String area = query.getString("area");
  94. String room = query.getString("room");
  95. String price = query.getString("price");
  96. //print
  97. System.out.println("--> Streat " + streat
  98. + " Number " + number
  99. + " Region " + region
  100. + " Area " + area
  101. + " Rooms " + room
  102. + " Price " + price);
  103.  
  104. }
  105.  
  106.  
  107. }
  108. public static void searchByRegion() throws SQLException {
  109. Connection dbConnection = null;
  110. Statement statement = null;
  111.  
  112. String sqlSearch = "select * from flats where region = 'Goloseevskiy' ";
  113.  
  114. dbConnection = getDBConnection();
  115. statement = (Statement) dbConnection.createStatement();
  116. ResultSet query = statement.executeQuery(sqlSearch);
  117. while (query.next()){
  118. String streat = query.getString("streat");
  119. String number = query.getString("number");
  120. String region = query.getString("region");
  121. String area = query.getString("area");
  122. String room = query.getString("room");
  123. String price = query.getString("price");
  124. //print
  125. System.out.println("--> Streat " + streat
  126. + " Number " + number
  127. + " Region " + region
  128. + " Area " + area
  129. + " Rooms " + room
  130. + " Price " + price);
  131.  
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement