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.查找运行结果

猜你喜欢

转载自www.cnblogs.com/seviyan/p/11751510.html