这是源程序:
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();
}
}
}
------解决方案--------------------
好像是没有找到dot.properties
这个文件是放在src下面的吗?
------解决方案--------------------
不是连接数据库不上,是因为读不到配置文件。。。
------解决方案--------------------
明显是读文件出问题了