1. 程式人生 > 實用技巧 >jdbc使用筆記(一)Statement介面

jdbc使用筆記(一)Statement介面

jdbc使用筆記(一)Statement介面


  • maven依賴
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>6.0.6</version>
    </dependency>

  • 使用
package com.jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class App {

    // 建立資料庫使用
    final static String JDBC_URL_iNIT = "jdbc:mysql://192.168.1.5:3306/?useSSL=false&useUnicode=true&characterEncoding=UTF-8";

    // 操作資料表使用
    final static String JDBC_URL = "jdbc:mysql://192.168.1.5:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8";

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        createDB();
        createTable();
        insertTable();
        queryTable();
    }

    //建立資料庫
    public static void createDB() throws ClassNotFoundException, SQLException {
        // 裝載mysql驅動
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = null;
        Statement statement = null;
        //  建立名為JDBC的資料庫
        try {
            //  連線資料庫
            connection = DriverManager.getConnection(JDBC_URL_iNIT,"root","root");
            statement = connection.createStatement();
            boolean bool = statement.execute("CREATE DATABASE IF NOT EXISTS jdbc DEFAULT CHARSET utf8 COLLATE utf8_general_ci");
            System.out.println(bool);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(connection != null)connection.close();
            if(statement != null)statement.close();
        }
    }

    //建立資料表
    public static void createTable() throws ClassNotFoundException, SQLException {
        // 裝載mysql驅動
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = null;
        Statement statement = null;
        //  建立user表
        try {
            //  連線資料庫
            connection = DriverManager.getConnection(JDBC_URL,"root","root");
            statement = connection.createStatement();
            boolean bool = statement.execute("CREATE TABLE `user`  (\n" +
                    "  `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,\n" +
                    "  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,\n" +
                    "  `age` int(11) NULL DEFAULT NULL,\n" +
                    "  `id` int(11) NOT NULL AUTO_INCREMENT,\n" +
                    "  PRIMARY KEY (`id`) USING BTREE\n" +
                    ") ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic");
            System.out.println(bool);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(connection != null)connection.close();
            if(statement != null)statement.close();
        }
    }

    //插入(更新\刪除)資料
    public static void insertTable() throws ClassNotFoundException, SQLException {
        // 裝載mysql驅動
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = null;
        Statement statement = null;
        try {
            //  連線資料庫
            connection = DriverManager.getConnection(JDBC_URL,"root","root");
            statement = connection.createStatement();
            statement.executeUpdate("INSERT INTO user(user_name, password, age ) VALUES ('雷OK', 'XM', 200 )",Statement.RETURN_GENERATED_KEYS);
            statement.executeUpdate("INSERT INTO user(user_name, password, age ) VALUES (\"李What's\", 'BD', 300 )",Statement.RETURN_GENERATED_KEYS);
            statement.executeUpdate("INSERT INTO user(user_name, password, age ) VALUES ('馬氪金', 'TX', 100 )",Statement.RETURN_GENERATED_KEYS);
            statement.executeUpdate("INSERT INTO user(user_name, password, age ) VALUES ('馬買買', 'ALBB', 500 )",Statement.RETURN_GENERATED_KEYS);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(connection != null)connection.close();
            if(statement != null)statement.close();
        }
    }

    //查詢資料
    public static void queryTable() throws ClassNotFoundException, SQLException {
        // 裝載mysql驅動
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            //  連線資料庫
            connection = DriverManager.getConnection(JDBC_URL,"root","root");
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select id,user_name,password,age from user");
            //4.輸出查詢結果
            while (resultSet.next()){
                System.out.println(
                        "id=" + resultSet.getString("id") +
                        " user_name=" + resultSet.getString("user_name") +
                        " password=" + resultSet.getString("password") +
                        " age=" + resultSet.getString("age")
                );
            }
           } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(connection != null)connection.close();
            if(statement != null)statement.close();
            if(resultSet != null)resultSet.close();
        }
    }
}