java项目中连接Oracle数据库时,出现ClassNotFoundException
我在自己的类中写了getConnection函数,读取文件database.properties中连接oracle的信息,然后返回这个数据库连接。
在Eclipse里我也添加了ojdbc14.jar包,过程是Project->Properties->Java Build Path->Libraries->Add External JARs
为什么总是出现如下错误:
Unhandled exception type ClassNotFoundException
public static Connection getConnection() throws SQLException, IOException { Properties props = new Properties(); FileInputStream in = new FileInputStream("database.properties"); props.load(in); in.close(); String drivers = props.getProperty("jdbc.drivers"); if (drivers != null) System.setProperty("jdbc.drivers", drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); Class.forName("oracle.jdbc.driver.OracleDriver");//ClassNotFoundException return DriverManager.getConnection(url, username, password); }
#jdbc.drivers=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@localhost:1521:xejdbc.username=schooljdbc.password=school