DELMAR_
2021-07-13 08:52
采纳率: 100%
浏览 43
已采纳

javaweb数据库连接问题,出现NullPointer问题

问题在连接时,出现空指针
一开始以为是版本不匹配,因为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());
    }

}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 格一物 2021-07-13 10:30
    已采纳

    属性值不匹配,将properties中的jdbc.url改为url,用户名和密码也对应调整下,属性名参照下图名称!
    遇到这样的问题,看下jar包源码,再不行断点调试系,现在用maven和idea看源码很方便的,不像以前用老板的eclipse,还要借助插件或jd-gui编译

    img

    img

    已采纳该答案
    打赏 评论
  • 格一物 2021-07-13 09:26

    JdbcUtils 文件25行打断点,查看properties对象属性值,jdbc.url是否正确,看样子是驱动类未识别出来你,加载驱动类时,全限类名为空导致的!

    img

    打赏 评论
  • DELMAR_ 2021-07-13 10:12

    img

    打赏 评论

相关推荐 更多相似问题