jdbc连接mysql数据库的5种常用方法

jdbc编程六步

8版本驱动:com.mysql.cj.jdbc.Driver

5版本驱动:com.mysql.jdbc.Driver

第一步:注册驱动(作用:告诉java程序,即将要连接的哪个数据库)

第二步:获取连接(表示jvm的进程和数据库进程之间的通道打开了,这属于进程之间的通信,使用完之后要关闭)

第三步:获取数据库操作对象(专门执行sql语句的对象)

第四步:执行SQL语句

第五步:处理查询结果集(只有执行的是select语句的时候,才有第五步处理查询结果集)

第六步:释放资源(使用完资源以后一定要关闭资源。java和数据库属于进程间的通信,开启之后一定要关闭)


连接数据库

连接数据库的准备导入jdbc的jar包:


//方式1 注册
public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
        Properties properties = new Properties();
        properties.setProperty("user","root");//用户
        properties.setProperty("password","yefengwei");
        Connection connection = driver.connect(url,properties);
        System.out.println("方式2="+connection);
    }
//方式2 使用DriverManager 替代 Diver 进行统一管理
public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //使用反射加载Driver
        Class aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
        String user = "root";
        String password = "yefengwei";
        DriverManager.registerDriver(driver);//注册Driver驱动
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("第三种方式="+connection);
    }
//方式3
public void connect04() throws ClassNotFoundException, SQLException {
        //使用反射加载了Driver类
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
        String user = "root";
        String password = "yefengwei";
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println("第四方式"+connection);
    }
//方式4:在方式3的基础上改进,增加配置文件,让连接mysql更加灵活
public void connect05() throws ClassNotFoundException, SQLException, IOException {
        //通过Properties对象获取配置文件信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Class.forName(driver);//建议加上,加载驱动
        /*forName
        try {//registerDriver()注册驱动
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }*/
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("方式5"+connection);
        //方式5 com.mysql.cj.jdbc.ConnectionImpl@292b08d6
    }

配置文件如下:

8版本:url=jdbc:mysql://localhost:3306/login数据库名称?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8

driver=com.mysql.cj.jdbc.Driver

5版本:url=jdbc:mysql://localhost:3306/数据库名称

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8
user=root
password=yefengwei
driver=com.mysql.cj.jdbc.Driver
//方式5,使用资源绑定器绑定属性配置文件
public void connect06(){
        //通过资源绑定器绑定属性配置文件
        //创建好要配置的文件(.properties文件)属性名(key)=属性值(value) ---属性值不能有分号、双引号
        ResourceBundle bundle = ResourceBundle.getBundle("mysql");//只需要输入文件名 .properties不用加,也不需要加路径 只需要写.properties的文件名
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
        Connection conn = null;
        Statement statement = null;
        try {
            //1.注册
            Class.forName(driver);
            //2.获取数据库连接
            conn = DriverManager.getConnection(url,user,password);
            //3.创建数据库操作对象
            statement = conn.createStatement();
            //4.执行sql语句
            String sql = "insert into user values(3,'广志','88545')";
            int count = statement.executeUpdate(sql);
            System.out.println("插入成功");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值