日期:2014-05-17  浏览次数:20772 次

关于JDBC工具类的问题...不知道是不是我配置的问题. 在线等.
package JDBCUtil;

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

public class JdbcUtils {

private JdbcUtils(){}
private static String url;
private static String user;
private static String password;

static{
InputStream in = JdbcUtils.class.getResourceAsStream("jdbc.properties");
Properties config = new Properties();
try {
config.load(in);
url = config.getProperty("url");
user = config.getProperty("user");
password = config.getProperty("password");
Class.forName(config.getProperty("driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,user,password);
}

public static void release(Connection con , Statement st , ResultSet rs){


if(st != null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws SQLException{
System.out.println(getConnection());
}
}
这是报的错 Could not find the main class : JDBCUtil.jdbcUtils. Program will exit.


java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at JDBCUtil.JdbcUtils.<clinit>(JdbcUtils.java:27)
Exception in thread "main"

------解决方案--------------------
错误提示很明显,是这句Class.forName(config.getProperty("driver"));有问题,检查:
1)jdbc.properties中driver是否正确;
2)数据库连接所需jar包是否导入;
------解决方案--------------------
+1
探讨

错误提示很明显,是这句Class.forName(config.getProperty("driver"));有问题,检查:
1)jdbc.properties中driver是否正确;
2)数据库连接所需jar包是否导入;

------解决方案--------------------
一看题目在线等,哇,这么急。一看时间,凌晨14分,我当时还在梦里。
一楼正解,不过楼主估计等睡着了。