跳过废话点下面
今天使用 Druid连接 本地的MySQL服务器,写了一个简单的 Druid读取配置文件,利用工厂模式创建数据连接池。
import com.alibaba.druid.pool.DruidDataSourceFactory
import java.io.FileInputStream
import java.io.InputStream
import java.sql.Connection
import java.util.Properties
import javax.sql.DataSource
class DBUtils {
//数据源连接池对象
private var dataSource: DataSource? = null
//ThreadLocal 对象
private var threadLocal: ThreadLocal<Connection>? = null
init {
try {
val inputStream: InputStream = FileInputStream("mysql/db.properties")
val properties: Properties = Properties()
properties.load(inputStream)
//使用Druid连接池方式
dataSource = DruidDataSourceFactory.createDataSource(properties)
threadLocal = ThreadLocal()
} catch (e: Exception) {
e.printStackTrace()
}
}
fun getConnection():Connection{
return dataSource!!.connection
}
}
配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/root
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
IDEA的终端报错
"D:\Program Files\Java\jdk-17.0.2\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar=51419:D:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\WuXiangGuJun\IdeaProjects\SQLite-JDBC\build\classes\kotlin\main;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk8\1.6.20\dab8089bca6ac0e394c37281ea8cff2f99acd421\kotlin-stdlib-jdk8-1.6.20.jar;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\org.xerial\sqlite-jdbc\3.36.0.3\7fa71c4dfab806490cb909714fb41373ec552c29\sqlite-jdbc-3.36.0.3.jar;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\com.alibaba\druid\1.2.9\1019c77634341b3b2dafe553a71c1874a7dce570\druid-1.2.9.jar;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\mysql\mysql-connector-java\8.0.29\16bfffda393ac4fe56f0985f1f035b37d3fc48f\mysql-connector-java-8.0.29.jar;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk7\1.6.20\f8629f336bad4001c89e9cffa5ef3d4b5d0f5e22\kotlin-stdlib-jdk7-1.6.20.jar;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.6.20\6cedc143badbb4f1c6b7f5a340b04edff1743208\kotlin-stdlib-1.6.20.jar;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\com.google.protobuf\protobuf-java\3.19.4\748e4e0b9e4fa6b9b1fe65690aa04a9db56cfc4d\protobuf-java-3.19.4.jar;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-common\1.6.20\27b4562b6713d70f458c6d7ea39aadacb8e6a92b\kotlin-stdlib-common-1.6.20.jar;C:\Users\WuXiangGuJun\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations\13.0\919f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar DBTestKt
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
5月 11, 2022 10:30:24 上午 com.alibaba.druid.pool.DruidDataSource error
严重: init datasource error, url: jdbc:mysql://localhost:3306/root
java.sql.SQLSyntaxErrorException: Unknown database 'root'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1667)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1733)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:924)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1403)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1399)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:100)
at DBUtils.getConnection(DBUtils.kt:30)
at DBTestKt.main(DBTest.kt:3)
5月 11, 2022 10:30:24 上午 com.alibaba.druid.pool.DruidDataSource error
严重: {dataSource-1} init error
java.sql.SQLSyntaxErrorException: Unknown database 'root'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1667)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1733)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:924)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1403)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1399)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:100)
at DBUtils.getConnection(DBUtils.kt:30)
at DBTestKt.main(DBTest.kt:3)
5月 11, 2022 10:30:24 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Exception in thread "main" java.sql.SQLSyntaxErrorException: Unknown database 'root'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1667)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1733)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:924)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1403)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1399)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:100)
at DBUtils.getConnection(DBUtils.kt:30)
at DBTestKt.main(DBTest.kt:3)
进程已结束,退出代码1
随手上网一搜索,是因为不同版本的jdbc驱动包应该使用不同的驱动类名。
如图我的MySQL版本是5.7.26,但是我下载的MySQL驱动包是最新的
却使用com.mysql.jdbc.Driver
@TOC
解决办法
所以我应该将db.properties里面的driverClassName修改成
driverClassName=com.mysql.cj.jdbc.Driver
再次运行,我们可以看到已经没有*Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.*报错了
我太懒了,不愿意做笔记全部靠脑子。每次遇到问题都会将大量时间花在百度上面,现在记录一下,下次遇到可以直接解决