[Java] 纯文本查看 复制代码
// 工具类
public class JDBCUILTS1 {
private static String url;
private static String user;
private static String password;
private static String driver;
static {
try {
Properties p = new Properties();
p.load(JDBCUILTS1.class.getClassLoader().getResourceAsStream("jdbc.properties"));
url = p.getProperty("url");
user = p.getProperty("user");
password = p.getProperty("password");
driver = p.getProperty("driver");
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获取连接 返回连接对象
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
// 抛异常
public static void close(Connection con, Statement st) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection con, Statement st, ResultSet rs) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}