Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package test;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Test {
- private static Connection getDBConnection() {
- Connection dbConnection = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- }
- try {
- //dbConnection = DriverManager.getConnection("jdbc:mysql://192.168.100.240:3306/organizer", "user","user");
- dbConnection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taxi", "root", "");
- return dbConnection;
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return dbConnection;
- }
- static void showResult(ResultSet res) throws SQLException {
- while (res.next()) {
- int i = 1;
- try {
- while (true) {
- System.out.print(res.getString(i) + " ");
- i++;
- }
- } catch (Exception e) {
- System.out.println("");
- }
- }
- }
- private static void createTable() throws SQLException {
- Connection dbConnection = null;
- Statement statement = null;
- String createTableSQL = "create table customers ("
- + "id int (10) AUTO_INCREMENT,"
- + "name varchar(15) NOT NULL,"
- + "data varchar(20) NOT NULL,"
- + "number varchar(10) NOT NULL,"
- + "PRIMARY KEY (id));";
- try {
- dbConnection = getDBConnection();
- statement = dbConnection.createStatement();
- statement.execute(createTableSQL);
- System.out.println("Table is created!");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- static void insert(String name, String data, String number) throws SQLException {
- Connection dbConnection = getDBConnection();
- Statement statement = null;
- try {
- statement = dbConnection.createStatement();
- String createSQL = "INSERT INTO customers (name, data, number) "
- + "VALUES('" + name + "','" + data + "','" + number + "');";
- statement.execute(createSQL);
- System.out.println("Insert successfully");
- } catch (SQLException ex) {
- System.out.println(ex);
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- static void delete(String id) throws SQLException {
- Connection dbConnection = getDBConnection();
- Statement statement = null;
- try {
- statement = dbConnection.createStatement();
- String createSQL = "DELETE FROM customers WHERE id='" + id + "';";
- statement.execute(createSQL);
- System.out.println("Deleted");
- } catch (SQLException ex) {
- System.out.println(ex);
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- static void select(String id) throws SQLException {
- Connection dbConnection = getDBConnection();
- Statement statement = null;
- try {
- statement = dbConnection.createStatement();
- String createSQL = "SELECT * FROM customers WHERE id='" + id + "';";
- ResultSet res = statement.executeQuery(createSQL);
- System.out.println("Successfully");
- showResult(res);
- } catch (SQLException ex) {
- System.out.println(ex);
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- static void selectAll() throws SQLException {
- Connection dbConnection = getDBConnection();
- Statement statement = null;
- try {
- statement = dbConnection.createStatement();
- String createSQL = "SELECT * FROM customers;";
- ResultSet res = statement.executeQuery(createSQL);
- System.out.println("Successfully");
- showResult(res);
- } catch (SQLException ex) {
- System.out.println(ex);
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- static void update(String id,String name, String data, String number) throws SQLException {
- Connection dbConnection = getDBConnection();
- Statement statement = null;
- try {
- statement = dbConnection.createStatement();
- String createSQL = "UPDATE customers SET name='"+name+"', data='"+data+"', number='"+number+"' where id='"+id+"';";
- statement.execute(createSQL);
- System.out.println("Update successfully");
- } catch (SQLException ex) {
- System.out.println(ex);
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- public static void main(String[] args) {
- try {
- //createTable();
- //insert("Васьок", "12/10/2016", "55372");
- //delete("2");
- //update("1","Василий", "12/10/2016", "55372");
- //select("1");
- //selectAll();
- } catch (Exception ex) {
- System.out.println(ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement