日期:2014-05-20  浏览次数:20721 次

配置文件读取连接数据库问题
配置文件如下:
beanDaoImpl=com.lipan.daoImpl.BeanDaoImpl
userService=com.lipan.serviceImpl.UserServiceImpl
url="jdbc:mysql://127.0.0.1:3306/lipan"
user="root"
password="root"
driver="com.mysql.jdbc.Driver"

数据库连接代码如下

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public final class DBConnection {

private static String url=null;
private static String user=null;
private static String password=null;
private static String driver=null;
private DBConnection(){}

private volatile static DBConnection instance=null;

public static DBConnection getInstance(){
if(null==instance){
synchronized (DBConnection.class) {
if(null==instance){
instance=new DBConnection();
}
}
}
return instance;
}


public Connection getConnection(){
Connection conn=null;
try {
Properties prop=new Properties();
InputStream inStream=DBConnection.class.getClassLoader().getResourceAsStream("TestProperties.properties");
prop.load(inStream);

url=(String)prop.getProperty("url");
user=(String)prop.getProperty("user");
password=(String)prop.getProperty("password");
driver=(String)prop.getProperty("driver");

Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public void close(ResultSet rs, PreparedStatement ps, Connection conn){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e2) {
e2.printStackTrace();
}finally{
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e3) {
e3.printStackTrace();
}
}
}

}

}


数据库连接信息写死不用从配置文件读信息没有错误我试过的,但是从配置文件读取后连接代码如上出错,不太理解,高手指点下下,谢谢了!

报错如下
java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.lipan.jdbc.util.DBConnection.getConnection(DBConnection.java:45)
at com.lipan.daoImpl.BeanDaoImpl.CUD(BeanDaoImpl.java:21)
at com.lipan.serviceImpl.UserServiceImpl.creatUser(UserServiceImpl.java:24)
at com.lipan.serviceImpl.UserServiceImpl.main(UserServiceImpl.java:19)
java.lang.NullPointerException
at com.lipan.daoImpl.BeanDaoImpl.CUD(BeanDaoImpl.java:23)
at com.lipan.serviceImpl.UserServiceImpl.creatUser(UserServiceImpl.java:24)
at com.lipan.serviceImpl.UserServiceImpl.main(UserServiceImpl.java:19)

------解决方案--------------------
user="root"
password="root"
driver="com.mysql.jdbc.Driver"

==》

user=root
password=root
driver=com.mysql.jdbc.Driver