Last active
February 28, 2016 18:21
-
-
Save Viacheslav77/377a585efb5840c4a85e to your computer and use it in GitHub Desktop.
Спроектировать базу «Квартиры». Каждая запись в базе содержит данные о квартире (район, адрес, площадь, кол. комнат, цена). Сделать возможность выборки квартир из списка по параметрам.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Kvartiry; | |
//Спроектировать базу «Квартиры». Каждая запись в базе содержит данные о квартире (район, | |
//адрес, площадь, кол. комнат, цена). Сделать возможность выборки квартир из списка по | |
//параметрам. | |
import java.sql.*; | |
import java.util.Random; | |
public class Main { | |
private static final String DB_DRIVER = "com.mysql.jdbc.Driver"; | |
private static final String DB_CONNECTION = "jdbc:myflats://localhost:3306/DB_Flats"; | |
private static final String DB_USER = "root"; | |
private static final String DB_PASSWORD = ""; | |
private static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS Flats (\n" + | |
" `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,\n" + | |
" `house_number` VARCHAR(45) NULL,\n" + | |
" `flat_number` VARCHAR(45) NULL,\n" + | |
" `street` VARCHAR(45) NULL,\n" + | |
" `city` VARCHAR(45) NULL,\n" + | |
" `postcode` VARCHAR(100) NULL,\n" + | |
" `area` DECIMAL(10,2) NULL,\n" + | |
" `number_of_rooms` INT(3) NOT NULL,\n" + | |
" `price` DECIMAL(18,2) NULL)"; | |
private static Connection getDBConnection() { | |
Connection dbConnection = null; | |
try { | |
Class.forName(DB_DRIVER); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
try { | |
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
Statement st = null; | |
try { | |
st = dbConnection.createStatement(); | |
st.execute(CREATE_TABLE_SQL); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
return null; | |
} finally { | |
if (st != null) { | |
try { | |
st.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return dbConnection; | |
} | |
public static void main(String[] args) { | |
Connection conn = getDBConnection(); | |
if (conn == null) { | |
return; | |
} | |
// inserting data | |
PreparedStatement ps = null; | |
try { | |
ps = conn.prepareStatement("INSERT INTO Apartments (house_number,flat_number,street,city,postcode,area,number_of_rooms,price) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"); | |
for (int i = 0; i < 10; i++) { | |
String [] street = { "New","Old", "Vasiliya","Petra","Zavodskay","Stroiteley"}; | |
String [] city = { "Kiev", "Vasilkov","New York", "London"}; | |
Random r = new Random (); | |
ps.setString(1, "house_number" + r.nextInt(10)); | |
ps.setString(2, "flat_number" + r.nextInt(10)); | |
ps.setString(3, street[r.nextInt(5)]); | |
ps.setString(4, city[r.nextInt(4)]); | |
ps.setString(5, "postcode" + i); | |
ps.setDouble(6, r.nextInt(10) * 10); | |
ps.setInt(7, r.nextInt(10)); | |
ps.setFloat(8, r.nextInt(10) * 100); | |
ps.executeUpdate(); | |
} | |
} catch (SQLException e){ | |
e.printStackTrace(); | |
} finally { | |
if (ps != null) { | |
try { | |
ps.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
try { | |
ps = conn.prepareStatement("SELECT * FROM DB_Flats.Flats where area > 33 and street like 'Stroiteley'"); | |
ResultSet rs = ps.executeQuery(); | |
ResultSetMetaData md = rs.getMetaData(); // | |
for (int i = 1; i <= md.getColumnCount(); i++) | |
System.out.print(md.getColumnName(i) + "\t\t"); | |
System.out.println(); | |
while (rs.next()) { | |
for (int i = 1; i <= md.getColumnCount(); i++) { | |
System.out.print(rs.getString(i) + "\t\t"); | |
} | |
System.out.println(); | |
} | |
} catch (SQLException e){ | |
e.printStackTrace(); | |
} finally { | |
if (ps != null) { | |
try { | |
ps.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
try { | |
conn.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment