JDBC 连接数据库报错:inStream parameter is null

胖鸟飞不动 2019-08-04 12:12:44
[/code]

折腾了好久 一直报错,不知道为什么。重写一个也是错的。

---------------------------------------------------------------分隔符-----------------------------------------------------------


package BaseUtil;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigManager {
private static ConfigManager configManager;
private static Properties properties;

public ConfigManager(){
properties=new Properties();

String config="MysqlConn.properties";

InputStream inputStream=ConfigManager.class.getClassLoader().getResourceAsStream(config);

try {
properties.load( inputStream );
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ConfigManager getInstance(){
if (configManager==null)configManager=new ConfigManager();
return configManager;
}

public String getValue(String key){
return properties.getProperty( key );
}

}


---------------------------------------------------------------分隔符------------------------------------------------------------



public class BaseDao {
protected Connection conn;
protected PreparedStatement preparedStatement;
protected ResultSet resultSet;

public Connection getConn()throws Exception{
ConfigManager configManager=ConfigManager.getInstance();
String driver=configManager.getValue("driver" );

String url=configManager.getValue( "url" );

String username=configManager.getValue( "username" );

String password=configManager.getValue( "password" );
try {
Class.forName( driver );
conn= DriverManager.getConnection(url,username,password);
System.out.println("sucess");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public PreparedStatement ps(String sql, Object[]param) throws Exception {
this.getConn();
try {
preparedStatement=conn.prepareStatement( sql );
for (int i = 0; i <param.length ; i++) {
preparedStatement.setObject( i+1,param[i] );
}
} catch (SQLException e) {
e.printStackTrace();
}
return preparedStatement;
}
public ResultSet selectSql(String sql,Object[] param) throws Exception {
this.getConn();
this.ps( sql,param );
try {
resultSet =preparedStatement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}finally {
this.releaseResource( resultSet,preparedStatement,conn );
}

return resultSet;
}
public void releaseResource(ResultSet resultSet, PreparedStatement preparedStatement,Connection con ){
try {
if (resultSet!=null)resultSet.close();
if (preparedStatement!=null)preparedStatement.close();
if (con!=null)con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


---------------------------------------------------------------分隔符------------------------------------------------------------


...全文
2606 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
miozus 2021-05-26
  • 打赏
  • 举报
回复
`target/blog-1.0-SNAPSHOT/WEB-INF` 如果是 webapp ,这里也要复制一份放进去
miozus 2021-04-24
  • 打赏
  • 举报
回复 2
对的,配置文件至少、最终要出现在编译后的类目录里面


src.main
↳java
↳resources
druid.properties

target
↳ classes
↳ druid.properties


单纯放在 resource 目录,也可能报错:

昨天还行,线上连接又可以连接数据库,线下测试报错找不到配置文件,可能 maven clean 清空库存了。

我把配置文件,从 src 复制到编译后的 target 目录下的 classes 里面,又能连接上了。
湫杏野氵 2021-03-11
  • 打赏
  • 举报
回复
啊,我也解决了,就是要把properties放在resources里面然后就可以运行了
faith.huan 2019-08-04
  • 打赏
  • 举报
回复
重新放置下你的MysqlConn.properties文件位置, 你放到src目录下试试, 无法读取到你的配置文件 InputStream inputStream=ConfigManager.class.getClassLoader().getResourceAsStream(config); 这行代码执行后,inputStream==null
胖鸟飞不动 2019-08-04
  • 打赏
  • 举报
回复
引用 1 楼 huanqingdong 的回复:
重新放置下你的MysqlConn.properties文件位置, 你放到src目录下试试, 无法读取到你的配置文件 InputStream inputStream=ConfigManager.class.getClassLoader().getResourceAsStream(config); 这行代码执行后,inputStream==null
哦 我搞错了,我再路径里 加了resources/ 。。。。。
faith.huan 2019-08-04
  • 打赏
  • 举报
回复
引用 2 楼 versus117的回复:
[quote=引用 1 楼 huanqingdong 的回复:] 重新放置下你的MysqlConn.properties文件位置, 你放到src目录下试试, 无法读取到你的配置文件 InputStream inputStream=ConfigManager.class.getClassLoader().getResourceAsStream(config); 这行代码执行后,inputStream==null
很奇怪,我把resources 文件夹整个放到src里面的时候还是报错,单独把properties文件拿出来就好了,再放回到resources文件夹后又不报错了……[/quote] 只要你的calsees文件夹里有这个properties文件就不报错,你把resources文件mark为resources目录试一下,在文件夹上右键,mark xxx
胖鸟飞不动 2019-08-04
  • 打赏
  • 举报
回复
引用 1 楼 huanqingdong 的回复:
重新放置下你的MysqlConn.properties文件位置, 你放到src目录下试试, 无法读取到你的配置文件 InputStream inputStream=ConfigManager.class.getClassLoader().getResourceAsStream(config); 这行代码执行后,inputStream==null
很奇怪,我把resources 文件夹整个放到src里面的时候还是报错,单独把properties文件拿出来就好了,再放回到resources文件夹后又不报错了……

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧