麻烦大家看下谢谢 !空指针问题 新手求解决办法

czq444 2017-07-26 05:05:31
public class TextJdbc {


public void Text1Driver() throws Exception {

String driverClass = null;
String url = null;
String user = null;
Object password = null;
// 读取类路径下的jdbcdfile文件
InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();

properties.load(is); //提示在这出现空指针异常

driverClass = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");

Driver driver = (Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = (Connection) driver.connect(url, info);
}

public static void main(String[] args) throws Exception {
TextJdbc tj = new TextJdbc();
tj.Text1Driver();

}

}
...全文
625 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ohense 2017-07-30
  • 打赏
  • 举报
回复
InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); 相对路径应该为:"src\\JDBC\\jdbc.properties" 是以工程路径为根目录
异类丶 2017-07-27
  • 打赏
  • 举报
回复
路径加个/试试。
czq444 2017-07-27
  • 打赏
  • 举报
回复
引用
学着看看异常,空指针这类问题上了班问出来会挨骂的 谢谢提醒!
JOKER叔叔 2017-07-27
  • 打赏
  • 举报
回复
学着看看异常,空指针这类问题上了班问出来会挨骂的
czq444 2017-07-27
  • 打赏
  • 举报
回复
引用
你这图都放到包里面去了?直接放src下面。。。。
什么意思, 不太懂诶, 文件在src下啊? 什么图?谢谢回答
引用
InputStream is = getClass().getResourceAsStream("jdbc.properties");
改了出现错误 连接数据库错误 谢谢回答 Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at JDBC.TextJdbc.Text1Driver(TextJdbc.java:51) 在下面这行 Connection connection = (Connection) driver.connect(url, info); at JDBC.TextJdbc.main(TextJdbc.java:57)
李德胜1995 2017-07-26
  • 打赏
  • 举报
回复
引用 5 楼 czq444 的回复:
文件在src下
你这图都放到包里面去了?直接放src下面。。。。
soton_dolphin 2017-07-26
  • 打赏
  • 举报
回复
InputStream is = getClass().getResourceAsStream("jdbc.properties");
czq444 2017-07-26
  • 打赏
  • 举报
回复
文件在src下

lifeizhao 2017-07-26
  • 打赏
  • 举报
回复
"jdbc.properties 没找到这个文件
李德胜1995 2017-07-26
  • 打赏
  • 举报
回复
把properties文件放在src下面,或者src右键建立一个resource文件放在里面。。。
soton_dolphin 2017-07-26
  • 打赏
  • 举报
回复
试一下用 TextJdbc.class.getResourceAsStream("/jdbc.properties") 看看你的文件名是不是一样
soton_dolphin 2017-07-26
  • 打赏
  • 举报
回复
你的 jdbc.properties 放在哪里?

62,614

社区成员

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

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