Today, in the process of learning JDBC, running a JDBC Java times error is as follows:
Exception in thread ' main ' java.lang.NoClassDefFoundError:Could not initialize class Me.gacl.utils.JdbcUtils
At Me.gacl.demo.Test.main (test.java:30)
Check, this exception is said to find me.gacl.utils.JdbcUtils this class, looked at this code, as follows:
Package me.gacl.utils;
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 driver = null;
private static String URL = null;
private static String username = null;
private static String password = NULL;
static{
try{
Read database connection information in the Db.properties file
Properties prop = new properties ();
InputStream in = JdbcUtils.class.getClassLoader (). getResourceAsStream ("db.properties");
Prop.load (in);
Get database Connection Driver
Driver = Prop.getproperty ("Driver");
Get Database connection URL address
url = prop.getproperty ("url");
Get database connection user name
Username = Prop.getproperty ("username");
Get the database connection password
Password = prop.getproperty ("password");
Load Database Driver
Class.forName (driver);
}catch (Exception e) {
throw new Exceptionininitializererror (e);
}
}
public static Connection getconnection () throws sqlexception{
Return drivermanager.getconnection (Url,username,password);
}
public static void Release (Connection conn,statement St,resultset rs) {
if (rs!=null) {
try{
Close the ResultSet object that stores the query results
Rs.close ();
}catch (Exception e) {
E.printstacktrace ();
}
rs = null;
}
if (st!=null) {
try{
Close the statement object that is responsible for executing the SQL command
St.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
if (conn!=null) {
try{
Close the Connection database connection object
Conn.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
}
}
Found that there is no obvious error, in looking at the error message, found unable to instantiate the class, so is the problem of static code block, want to see the specific occurrence of the exception is what, so put throw new Exceptionininitializererror (e);
Change to E.printstacktrace (); Then the specific exception information appears as follows:
java.lang.NullPointerException
At java.util.properties$linereader.readline (properties.java:434)
At java.util.Properties.load0 (properties.java:353)
At java.util.Properties.load (properties.java:341)
At me.gacl.utils.jdbcutils.<clinit> (jdbcutils.java:25)
At Me.gacl.demo.Test.main (test.java:17)
java.sql.SQLException:The URL cannot be null
At java.sql.DriverManager.getConnection (drivermanager.java:649)
At java.sql.DriverManager.getConnection (drivermanager.java:247)
At me.gacl.utils.JdbcUtils.getConnection (jdbcutils.java:45)
At Me.gacl.demo.Test.main (test.java:17)
The reason for the exception is that the class URL is empty, stating inputstream in = JdbcUtils.class.getClassLoader (). getResourceAsStream ("db.properties"); Db.properties can not get, and then go to see Class.getclassloader (). getResourceAsStream method, which shows that this method will get the file from Classpath, so generally put the configuration file in the project \ Web-inf\classes folder, and then find that the db.properties is not under the classes file, so this exception is resolved.
Placing the configuration file in the classes directory solves the problem.
Workarounds for Noclassdeffounderror Exceptions using JDBC