这是源程序:
package JdbcUtil;
import java.sql.*;
import java.io.*;
import java.util.*;public class JdbcUtil {
private static String url/*="jdbc:mysql://localhost:3306/mysql1"*/;
private static String driver/*="com.mysql.jdbc.Driver"*/;
private static String user/*="root"*/;
private static String password/*="root"*/; // 在静态块中自动读取属性文件
static {
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dot.properties");
Properties pro = new Properties();
try {
pro.load(in);
driver = pro.getProperty("driver");
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
} public static Connection getConnection() throws ClassNotFoundException,
SQLException {
Connection conn = null;
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
return conn;
} public static void closeDb(ResultSet res, Statement stat, Connection conn) { try {
if (res != null) {
res.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
} try {
if (stat != null) {
stat.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
} try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
} } public static void main(String[] args) {
try {
Connection conn = JdbcUtil.getConnection();
System.out.println(conn);
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} }
package JdbcUtil;
import java.sql.*;
import java.io.*;
import java.util.*;public class JdbcUtil {
private static String url/*="jdbc:mysql://localhost:3306/mysql1"*/;
private static String driver/*="com.mysql.jdbc.Driver"*/;
private static String user/*="root"*/;
private static String password/*="root"*/; // 在静态块中自动读取属性文件
static {
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dot.properties");
Properties pro = new Properties();
try {
pro.load(in);
driver = pro.getProperty("driver");
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
} public static Connection getConnection() throws ClassNotFoundException,
SQLException {
Connection conn = null;
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
return conn;
} public static void closeDb(ResultSet res, Statement stat, Connection conn) { try {
if (res != null) {
res.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
} try {
if (stat != null) {
stat.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
} try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
} } public static void main(String[] args) {
try {
Connection conn = JdbcUtil.getConnection();
System.out.println(conn);
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} }
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql1
user=root
password=root
求解
PS(用的是MyEclipse写的,数据库驱动已连接)
java.lang.ExceptionInInitializerError
Caused by: 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 JdbcUtil.JdbcUtil.<clinit>(JdbcUtil.java:17)
Exception in thread "main"
这个文件是放在src下面的吗?