老是报错。下面是代码
package com.swjd;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 DbUtil {
public static void main(String[] args) {
getConnection();
}
public static Connection getConnection(){
Connection coon=null;
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbinfo.properlies");
Properties pro =new Properties();
try {
pro.load(in);
String driver = pro.getProperty("driver");
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password =pro.getProperty("password");
coon = DriverManager.getConnection(url, username, password);
System.out.println("coon+"+coon);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return coon;
}}dbinfo.properlies里的内容在下面driver=oracle.jdbc.driver.OracleDriver
rul=jdbc:oracle:rhin:@localhost:1521:orcl
username=scott
password=123下面是报错的代码
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at com.swjd.DbUtil.getConnection(DbUtil.java:20)
at com.swjd.DbUtil.main(DbUtil.java:13)
package com.swjd;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 DbUtil {
public static void main(String[] args) {
getConnection();
}
public static Connection getConnection(){
Connection coon=null;
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbinfo.properlies");
Properties pro =new Properties();
try {
pro.load(in);
String driver = pro.getProperty("driver");
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password =pro.getProperty("password");
coon = DriverManager.getConnection(url, username, password);
System.out.println("coon+"+coon);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return coon;
}}dbinfo.properlies里的内容在下面driver=oracle.jdbc.driver.OracleDriver
rul=jdbc:oracle:rhin:@localhost:1521:orcl
username=scott
password=123下面是报错的代码
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at com.swjd.DbUtil.getConnection(DbUtil.java:20)
at com.swjd.DbUtil.main(DbUtil.java:13)
rul=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=123
Properties prop=null;
Connection conn=null;
FileInputStream fis=null;
try {
fis=new FileInputStream("src/jdbc.properties");
prop=new Properties();
prop.load(fis);
String driver=prop.getProperty("driver");
String url=prop.getProperty("url");
Class.forName(driver);
conn=DriverManager.getConnection(url,prop);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
} }
发一个正确的,楼主对照一下吧。
快上班了,没时间帮你对照着看了。