import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static String Driver;
private static String URL;
private static String user;
private static String passwed;
private static String path;
private static Connection con;
private JdbcUtils() {
}
static {
Properties properties = new Properties();
InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("config.properties");
try {
/**
* 加载配置文件
*/
properties.load(is);
Driver = properties.getProperty("Driver");
URL = properties.getProperty("URL");
user = properties.getProperty("user");
passwed = properties.getProperty("passwed");
/**
* 注册驱动
*/
Class.forName(Driver);
/**
* 获得Connection对象
*/
con = DriverManager.getConnection(URL, user, passwed);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 返回Connection连接对象
* @return
*/
public static Connection getConnection() {
return con;
}
/**
* 释放资源
* @param sta
* @param con
*/
public static void close(Statement sta, Connection con) {
close(null, sta, con);
}
/**
* 释放资源
* @param rest
* @param sta
* @param con
*/
public static void close(ResultSet rest, Statement sta, Connection con) {
if (rest != null) {
try {
rest.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
jdbc工具类1
最新推荐文章于 2022-10-19 09:45:06 发布