11、JDBC-Druid

依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>yofc</groupId>
    <artifactId>jdbc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.14</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 指定jdk -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

获取连接

无配置文件

@Test
public void testDruid() {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;

    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://192.168.8.136:3306/jdbc");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    try {
        conn = dataSource.getConnection();
        pstmt = conn.prepareStatement("select * from user");
        rset = pstmt.executeQuery();
        while (rset.next()) {
            System.out.println(rset.getInt("id") + "   " + rset.getString("name"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (rset != null) rset.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (Exception e) {
        }
    }
}

有配置文件

druid.properties

username=root
password=root
url=jdbc:mysql://192.168.8.136:3306/jdbc
driverClassName=com.mysql.cj.jdbc.Driver
@Test
public void testDruidWithConfig() {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;

    try {
        Properties properties = new Properties();
        properties.load(this.getClass().getResourceAsStream("druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        conn = dataSource.getConnection();
        pstmt = conn.prepareStatement("select * from user");
        rset = pstmt.executeQuery();
        while (rset.next()) {
            System.out.println(rset.getInt("id") + "   " + rset.getString("name"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (rset != null) rset.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (Exception e) {
        }
    }
}

官方文档

猜你喜欢

转载自www.cnblogs.com/jhxxb/p/10456013.html