您好下面是我的代码,它将用作连接工厂类。但我得到了 java.lang.ExceptionInInitializerError。请指教如何解决?我认为这是由于静态块造成的陷阱,但不知道这到底是什么。
package j2ee.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnFactory
{
public static Properties prop;
static
{
prop=new Properties();
try {
prop.load(ConnFactory.class.getClassLoader().getResourceAsStream("DBConfig.properties"));
} catch (Exception e) {
e.printStackTrace();
}
}
private static ConnFactory instance = new ConnFactory();
public static final String URL = prop.getProperty("DEVURL");
public static final String USER = prop.getProperty("DEVUSER");
public static final String PASSWORD = prop.getProperty("DEVPASSWORD");
public static final String DRIVER_CLASS = prop.getProperty("DEVDRIVER_CLASS");
private ConnFactory() {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private Connection createConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
System.out.println("ERROR: Unable to Connect to Database.");
}
return connection;
}
public static Connection getConnection() {
return instance.createConnection();
}
public static void main(String a[])
{
Connection test=ConnFactory.getConnection();
System.out.println("Done");
}
}
错误是:
Caused by: java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at j2ee.dao.ConnFactory.<init>(ConnFactory.java:28)
at j2ee.dao.ConnFactory.<clinit>(ConnFactory.java:20)