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) {
            }
        }
    }

    官方文档

  • 相关阅读:
    外键的创建
    MySQL (1366, "Incorrect string value: '\xF0\x9F\x8E\xAC\xE5\x89...' for column 'description' at row 1")
    python 获取文件路径
    ModuleNotFoundError: No module named 'Crypto'
    Django---错误
    CentOS7系统ifconfig无法使用的解决方法
    NACOS集群搭建遇到的问题
    Mysql连接报错:1130-host ... is not allowed to connect to this MySql server如何处理
    CentOS7 yum方式安装MySQL5.7
    MySQL数据库的全局锁和表锁
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10456013.html
Copyright © 2011-2022 走看看