JDBC | 第一章: 快速開始使用JDBC鏈接Mysql數據庫之簡單CRUD

開始使用基於java的JDBC技術來鏈接mysql進行msyql數據庫簡單的CRUD操做java

下載對應mysql驅動包

這裏我建立maven項目基於maven下載mysql

<!--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,'17521197955@163.com',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();
            }

        }
    }

完整項目案例
點擊這裏 githubgit

相關文章
相關標籤/搜索