Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;
- /**
- * Created by 1 on 13.03.2017.
- */
- public class Main {
- static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/flat";
- static final String DB_USER = "root";
- static final String DB_PASSWORD = "123";
- static Connection conn;
- public static void main(String[] args) {
- int ch = 0;
- Scanner sc = new Scanner(System.in);
- try {
- conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
- //initDB();
- while (true) {
- System.out.println("Choose: 1. Area | 2. id | 3. Rooms | 4. Price | 5. Square");
- ch=sc.nextInt();
- switch (ch) {
- case 1: chooseArea(sc); break;
- case 2: chooseId(sc); break;
- case 3: chooseRooms(sc); break;
- case 4: choosePrice(sc); break;
- case 5: chooseS(sc); break;
- default:
- System.out.println("Incorrect number");
- }
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- return;
- }
- }
- public static void chooseArea(Scanner sc) throws SQLException {
- String q = null;
- System.out.println("Enter the name of area for selection");
- q = sc.next();
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE Area = ?");
- ps.setString(1,q);
- try{
- ResultSet rs = ps.executeQuery();
- try{
- ResultSetMetaData ms = rs.getMetaData();
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(ms.getColumnName(i) + "\t");
- }
- System.out.println();
- while (rs.next()) {
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(rs.getString(i) + "\t");
- }
- System.out.println();
- }
- }finally {
- rs.close();
- }
- }finally {
- ps.close();
- }
- }
- public static void chooseId(Scanner sc) throws SQLException {
- String q = null;
- System.out.println("Enter the id of flat for selection");
- q = sc.next();
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE idFlats = ?");
- ps.setInt(1,Integer.parseInt(q));
- try{
- ResultSet rs = ps.executeQuery();
- try{
- ResultSetMetaData ms = rs.getMetaData();
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(ms.getColumnName(i) + "\t");
- }
- System.out.println();
- while (rs.next()) {
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(rs.getString(i) + "\t");
- }
- System.out.println();
- }
- }finally {
- rs.close();
- }
- }finally {
- ps.close();
- }
- }
- public static void chooseRooms(Scanner sc) throws SQLException {
- String q = null;
- System.out.println("Enter the number of rooms for selection");
- q = sc.next();
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE Rooms = ?");
- ps.setInt(1,Integer.parseInt(q));
- try{
- ResultSet rs = ps.executeQuery();
- try{
- ResultSetMetaData ms = rs.getMetaData();
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(ms.getColumnName(i) + "\t");
- }
- System.out.println();
- while (rs.next()) {
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(rs.getString(i) + "\t");
- }
- System.out.println();
- }
- }finally {
- rs.close();
- }
- }finally {
- ps.close();
- }
- }
- public static void choosePrice(Scanner sc) throws SQLException {
- String q = null;
- System.out.println("Enter the price for selection");
- q = sc.next();
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE Price >= ?");
- ps.setInt(1,Integer.parseInt(q));
- try{
- ResultSet rs = ps.executeQuery();
- try{
- ResultSetMetaData ms = rs.getMetaData();
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(ms.getColumnName(i) + "\t");
- }
- System.out.println();
- while (rs.next()) {
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(rs.getString(i) + "\t");
- }
- System.out.println();
- }
- }finally {
- rs.close();
- }
- }finally {
- ps.close();
- }
- }
- public static void chooseS(Scanner sc) throws SQLException {
- String q = null;
- System.out.println("Enter the square for selection");
- q = sc.next();
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM flats WHERE S = ?");
- ps.setDouble(1,Double.parseDouble(q));
- try{
- ResultSet rs = ps.executeQuery();
- try{
- ResultSetMetaData ms = rs.getMetaData();
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(ms.getColumnName(i) + "\t");
- }
- System.out.println();
- while (rs.next()) {
- for (int i = 1; i <= ms.getColumnCount(); i++) {
- System.out.println(rs.getString(i) + "\t");
- }
- System.out.println();
- }
- }finally {
- rs.close();
- }
- }finally {
- ps.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement