zoukankan      html  css  js  c++  java
  • 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) {
            }
        }
    }

    官方文档

  • 相关阅读:
    获取当前android设备是mips架构还是arm架构
    查看android设备的设备名称
    android源码查看源码的版本
    netcfg android
    linux中解压zip 中文乱码处理
    通过命令行查看当前android系统的版本
    关于Yorhom Yorhom's Game Box
    是英雄就下100层(1)
    Javascript可移动地图的实现Demo演示
    HTML5之游戏DEMO Yorhom's Game Box
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10456013.html
Copyright © 2011-2022 走看看