报错:スレッド「main」の例外java.lang.NullPointer例外:inStreamパラメーターがnull
最初のステップ:
問題の原因:
このリンクを参照してください
https://www.cnblogs.com/xc1370338904/p/11212687.html
ここでソースコードを一致させます:
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC
jdbc.userName=root
jdbc.passWord=636895
package jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils {
private static Properties properties=null;
static {
//初始化properties,即加载dbconfig.properties配置文件1次到properties中
try {
//1.加载配置文件
InputStream inputStream=JdbcUtils.class.getClassLoader().getResourceAsStream("dbconfig.properties");
properties =new Properties();
properties.load(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
Connection connection=JdbcUtils.getConnection();
System.out.println(connection);
}
public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
//2.加载驱动类
Class.forName(properties.getProperty("jdbc.driverClassName"));
//3.通过Drivermanager得到connection
Connection connection=DriverManager.getConnection(properties.getProperty("jdbc.url"), properties.getProperty("jdbc.userName"), properties.getProperty("jdbc.passWord"));
return connection;
}
}
結果: