JDBC连接数据库的五种方式

  public  void conn1() throws SQLException {

        /*
        第三方的jar包,静态加载,灵活性差,依赖性强
         */
        //        获取驱动
        Driver driver = new Driver();
//        得到连接
        String url="jdbc:mysql://localhost:3306/test1";
//        将用户和密码封装成Properties文件
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
//        获得连接
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
//       得到SQL语句
        String sql="update emp set empname='周润发' where empno = 'E101'";
        Statement statement = connect.createStatement();
        statement.executeUpdate(sql);
//        关闭资源
        statement.close();
        connect.close();

    }

该方式采取第三方的jar包,静态加载,灵活性差,依赖性强

 public  void conn2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        /*
        使用反射机制动态加载Driver类,减少依赖性
         */
        //用配置文件保存"com.mysql.jdbc.Driver"
        Class aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url="jdbc:mysql://localhost:3306/test1";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
//        获得连接
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
//       得到SQL语句
        String sql="update emp set empname='周润发' where empno = 'E101'";
        Statement statement = connect.createStatement();
        statement.executeUpdate(sql);
//        关闭资源
        statement.close();
        connect.close();
    }

此方式使用反射机制动态加载Driver类,减少依赖性

 public void conn3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
            Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
            Driver  driver = (Driver) aClass.newInstance();
            String url="jdbc:mysql://localhost:3306/test1";
            String user="root";
            String password="123456";
//            注册driver驱动
            DriverManager.registerDriver(driver);
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);

        }

该方式采用Driver Manager的连接管理包对连接进行管理,有助于方便管理

        public void  conn4() throws ClassNotFoundException, SQLException {
        /*
        使用class.forname自动完成注册,简化代码
         */
//            使用反射机制
//            在加载Driver类时自动注册
           /* static {
                try {
                    DriverManager.registerDriver(new Driver());
                } catch (SQLException var1) {
                    throw new RuntimeException("Can't register driver!");
                }*/
//            底层原码静态代码块中自动注册了
            Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/test1";
            String user="root";
            String password="123456";
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
        }

这种方式采用Class.forName()自动注册,采用反射机制加载

   public void conn5() throws IOException, ClassNotFoundException, SQLException {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            Class.forName("jdbc:mysql://localhost:3306/test1");
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);

        }

该方式同上

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值