JDBC连接

  • 加载驱动
  • 创建连接(url,用户名,密码)
  • prepareStatement预编译
  • 执行sql

一、最简单的方法(方法)

    @Test
    public void test() {
        Connection connect = null;
        try{
            // 1、通过反射获取驱动实例
            Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
            Driver driver = (Driver)clazz.newInstance();
            String url = "jdbc:mysql://localhost:3306/funddb?useSSL=false&serverTimezone=UTC";
            Properties properties = new Properties();
            properties.setProperty("user", "leo-zu");
            properties.setProperty("password", "123456");
            // 2、获取数据库连接
            connect = driver.connect(url, properties);
            System.out.println(connect);
            // 3、使用prepareStatement,具有预编译,防sql注入等功能
            String sql = "select * from fund_role";
            PreparedStatement prepareStatement = connect.prepareStatement(sql);
            ResultSet resultSet = prepareStatement.executeQuery();
            if (resultSet.next()){
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getString("role_name"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (connect != null){
                try {
                    connect.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }

二、第二种方法

使用DriverManager获取连接,通过发射加载driver的静态代码块,会自动创建驱动并注册

    @Test
    public void test2() {
        Connection connect = null;
        try{
            // 1、通过反射加载驱动时,driver静态代码块会注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/funddb?useSSL=false&serverTimezone=UTC";
            Properties properties = new Properties();
            properties.setProperty("user", "leo-zu");
            properties.setProperty("password", "123456");
            // 2、使用DriverManager获取数据库连接
            connect = DriverManager.getConnection(url, properties);
            // 或者直接connect = DriverManager.getConnection(url, "leo-zu", "123456");
            // 3、使用prepareStatement,具有预编译,防sql注入等功能
            String sql = "select * from fund_role";
            PreparedStatement prepareStatement = connect.prepareStatement(sql);
            ResultSet resultSet = prepareStatement.executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getString("role_name"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (connect != null){
                try {
                    connect.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }

三、使用配置文件

    @Test
    public void test3() {
        Connection connect = null;
        try{
            // 1、加载配置文件
            InputStream inputStream = JdbcTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driverClass = properties.getProperty("driverClass");

            // 2、通过反射加载驱动时,driver静态代码块会注册驱动
            Class.forName(driverClass);
            
            // 3、使用DriverManager获取数据库连接
            connect = DriverManager.getConnection(url, user, password);

            // 4、使用prepareStatement,具有预编译,防sql注入等功能
            String sql = "select * from fund_role";
            PreparedStatement prepareStatement = connect.prepareStatement(sql);
            ResultSet resultSet = prepareStatement.executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getString("role_name"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (connect != null){
                try {
                    connect.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }
user=用户名
password=密码
url=jdbc:mysql://localhost:3306/数据库?useSSL=false&serverTimezone=UTC
driverClass=com.mysql.cj.jdbc.Driver
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值