JAVA&JDBC的五种连接方式

以Mysql为例

方式一:静态连接 

public void connect01() throws SQLException {
    Driver driver = new Driver();//创建Driver对象
    String url = "jdbc:mysql://localhost:3306/java_test";
    Properties properties = new Properties();
    properties.setProperty("user", "root");//用户
    properties.setProperty("password", "123456");//密码
    Connection connect = driver.connect(url, properties);
    System.out.println(connect);
}

方式二:动态反射

public void connect02() 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/java_test";
    Properties properties = new Properties();
    properties.setProperty("user", "root");//用户
    properties.setProperty("password", "123456");//密码
    Connection connect = driver.connect(url, properties);
    System.out.println("第二种方式 = " + connect);
}

方式三:使用DriverManager 替代 Driver 进行统一管理

//首先还是使用反射加载Driver
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//创建url和user,密码
String url = "jdbc:mysql://localhost:3306/java_test";
String user = "root";
String password = "123456";

//注册Driver驱动
DriverManager.registerDriver(driver);

Connection connection = DriverManager.getConnection(url, user, password);
//传统建立连接的方式:
/*
Connection connection = driver.connect(url,user,password);
*/
System.out.println("第三种方式 = " + connection);

方式四:对方式三的优化

//使用反射加载了Driver类
/*
* 相较于第三种方式
* 在加载Driver类时,完成注册
* 源码分析:
* 1.静态代码块:类加载的时候执行1次
* 2.DriverManager.registerDriver(new Driver())
* 3.因此注册driver的工作已经完成
* static {
    try {
        DriverManager.registerDriver(new Driver());//此处合并了newInstance和registerDriver
    } catch (SQLException var1) {
         throw new RuntimeException("Can't register driver!");
    }
}
* */
Class.forName("com.mysql.cj.jdbc.Driver");//mysql5.1.6及以上可以无需这句话(指newInstance),因为META-INF中已经有了
​
//创建url和user,密码
String url = "jdbc:mysql://localhost:3306/java_test";
String user = "root";
String password = "123456";
​
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("第四种方式 = " + connection);

方式五:使用Properties动态获取url,user,password,更为灵活

//通过Properties对象获取配置文件信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
//一定要根据properties的配置文件key来获取对应的值
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");//注:此处getProperties获取的顺序可以打乱
​
//虽然这句话可以不写
//Class.forName("com.mysql.cj.jdbc.Driver");//mysql5.1.6及以上可以无需这句话,因为META-INF中已经有了
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式五: " + connection);
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值