日志记录出错,搞毛啊...
-
"E:\Program Files\OpenJDK\jdk-13.0.1\bin\java.exe" --enable-preview "-javaagent:E:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.4\lib\idea_rt.jar=4413:E:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath F:\Intellij-workspace\JDBC\target\classes;C:\Users\11479.m2\repository\com\zaxxer\HikariCP\3.4.1\HikariCP-3.4.1.jar;C:\Users\11479.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\11479.m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar;C:\Users\11479.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar com.bk.jdbc.test.ConnTest
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Connection established.
Process finished with exit code 0
用OpenJDK 13
编译,Maven
解决依赖Maven的配置是
<?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>BKTech</groupId> <artifactId>JDBC</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>13.0.1</source> <target>13.0.1</target> <compilerArgs>--enable-preview</compilerArgs> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
DBUtil的代码是
package com.bk.jdbc.util; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class DBUtil { /* * load DB driver */ // static { // try { // Class.forName("com.mysql.jdbc.Driver"); // }catch (ClassNotFoundException e) { // System.out.println("Failed to load DB driver!"); // } // } /* * establish connection */ private static DataSource ds; static { try { HikariConfig cfg=new HikariConfig("./conf/bruce.conf"); ds=new HikariDataSource(cfg); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConn() throws SQLException { return ds.getConnection(); } /* * close connection */ public static void closeConn(Connection conn) { try { conn.close(); }catch (SQLException e) { e.printStackTrace(); } } }
虽然只是几条warning,但是每次连接数据库的时候都提示,很烦啊
-
顺便说下,ConnTest的代码是
package com.bk.jdbc.test; import com.bk.jdbc.util.DBUtil; import java.sql.Connection; import java.sql.SQLException; public class ConnTest { public static void main(String[] args) { Connection conn=null; try{ conn= DBUtil.getConn(); System.out.println("Connection established."); }catch (SQLException e) { e.printStackTrace(); }finally { DBUtil.closeConn(conn); } } }
-
解决了...用
Maven
再添加一个slf4j-jdk14
的依赖就行...<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.7.25</version> </dependency>
"E:\Program Files\OpenJDK\jdk-13.0.1\bin\java.exe" --enable-preview "-javaagent:E:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.4\lib\idea_rt.jar=5980:E:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath F:\Intellij-workspace\JDBC\target\classes;C:\Users\11479.m2\repository\com\zaxxer\HikariCP\3.4.1\HikariCP-3.4.1.jar;C:\Users\11479.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\11479.m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar;C:\Users\11479.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;C:\Users\11479.m2\repository\org\slf4j\slf4j-jdk14\1.7.25\slf4j-jdk14-1.7.25.jar com.bk.jdbc.test.ConnTest
11月 19, 2019 6:41:18 下午 com.zaxxer.hikari.HikariDataSource <init>
信息: HikariPool-1 - Starting...
11月 19, 2019 6:41:19 下午 com.zaxxer.hikari.HikariDataSource <init>
信息: HikariPool-1 - Start completed.
Connection established.
Process finished with exit code 0