1. 程式人生 > 實用技巧 >JDBC | 第一章: 快速開始使用JDBC連線Mysql資料庫?

JDBC | 第一章: 快速開始使用JDBC連線Mysql資料庫?

開始使用基於java的JDBC技術來連線mysql進行msyql資料庫簡單的CRUD操作

下載對應mysql驅動包

這裡我建立maven專案基於maven下載

 <!--mysql 驅動-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.21</version>
    </dependency>
    <!---->

連線資料庫

/*
 * jdbc:mysql:  是指JDBC連線方式;
 *127.0.0.1:       是指你的本機地址;
 * 3306               SQL資料庫的埠號;
 * mysql              就是你要連線的資料庫的名字。
 * characterEncoding     指定資料庫連線的編碼方式
 * com.mysql.jdbc.Driver  註冊資料庫驅動 過時
 * com.mysql.cj.jdbc.Driver  新的資料庫驅動
 */
       public Connection getConnection() {

        //mysql 連線url
        String url = "jdbc:mysql://localhost:3306/kenx_test?characterEncoding=utf-8";
        //資料庫使用者名稱
        String userName = "root";
        //資料庫密碼
        String passWord = "root";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");  //註冊資料庫驅動
            Connection conn = DriverManager.getConnection(url, userName, passWord); //獲取資料連線
            System.out.println("資料庫連線成功");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

進行資料新增操作

 public int ins() {
        Connection connection = null;
        Statement statement = null;
        String sql = "";
        try {
            //獲取資料連線
            connection = getConnection();
            //獲取傳送sql指令執行sql物件
            statement = connection.createStatement();
            StringBuffer sbf = new StringBuffer("insert into user (id, name, age, email, manager_id, create_time)");
            String id = String.valueOf(System.currentTimeMillis());
            Timestamp dateTime = Timestamp.valueOf(DateUtil.now());

            sbf.append(" values (" + id + ",'kenx',24,'[email protected]',155065178165505,'" + dateTime + "')");
            sql = sbf.toString();
            System.out.println("執行sql" + sql);
            //執行成功返回1
            int success = statement.executeUpdate(sql);
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }finally {
            //執行完資料庫操作後記得關閉資料庫連線資源
            try{

                statement.close();
                connection.close();

            }catch (SQLException e){
                e.printStackTrace();
            }

        }

    }

進行資料庫的查詢操作

     public void select() {
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        String sql = "select * from user limit 0,1";
        try {
            //獲取資料連線
            connection = getConnection();
            //獲取傳送sql指令執行sql物件
            statement = connection.createStatement();
            //返回查詢結果集用於儲存資料庫查詢內容
            rs = statement.executeQuery(sql);
            //遍歷結果集拿到資料
            while (rs.next()) {
                System.out.println("id" + "\t" + rs.getString("id"));
                System.out.println("name" + "\t" + rs.getString("name"));
                System.out.println("age" + "\t" + rs.getString("age"));
                System.out.println("email" + "\t" + rs.getString("email"));
                System.out.println("manager_id" + "\t" + rs.getString("manager_id"));
                System.out.println("create_time" + "\t" + rs.getString("create_time"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //執行完資料庫操作後記得關閉資料庫連線資源
            try{
                rs.close();
                statement.close();
                connection.close();

            }catch (SQLException e){
                e.printStackTrace();
            }

        }
    }

進行資料庫更新操作

 public int update() {
        Connection connection = null;
        Statement statement = null;
        String sql = "update user\n" +
                "set manager_id =155065512602445\n" +
                "where id=1597890374878";
        try {
            //獲取資料連線
            connection = getConnection();
            //獲取傳送sql指令執行sql物件
            statement = connection.createStatement();
            //更新成功返回1
            int success = statement.executeUpdate(sql);
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;

        }finally {
            //執行完資料庫操作後記得關閉資料庫連線資源
            try{
                statement.close();
                connection.close();

            }catch (SQLException e){
                e.printStackTrace();
            }

        }

    }

進行資料庫刪除操作

    public int del(){
        Connection connection = null;
        Statement statement = null;
        String sql = "delete from user where id='1597890374878'";
        try {
            //獲取資料連線
            connection = getConnection();
            //獲取傳送sql指令執行sql物件
            statement = connection.createStatement();
            //刪除成功返回1
            int success = statement.executeUpdate(sql);
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;

        }finally {
            //執行完資料庫操作後記得關閉資料庫連線資源
            try{
                statement.close();
                connection.close();

            }catch (SQLException e){
                e.printStackTrace();
            }

        }
    }

完整專案案例
點選這裡 github