Advertisement
Konark

Laba 1 - CrossPlatform

Oct 26th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.42 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package test;
  7.  
  8. import java.sql.*;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. public class Test {
  16.  
  17.     private static Connection getDBConnection() {
  18.         Connection dbConnection = null;
  19.         try {
  20.             Class.forName("com.mysql.jdbc.Driver");
  21.         } catch (ClassNotFoundException e) {
  22.             System.out.println(e.getMessage());
  23.         }
  24.         try {
  25.             //dbConnection = DriverManager.getConnection("jdbc:mysql://192.168.100.240:3306/organizer", "user","user");
  26.             dbConnection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taxi", "root", "");
  27.             return dbConnection;
  28.         } catch (SQLException e) {
  29.             System.out.println(e.getMessage());
  30.         }
  31.         return dbConnection;
  32.     }
  33.  
  34.     static void showResult(ResultSet res) throws SQLException {
  35.         while (res.next()) {
  36.             int i = 1;
  37.             try {
  38.                 while (true) {
  39.                     System.out.print(res.getString(i) + " ");
  40.                     i++;
  41.                 }
  42.             } catch (Exception e) {
  43.                 System.out.println("");
  44.             }
  45.         }
  46.     }
  47.  
  48.     private static void createTable() throws SQLException {
  49.         Connection dbConnection = null;
  50.         Statement statement = null;
  51.  
  52.         String createTableSQL = "create table customers ("
  53.                 + "id int (10) AUTO_INCREMENT,"
  54.                 + "name varchar(15) NOT NULL,"
  55.                 + "data varchar(20) NOT NULL,"
  56.                 + "number varchar(10) NOT NULL,"
  57.                 + "PRIMARY KEY (id));";
  58.         try {
  59.             dbConnection = getDBConnection();
  60.             statement = dbConnection.createStatement();
  61.             statement.execute(createTableSQL);
  62.             System.out.println("Table is created!");
  63.         } catch (SQLException e) {
  64.             System.out.println(e.getMessage());
  65.         } finally {
  66.             if (statement != null) {
  67.                 statement.close();
  68.             }
  69.             if (dbConnection != null) {
  70.                 dbConnection.close();
  71.             }
  72.         }
  73.     }
  74.  
  75.     static void insert(String name, String data, String number) throws SQLException {
  76.         Connection dbConnection = getDBConnection();
  77.         Statement statement = null;
  78.         try {
  79.             statement = dbConnection.createStatement();
  80.             String createSQL = "INSERT INTO customers (name, data, number) "
  81.                     + "VALUES('" + name + "','" + data + "','" + number + "');";
  82.             statement.execute(createSQL);
  83.             System.out.println("Insert successfully");
  84.         } catch (SQLException ex) {
  85.             System.out.println(ex);
  86.         } finally {
  87.             if (statement != null) {
  88.                 statement.close();
  89.             }
  90.             if (dbConnection != null) {
  91.                 dbConnection.close();
  92.             }
  93.         }
  94.     }
  95.  
  96.     static void delete(String id) throws SQLException {
  97.         Connection dbConnection = getDBConnection();
  98.         Statement statement = null;
  99.         try {
  100.             statement = dbConnection.createStatement();
  101.             String createSQL = "DELETE FROM customers WHERE id='" + id + "';";
  102.             statement.execute(createSQL);
  103.             System.out.println("Deleted");
  104.         } catch (SQLException ex) {
  105.             System.out.println(ex);
  106.         } finally {
  107.             if (statement != null) {
  108.                 statement.close();
  109.             }
  110.             if (dbConnection != null) {
  111.                 dbConnection.close();
  112.             }
  113.         }
  114.     }
  115.  
  116.     static void select(String id) throws SQLException {
  117.         Connection dbConnection = getDBConnection();
  118.         Statement statement = null;
  119.         try {
  120.             statement = dbConnection.createStatement();
  121.             String createSQL = "SELECT * FROM customers WHERE id='" + id + "';";
  122.             ResultSet res = statement.executeQuery(createSQL);
  123.             System.out.println("Successfully");
  124.             showResult(res);
  125.         } catch (SQLException ex) {
  126.             System.out.println(ex);
  127.         } finally {
  128.             if (statement != null) {
  129.                 statement.close();
  130.             }
  131.             if (dbConnection != null) {
  132.                 dbConnection.close();
  133.             }
  134.         }
  135.     }
  136.  
  137.     static void selectAll() throws SQLException {
  138.         Connection dbConnection = getDBConnection();
  139.         Statement statement = null;
  140.         try {
  141.             statement = dbConnection.createStatement();
  142.             String createSQL = "SELECT * FROM customers;";
  143.             ResultSet res = statement.executeQuery(createSQL);
  144.             System.out.println("Successfully");
  145.             showResult(res);
  146.         } catch (SQLException ex) {
  147.             System.out.println(ex);
  148.         } finally {
  149.             if (statement != null) {
  150.                 statement.close();
  151.             }
  152.             if (dbConnection != null) {
  153.                 dbConnection.close();
  154.             }
  155.         }
  156.     }
  157.  
  158.     static void update(String id,String name, String data, String number) throws SQLException {
  159.         Connection dbConnection = getDBConnection();
  160.         Statement statement = null;
  161.         try {
  162.             statement = dbConnection.createStatement();
  163.             String createSQL = "UPDATE customers SET name='"+name+"', data='"+data+"', number='"+number+"' where id='"+id+"';";
  164.             statement.execute(createSQL);
  165.             System.out.println("Update successfully");
  166.         } catch (SQLException ex) {
  167.             System.out.println(ex);
  168.         } finally {
  169.             if (statement != null) {
  170.                 statement.close();
  171.             }
  172.             if (dbConnection != null) {
  173.                 dbConnection.close();
  174.             }
  175.         }
  176.     }
  177.  
  178.     public static void main(String[] args) {
  179.  
  180.         try {
  181.             //createTable();
  182.             //insert("Васьок", "12/10/2016", "55372");
  183.             //delete("2");
  184.             //update("1","Василий", "12/10/2016", "55372");
  185.             //select("1");
  186.             //selectAll();
  187.         } catch (Exception ex) {
  188.             System.out.println(ex);
  189.         }
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement