1、异常视图
2、解决方法
与之相关的部分代码:
1 static{2 try{3 //读取db.properties
4 Properties props = newProperties();5 FileInputStream in = new FileInputStream("./src/db.properties");6 //加载文件
7 props.load(in);8 url = props.getProperty("url");9 user = props.getProperty("user");10 password = props.getProperty("password");11 driverClass = props.getProperty("driverClass");12 //注册驱动
13 Class.forName(driverClass);14 } catch(FileNotFoundException e) {15 e.printStackTrace();16 } catch(IOException e) {17 //TODO Auto-generated catch block
18 e.printStackTrace();19 } catch(ClassNotFoundException e) {20 //TODO Auto-generated catch block
21 e.printStackTrace();22 System.out.println("注册驱动失败");23 }24 }
首先,我这个项目是Java web项目: (上面红色处的代码是Java项目的写法)
说明:
1、点(.):代表java命令运行的目录;
2、斜杠(/):表示classpath的根目录;
在java项目下,点(.) java命令的运行目录从项目的根目录开始的; classpath的根目录从bin目录开始的
在web项目下,点(.) java命令的运行目录从tomcat/bin目录下开始的; classpath的根目录从web-INF/classes目录下开始的
解决方法:
把上面红色处的代码FileInputStream in = new FileInputStream("./src/db.properties");改为InputStream in=JdbcUtil.class.getResourceAsStream("/db.properties");即可解决。