如题,初学者的我想写个DBUTils连接池却接连报错"noullpointException",我用断点
调试发现是Class.forname(driverClass)里的数据是noull
我到网上找答案,都说是mysql驱动包没放对,我又另外找教程然后老老实实照着教程做,但是还是这个问题。
我把我的配置贴一下,请各位指出错误:
这是我的工程目录
这是我的配置文件
url=jdbc:mysql://localhost:3306/mybase
user=root
password=123
driverClass=com.mysql.jdbc.Driver
把工具类也贴一下吧
package com.study_01;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCTest {
//定义全局变量
private static String url = "jdbc:mysql://localhost:3306/mybase";
private static String user = "root";
private static String password = "123";
private static String driverClass = "com.mysql.jdbc.Driver";
//读取配置文件内容,放在静态代码块中就行,因为只需要加载一次就可以了
static{
try{
Properties props = new Properties();
//使用类路径加载的方式读取配置文件
//读取的文件路径要以“/”开头,因为如果使用“.”的话,当部署到服务器上之后就找不到文件了,使用“/”开头会直接定位到工程的src路径下
InputStream in = JDBCTest.class.getResourceAsStream("database.properties");
//加载配置文件
props.load(in);
//读取配置文件信息
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
String driverClass = props.getProperty("driverClass");
//注册驱动程序
Class.forName(driverClass);
}catch(Exception e){
e.printStackTrace();
System.out.println("驱动程序注册失败!!!");
}
}
//获取连接对象Connection
public static Connection getConnection(){
try{
return DriverManager.getConnection(url,user,password);
}catch(SQLException e){
e.printStackTrace();
//跑出运行时异常
throw new RuntimeException();
}
}
}