JDBC连接数据库的几种方法

本文介绍了三种JDBC连接数据库的方法:直接通过Driver接口、编写通用方法和使用DriverManager类。详细步骤包括创建Driver对象、读取配置文件、加载驱动及获取数据库连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、 最古老的方法(通过 Driver 接口直接连接数据库)
1.首先创建一个 Driver 实现类的对象

   Driver dirver = new com.mysql.jdbc.Driver();

2.准备连接数据库的基本信息:url、user、password

   String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
   Properties info = new Properties();
   info.put("user", "root");
   info.put("password", "123456");

3.调用 Driver 借口的 connect(url, info) 获取数据库连接

 Connection connection = dirver.connect(url, info);

实例代码:

       @Test
      public void testDriver() throws SQLException {
          // 1. 创建一个 Driver 实现类的对象
          Driver dirver = new com.mysql.jdbc.Driver();
  
          // 2. 准备链接数据库的基本信息:url, user, password
          String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
          Properties info = new Properties();
          info.put("user", "root"); 
          info.put("password", "123456");
  
          // 3. 调用 Driver 借口的 connect(url, info) 获取数据库连接
          Connection connection = dirver.connect(url, info);
          System.out.println(connection);
      }

二、编写一个通用的方法(通过Driver 接口实现)
1.定义数据库基本信息变量

 String driverClass = null;
 String jdbcUrl = null;
 String user = null;
 String password = null;

2.创建一个properties文件,用来存放数据库连接基本信息

    driver=com.mysql.jdbc.Driver
    jdbcUrl=jdbc:mysql://127.0.0.1:3306/java_jdbc
    user=root
    password=123456

3.读取类路径下的jdbc.properties 文件

 InputStream in = 
                getClass().getClassLoader().getResourceAsStream("jdbc.properties");
 Properties properties = new Properties();
 properties.load(in);

4.从properties文件获取数据库基本信息

 driverClass = properties.getProperty("driver");
 jdbcUrl = properties.getProperty("jdbcUrl");
 user = properties.getProperty("user");
 password = properties.getProperty("password");

5.通过反射创建 Driver 对象

Driver driver = (Driver) Class.forName(driverClass).newInstance();

6.准备用户名和密码,并连接数据库

 Properties info = new Properties();
 info.put("user", user);
 info.put("password", password);
 Connection connection = driver.connect(jdbcUrl, info);

实例代码:

public Connection getConnection() throws Exception {
        String driverClass = null;
        String jdbcUrl = null;
        String user = null;
        String password = null;
       // 读取类路径下的jdbc.properties 文件
       InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(in);

        driverClass = properties.getProperty("driver");
        jdbcUrl = properties.getProperty("jdbcUrl");
        user = properties.getProperty("user");
        password = properties.getProperty("password");
        // 通过反射创建 Driver 对象
        Driver driver = (Driver) Class.forName(driverClass).newInstance();
        Properties info = new Properties();
        info.put("user", user);
        info.put("password", password);
        
        // 通过 Driver 的 connect 方法连接数据库.
        Connection connection = driver.connect(jdbcUrl, info);
        return connection;
    }
     @Test
     public void testGetConnection() throws Exception {
            System.out.println(getConnection());
     }
     
        

三、通过DriverManager 驱动管理类获取数据库连接
1.准备数据库连接的四个字符串
1).创建 properties 对象

Properties properties = new Properties();

2).获取jdbc.properties 对应的输入流

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

3).加载对应的输入流

 properties.load(inputStream);

4).具体决定 user、password、url、driver四个字符串

String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
  1. 加载数据库驱动程序
Class.forName(driver);
  1. 通过 Drivermanager 的 getConnection() 方法获取数据库连接
return DriverManager.getConnection(jdbcUrl, user, password);

实例化代码

@Test
    public void testGetConnection23() throws Exception {
        System.out.println(getConnection2());
    }
    public Connection getConnection2() throws IOException, ClassNotFoundException, SQLException{
    Properties properties = new Properties();
     
     InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        
        properties.load(inputStream);
        String driver = properties.getProperty("driver");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        
        Class.forName(driver);
        
        return DriverManager.getConnection(jdbcUrl, user, password);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值