Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class Appartments {
  4. private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  5. private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb";
  6. private static final String DB_USER = "root";
  7. private static final String DB_PASSWORD = "";
  8.  
  9. private static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS Appartments (" +
  10. "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
  11. "region VARCHAR(100) NOT NULL, " +
  12. "address VARCHAR(100) NOT NULL, " +
  13. "space INT(10) NOT NULL, " +
  14. "rooms INT(10) NOT NULL, " +
  15. "price INT(10) NOT NULL)";
  16.  
  17. public static void main(String[] args) {
  18. Connection conn = getDBConnection();
  19. if (conn == null) {
  20. System.out.println("Error creating connection!");
  21. return;
  22. }
  23.  
  24. try {
  25. Statement st = conn.createStatement();
  26. st.execute(CREATE_TABLE_SQL);
  27. //fillDb(st, conn);
  28. selectDb(st, conn, "*", "appartments", "price", "2000", "4000");
  29. selectDb(st, conn, "address", "appartments", "rooms='2'");
  30. selectDb(st, conn, "id", "appartments");
  31. sortDb(st, conn, "price");
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. private static void sortDb(Statement st, Connection conn, String sort) throws SQLException {
  38. st = conn.createStatement();
  39. try {
  40. ResultSet rs = st.executeQuery("SELECT * FROM appartments ORDER BY "+sort+";");
  41. ResultSetMetaData md = rs.getMetaData();
  42.  
  43. for (int i = 1; i <= md.getColumnCount(); i++)
  44. System.out.print(md.getColumnName(i) + "\t\t");
  45. System.out.println();
  46.  
  47. while (rs.next()) {
  48. for (int i = 1; i <= md.getColumnCount(); i++)
  49. System.out.print(rs.getString(i) + "\t\t");
  50. System.out.println();
  51. }
  52. } finally {
  53. if (st != null) st.close();
  54. }
  55. }
  56.  
  57. private static void selectDb(Statement st, Connection conn, String... array) throws SQLException {
  58. String arr[] = array;
  59. st = conn.createStatement();
  60. try {
  61. ResultSet rs;
  62. if(arr.length == 5)
  63. rs = st.executeQuery("SELECT "+arr[0]+" FROM "+arr[1]+" WHERE " +arr[2]+
  64. " BETWEEN "+Integer.parseInt(arr[3])+" AND "+Integer.parseInt(arr[4])+";");
  65. else if(arr.length == 3)
  66. rs = st.executeQuery("SELECT "+arr[0]+" FROM "+arr[1]+" WHERE " +arr[2]+";");
  67. else
  68. rs = st.executeQuery("SELECT "+arr[0]+" FROM "+arr[1]+";");
  69.  
  70. ResultSetMetaData md = rs.getMetaData();
  71.  
  72. for (int i = 1; i <= md.getColumnCount(); i++)
  73. System.out.print(md.getColumnName(i) + "\t\t");
  74. System.out.println();
  75.  
  76. while (rs.next()) {
  77. for (int i = 1; i <= md.getColumnCount(); i++)
  78. System.out.print(rs.getString(i) + "\t\t");
  79. System.out.println();
  80. }
  81. } finally {
  82. if (st != null) st.close();
  83. }
  84. }
  85.  
  86. private static void fillDb(Statement st, Connection conn) throws SQLException {
  87. try {
  88. st = conn.createStatement();
  89. st.executeUpdate("INSERT INTO Appartments (region, address, space, rooms, price) " +
  90. "VALUES ('Darnitsa', 'Grirorenko 15/7', '55', '2', '4000');");
  91. st.executeUpdate("INSERT INTO Appartments (region, address, space, rooms, price) " +
  92. "VALUES ('Darnitsa', 'Ahmatova 156/88', '27', '1', '2000');");
  93. st.executeUpdate("INSERT INTO Appartments (region, address, space, rooms, price) " +
  94. "VALUES ('Darnitsa', 'Ahmatova 1a/77', '28', '1', '3000');");
  95. st.executeUpdate("INSERT INTO Appartments (region, address, space, rooms, price) " +
  96. "VALUES ('Darnitsa', 'Bazana 13/3', '40', '2', '5000');");
  97. st.executeUpdate("INSERT INTO Appartments (region, address, space, rooms, price) " +
  98. "VALUES ('Darnitsa', 'Bazana 15/99', '60', '3', '55000');");
  99. st.executeUpdate("INSERT INTO Appartments (region, address, space, rooms, price) " +
  100. "VALUES ('Darnitsa', 'Bazana 21/123', '67', '3', '5300');");
  101. } catch (SQLException ex) {
  102. ex.printStackTrace();
  103. } finally {
  104. if (st != null) st.close();
  105. }
  106. }
  107.  
  108. private static Connection getDBConnection() {
  109. Connection dbConnection = null;
  110. try {
  111. Class.forName(DB_DRIVER);
  112. } catch (ClassNotFoundException e) {
  113. e.printStackTrace();
  114. return null;}
  115.  
  116. try {
  117. dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  118. } catch (SQLException e) {
  119. e.printStackTrace();
  120. return null;}
  121. return dbConnection;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement