Java实现关系型数据库工具类JdbcUtils系列一 :JDBC连接关系型数据库

Java实现关系型数据库工具类JdbcUtils系列一 :JDBC连接关系型数据库

  • 一、JDBC体系结构
  • 二、Java获取数据库连接方法1
  • 三、Java获取数据库连接方法2
  • 四、Java最常用获取数据库连接的方法
  • 五、将数据库信息放到配置文件中
  • 六、获取连接工具类

一、JDBC体系结构

JDBC接口(API)包括两个层次:

  • 面向应用的API:Java API,抽象接口,供应用程序开发人员使用(连接数据库,执行SQL语句,获得结果)。
  • 面向数据库的API:Java Driver API,供开发商开发数据库驱动程序用。

二、Java获取数据库连接方法1

import org.junit.Test;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

public class DriverTest {

    /**
     * 获取mysql连接
     */
    @Test
    public void test() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {

        Driver driver = null;

        //1.获取驱动
        String driverClassName = "com.mysql.cj.jdbc.Driver";

        Class clazz = Class.forName(driverClassName);
        driver =(Driver) clazz.newInstance();

        //2.获取连接
        String url = "jdbc:mysql://10.10.128.100:3306/dw";
        Properties props = new Properties();
        props.setProperty("user","root");
        props.setProperty("password","nio123456!A");
        Connection conn = driver.connect(url,props);
        System.out.println(conn);
        conn.close();
    }
}

三、Java获取数据库连接方法2

public class DriverTest {

    @Test
    public void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //1.注册驱动
        String driverClassName = "com.mysql.cj.jdbc.Driver";
        Class clazz = Class.forName(driverClassName);
        Driver driver = (Driver) clazz.newInstance();
        DriverManager.registerDriver(driver);

        //2.获取连接
        String url = "jdbc:mysql://10.10.128.100:3306/dw";
        String user = "root";
        String password = "nio123456!A";
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        conn.close();
    }
}

输出如下所示:
在这里插入图片描述

四、Java最常用获取数据库连接的方法

import org.junit.Test;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DriverTest {

    @Test
    public  void  test3() throws ClassNotFoundException, SQLException {
        String driverClassName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://10.10.128.100:3306/dw";
        String user = "root";
        String password = "nio123456!A";

        //1.加载驱动
        Class.forName(driverClassName);

        //2.获取连接
        Connection conn = DriverManager.getConnection(url, user, password);

        System.out.println(conn);
    }
}

五、将数据库信息放到配置文件中

application.properties

driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.10.128.100:3306/dw?useUnicode=true&characterEncoding=utf-8
username: root
password: nio123456!A
import org.junit.Test;

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

public class DriverTest {

    @Test
    public void test4() throws IOException, ClassNotFoundException, SQLException {
        Properties props = new Properties();
        props.load(this.getClass().getClassLoader().getResourceAsStream("application.properties"));


        String driverClassName = props.getProperty("driverClassName");
        String url = props.getProperty("url");
        String user = props.getProperty("username");
        String password = props.getProperty("password");

        Class.forName(driverClassName);

        //2.获取连接
        Connection conn = DriverManager.getConnection(url, user, password);

        System.out.println(conn);

    }
}
  • Class是当前类的Class对象,Class.getClassLoader()是获取当前类的类加载器。
  • getResourceAsStream()是获取资源的输入流。类加载器默认是从classPath路径加载资源。
  • 当使用Class.getClassLoader.getResourceAsStream()加载资源时,是从classPath路径下进行加载,放在resources下的文件加载时不能加(“/”)。

六、获取连接工具类

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

public class jdbcUtils {

    /**
     * 获取连接
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
        Properties props = new Properties();
        props.load(JdbcUtils.class.getClassLoader().getResourceAsStream("application" +
                ".properties"));

        String driverClassName = props.getProperty("driverClassName");
        String url = props.getProperty("url");
        String user = props.getProperty("username");
        String password = props.getProperty("password");

        Class.forName(driverClassName);

        //2.获取连接
        Connection conn = DriverManager.getConnection(url, user, password);

        return conn;
    }
}

你可能感兴趣的:(日常分享专栏,Java实现关系型数据库工具类,JdbcUtils,JDBC连接关系型数据库)