Java連接MySQL數據庫 JBDC接口實現增刪查改 mysql connector


1.准備MySQL驅動jar包

可以去官網下載 這里就不說了
 

2.把驅動jar包引入idea工程###

 
第一步 點擊idea右上角這個圖標

 

第二部 點擊加號 選擇java

 

第三部 選擇驅動jar地址 然后一直點擊ok

 
驅動jar就被導入external libraries了

 

3.增刪查改的代碼###

這里使用的表
create table EMP
(
EMP_ID int(10) not null auto_increment,
ENAME varchar(50),
GENDER int(2) comment '1男,2女',
HIREDATE date,
primary key (EMP_ID)
);

 

import java.sql.*;

public class JDBCDemo {
    //准備連接數據庫的四大要素 (驅動 url 用戶名 密碼)Driver是驅動jar包里的一個類 mydb是本地創建的一個數據庫 密碼root是自己改的,請輸入自己的密碼
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/mydb";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    //增加數據
    public static void insert(){
        //准備sql語句
        String sql = "INSERT into emp(emp_id, ename, gender, hiredate) VALUES(null,'zhangsan',1,'1999-12-12')";
        //這兩個對象是需要關閉的 先設為null 方便在finally塊中關閉
        Connection connection = null;
        Statement statement = null;

        try {
            //加載驅動
            Class.forName(DRIVER);
            //獲得數據庫的連接對象
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //獲得一個執行SQL的對象
            statement = connection.createStatement();
            //使用這個對象來執行SQL
            int i = statement.executeUpdate(sql);

            System.out.println("插入完畢"+i+"條");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(statement != null){
                    statement.close();
                }
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //改數據
    public static void update(){
        //准備sql更改語句
        String sql = "update emp t set t.ename = '李四',t.gender = 2, t.HIREDATE = '2019-12-12' where t.EMP_ID = 4";

        Connection connection = null;
        Statement statement = null;

        try {
            //加載驅動
            Class.forName(DRIVER);
            //獲得數據庫的連接對象
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //獲得一個執行SQL的對象
            statement = connection.createStatement();
            //使用這個對象來執行SQL
            int i = statement.executeUpdate(sql);

            System.out.println("修改完畢"+i+"條");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(statement != null){
                    statement.close();
                }
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //刪數據
    public static void delete(){
        //准備sql刪除語句
        String sql = "delete from emp where emp_id = 2;";

        Connection connection = null;
        Statement statement = null;

        try {
            //加載驅動
            Class.forName(DRIVER);
            //獲得數據庫的連接對象
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //獲得一個執行SQL的對象
            statement = connection.createStatement();
            //使用這個對象來執行SQL
            int i = statement.executeUpdate(sql);

            System.out.println("修改完畢"+i+"條");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(statement != null){
                    statement.close();
                }
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //查數據
    public static void select(){
        //准備sql查找語句
        String sql = "select * from emp";
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;

        try {
            //加載驅動
            Class.forName(DRIVER);
            //獲得數據庫的連接對象
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //獲得一個執行SQL的對象
            statement = connection.createStatement();
            //執行查詢的邏輯
            rs = statement.executeQuery(sql);
            //拿出數據
           while(rs.next()){
               //可以用數字代表列
               /*int empId = rs.getInt(1);
               String ename = rs.getString(2);
               int gender = rs.getInt(3);
               Date date = rs.getDate(4);*/
               //也可以用列的字段名
               int empId = rs.getInt("emp_id");
               String ename = rs.getString("ename");
               int gender = rs.getInt("gender");
               Date date = rs.getDate("hiredate");

               System.out.println(empId+"   "+ename+"   "+gender+"   "+date);
           }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null){
                    rs.close();
                }
                if(statement != null){
                    statement.close();
                }
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        //select示范
        select();
    }
}

 

4.查找運行結果###


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM