Last active
April 24, 2017 10:58
-
-
Save 13andrew13/55d5e77e7f5aa1de2376b979c7e05a9c 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
<component name="libraryTable"> | |
<library name="Maven: mysql:mysql-connector-java:5.1.35"> | |
<CLASSES> | |
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35.jar!/" /> | |
</CLASSES> | |
<JAVADOC> | |
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35-javadoc.jar!/" /> | |
</JAVADOC> | |
<SOURCES> | |
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35-sources.jar!/" /> | |
</SOURCES> | |
</library> | |
</component> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.company</groupId> | |
<artifactId>mvnTest</artifactId> | |
<packaging>jar</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<name>test-app</name> | |
<url>http://maven.apache.org</url> | |
<dependencies> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<version>5.1.35</version> | |
</dependency> | |
</dependencies> | |
</project> |
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
import com.sun.org.apache.xpath.internal.SourceTree; | |
import java.sql.*; | |
import java.util.Scanner; | |
/** | |
* Created by andrew on 21.04.17. | |
*/ | |
public class Main { | |
static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/flats"; | |
static final String DB_USER = "root"; | |
static final String DB_PASSWORD = "9707anton"; | |
static Connection conn; | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
try { | |
conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); | |
initDB(); | |
while (true) { | |
System.out.println("1: to add flat"); | |
System.out.println("2: to watch all flats"); | |
System.out.println("3: to see which has selected number of rooms"); | |
System.out.println("4: to see flats which prices are lower than"); | |
System.out.println("5: to see flats which price is more than"); | |
System.out.println("6: to see flats which are in such district"); | |
System.out.print("->"); | |
int x = scanner.nextInt(); | |
switch (x) { | |
case 1: | |
addFlat(); | |
break; | |
case 2: | |
viewAllFlats(); | |
break; | |
case 3: | |
withRooms(); | |
break; | |
case 4: | |
priceLower(); | |
break; | |
case 5: | |
priceHigher(); | |
break; | |
case 6: | |
districtFlats(); | |
break; | |
default: | |
return; | |
} | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void districtFlats() throws SQLException { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Print price over which you want to see flats:"); | |
String x = scanner.next(); | |
getFlats("SELECT * FROM Flats Where district ==\"" + x + "\";"); | |
} | |
private static void priceHigher() throws SQLException { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Print price over which you want to see flats:"); | |
float x = scanner.nextFloat(); | |
getFlats("SELECT * FROM Flats Where price>=" + x + ";"); | |
} | |
private static void priceLower() throws SQLException { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Print price under which you want to see flats:"); | |
float x = scanner.nextFloat(); | |
getFlats("SELECT * FROM Flats Where price<=" + x + ";"); | |
} | |
private static void withRooms() throws SQLException { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Print number of rooms:"); | |
int num = scanner.nextInt(); | |
getFlats("SELECT * FROM Flats WHERE rooms=" + num + ";"); | |
} | |
private static void viewAllFlats() throws SQLException { | |
getFlats("SELECT * FROM Flats"); | |
} | |
private static void getFlats(String statement) throws SQLException { | |
PreparedStatement ps = conn.prepareStatement(statement); | |
try { | |
ResultSet res = ps.executeQuery(); | |
try { | |
ResultSetMetaData mt = res.getMetaData(); | |
for (int i = 1; i <= mt.getColumnCount(); i++) { | |
System.out.print(mt.getColumnName(i) + "\t\t"); | |
} | |
System.out.println(); | |
while (res.next()) { | |
System.out.print(res.getInt(1) + "\t\t"); | |
System.out.print(res.getString(2) + "\t\t\t"); | |
System.out.print(res.getString(3) + "\t\t"); | |
System.out.print(res.getInt(4) + "\t\t"); | |
System.out.print(res.getFloat(5) + "\t\t"); | |
System.out.print(res.getFloat(6) + "\t\t"); | |
System.out.println(); | |
} | |
} finally { | |
res.close(); | |
} | |
} finally { | |
ps.close(); | |
} | |
} | |
private static void initDB() throws SQLException { | |
Statement st = conn.createStatement(); | |
try { | |
st.execute("DROP TABLE IF EXISTS Flats;"); | |
st.execute("CREATE TABLE Flats (id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,district VARCHAR(255),address VARCHAR(255),rooms INT ,square FLOAT,price FLOAT );"); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
if (st != null) | |
st.close(); | |
} | |
} | |
private static void addFlat() throws SQLException { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Print district:"); | |
String district = scanner.next(); | |
System.out.println("Print address:"); | |
String address = scanner.next(); | |
System.out.println("Print number of rooms:"); | |
int rooms = scanner.nextInt(); | |
System.out.println("Print square:"); | |
float square = scanner.nextFloat(); | |
System.out.println("Print price:"); | |
float price = scanner.nextFloat(); | |
PreparedStatement ps = conn.prepareStatement("INSERT INTO Flats(district,address,rooms,square,price) VALUES (?,?,?,?,?);"); | |
try { | |
ps.setString(1, district); | |
ps.setString(2, address); | |
ps.setInt(3, rooms); | |
ps.setFloat(4, square); | |
ps.setFloat(5, price); | |
ps.executeUpdate(); | |
} finally { | |
ps.close(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment