获取数据库连接方式

package learn_jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class learn_jdbc3 {
   public static void main(String[] args) throws Exception
  {
       //方式1
       Driver driver=new com.mysql.jdbc.Driver();
       String url="jdbc:mysql://localhost:3306/atguigudb";
       String password="123456";
       //将用户名和密码封装在Properties中
       Properties info=new Properties();
       info.setProperty("user","root");
       info.setProperty("password","123456");
        Connection connect = driver.connect(url, info);
       System.out.println(connect);

       //方式2:对方式1的迭代   :在如下的程序中不出现第三方的api,使得程序具有更好的可移植性
       // 1. 获取Driver实现类对象,使用反射
       Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
       Driver driver = (Driver) aClass.newInstance();
       //2. 提供要连接的数据库
       String url="jdbc:mysql://localhost:3306/atguigudb";
       String password="123456";
       //3. 将用户名和密码封装在Properties中
       Properties info=new Properties();
       info.setProperty("user","root");
       info.setProperty("password","123456");
       //4. 获取连接
       Connection connect = driver.connect(url, info);
       System.out.println(connect);

       //方式3:使用DriverManager替换Driver
       //1. 获取Driver实现类的对象
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
       2. 提供数据库连接的基本信息
       String url="jdbc:mysql://localhost:3306/atguigudb";
       String user="root";
      String password="123456";
       //注册驱动
       DriverManager.registerDriver(driver);
       //获取连接
       Connection connection = DriverManager.getConnection(url, user, password);
       System.out.println(connection);

       //方式4:
       //加载驱动
       //静态代码块什么时候执行:随着类的加载而执行
       /*
       因为在mysql中Driver实现类中,声明了操作
        */
       Class.forName("com.mysql.jdbc.Driver");
       String url="jdbc:mysql://localhost:3306/atguigudb";
       String user="root";
       String password="123456";
//可以调用 DriverManager 类的 getConnection() 方法建立到数据库的连接
        Connection connection = DriverManager.getConnection(url, user, password);
       System.out.println(connection);

       //方式5:将数据库需要的4个基本信息声明在配置文件中,通过读取配置文件的方式获取连接
       /*
       好处:实现了数据与代码的分离,实现了解耦
            如果需要修改配置文件信息,可以避免程序重新打包
        */
       //1.读取配置文件中四个基本信息
       InputStream resourceAsStream = learn_jdbc3.class.getClassLoader().
        getResourceAsStream("jdbc.properties");//类加载器加载
       Properties properties = new Properties();
       properties.load(resourceAsStream);
       String user = properties.getProperty("user");
       String password = properties.getProperty("password");
       String url = properties.getProperty("url");
       String driverClass = properties.getProperty("driverClass");
       //2. 加载驱动
       Class.forName(driverClass);
       //3. 获取连接
       Connection connection = DriverManager.getConnection(url, user, password);
       System.out.println(connection);
  }
}
posted @ 2022-08-24 09:36  zjw_rp  阅读(137)  评论(0编辑  收藏  举报