package cn.reghao.tnb.sb.db.mybatis;

import java.sql.*;

/**
 * @author reghao
 * @date 2023-04-13 16:35:00
 */
public class JdbcDemo {
    static String driver = "com.mysql.cj.jdbc.Driver";
    static String url = "jdbc:mysql://localhost:3306/reghao_atest_rdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
    static String username = "dev";
    static String password = "Dev@123456";

    static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    }

    static Statement getStatement() throws SQLException, ClassNotFoundException {
        Connection conn = getConnection();
        Statement stmt = conn.createStatement();
        return stmt;
    }

    static void update() {
    }

    static void delete() {
    }

    static void insert1() throws SQLException, ClassNotFoundException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);

        // 使用占位符定义 sql 语句
        String sql = "INSERT INTO emp(empno,ename,job,sal,hiredate,mgr,comm,deptno) VALUES(?,?,?,?,?,?,?,?)";

        // 使用连接对象取得发送 sql 语句的对象 (PreparedStatement 接口对象)
        PreparedStatement pstmt = conn.prepareStatement(sql);

        // 设置占位符内容
        /*pstmt.setInt(1, emp.getEmpno());
        pstmt.setString(2,emp.getEname());
        pstmt.setString(3,emp.getJob());
        pstmt.setDouble(4,emp.getSal());
        pstmt.setDate(5,new java.sql.Date(emp.getHiredate().getTime()));
        pstmt.setInt(6,emp.getMgr());
        pstmt.setDouble(7,emp.getComm());
        pstmt.setInt(8,emp.getDeptno());*/

        // 执行 sql 语句
        int ret = pstmt.executeUpdate();

        //rs.close();
        pstmt.close();
        conn.close();
    }

    static void select0() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        Statement stmt = conn.createStatement();

        String sql = "select * from user limit 100";
        sql = "select * from user where id = 1";
        sql = "select * from user where id = ?";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            String url = rs.getString("username");
            System.out.printf("%s: %s%n\n", id, url);
        }

        rs.close();
        stmt.close();
        conn.close();
    }

    static void select2() throws SQLException, ClassNotFoundException {
        Statement stmt = getStatement();
        String sql = "select * from user limit 100";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            String url = rs.getString("username");
            System.out.printf("%s: %s%n\n", id, url);
        }

        rs.close();
        stmt.close();
    }

    static void select3() throws SQLException, ClassNotFoundException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        Statement stmt = conn.createStatement();

        String sql = "select * from user limit 100";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            String url = rs.getString("username");
            System.out.printf("%s: %s%n\n", id, url);
        }

        rs.close();
        stmt.close();
        conn.close();
    }

    static void insert() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        Statement stmt = conn.createStatement();

        String sql = "select * from user limit 100";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            String url = rs.getString("username");
            System.out.printf("%s: %s%n\n", id, url);
        }

        rs.close();
        stmt.close();
        conn.close();
    }

    static void select() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        Statement stmt = conn.createStatement();

        String sql = "select * from file_meta limit 100";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            String url = rs.getString("object_name");
            System.out.printf("%s: %s%n\n", id, url);
        }

        rs.close();
        stmt.close();
        conn.close();
    }

    static void select1() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);

        String sql = "select * from user limit 100";
        sql = "select * from file_meta where id=?";
        //sql = "select * from ? where id=10";

        PreparedStatement pstmt = conn.prepareStatement(sql);
        int id0 = 10;
        pstmt.setInt(1, id0);

        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            // 获取字段的 JDBC 类型
            String jdbcType = rs.getMetaData().getColumnTypeName(1);
            // 获取字段的 Java 类型
            String javaType = rs.getMetaData().getColumnClassName(1);

            int id = rs.getInt("id");
            String url = rs.getString("object_name");
            System.out.printf("%s: %s%n\n", id, url);
        }

        rs.close();
        pstmt.close();
        conn.close();
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        select1();
    }
}
