这个是tomcat运行时配置文件没有进入target包里面,把配置文件复制进去就可以了
public class JDBCUtils {
private static String driver;
private static String url;
private static String password;
private static String username;
// 利用静态代码块,在类加载的时候就把参数加载进来
static {
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
try {
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
password = properties.getProperty("password");
username = properties.getProperty("username");
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
@Test
public void test(){
Connection conn = getConnection();
System.out.println(conn);
}