问题在连接时,出现空指针
一开始以为是版本不匹配,因为mysql8.0.21的,所以把Connector/J升至8.0.21,但是测试了下,还是报空指针
以下是运行时的sout
创建流对象java.io.BufferedInputStream@32a1bec0
创建数据库连接池{
CreateTime:"2021-07-13 08:43:54",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
七月 13, 2021 8:43:54 上午 com.alibaba.druid.pool.DruidDataSource error
严重: {dataSource-1} init error
java.lang.NullPointerException
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:589)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:815)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1227)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1223)
at com.atgui.utils.JdbcUtils.connection(JdbcUtils.java:43)
at com.atgui.utils.JdbcUtilsTest.testJdbcTest(JdbcUtilsTest.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
java.lang.NullPointerException
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:589)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:815)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1227)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1223)
at com.atgui.utils.JdbcUtils.connection(JdbcUtils.java:43)
at com.atgui.utils.JdbcUtilsTest.testJdbcTest(JdbcUtilsTest.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
以下为代码
package com.atgui.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils {
private static DruidDataSource dataSource;
static {
try {
Properties properties = new Properties();
// 读取 jdbc.properties 属性配置文件
InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
System.out.println("创建流对象"+inputStream);
// 从流中加载数据
properties.load(inputStream);
// 创建数据库 连接池
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
System.out.println("创建数据库连接池"+dataSource);
// System.out.println(dataSource.getConnection());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接池中的链接
* @return
*/
public static Connection connection(){
Connection conn = null;
try {
conn = dataSource.getConnection();
System.out.println("数据库链接成功"+conn);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭连接
// * @param connection
*/
public static void close(Connection conn){
if(conn != null){
try {
conn.close();
System.out.println("连接已关闭");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
以下为测试类
package com.atgui.utils;
import org.junit.Test;
public class JdbcUtilsTest {
@Test
public void testJdbcTest(){
System.out.println(JdbcUtils.connection());
}
}