自定义的一个JDBC工具类
package JDBCutils;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import load.loadDemo;
public class JDBCutils {
private JDBCutils(){} //为了不让类外创建对象
private static Properties pro = new Properties();
static{
try {
//通过类加载器将配置文件加载到内存中
String path= loadDemo.class.getClassLoader().getResource("configLoad.properties").getPath();
pro.load(new FileInputStream(new File(path)));
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn(){
Connection conn = null;
try {
String databaseuser = pro.getProperty("databaseuser");
String databasepassword = pro.getProperty("databasepassword");
String loadurl = pro.getProperty("loadurl");
String driver = pro.getProperty("driver");
Class.forName(driver);
conn = DriverManager.getConnection(loadurl,databaseuser,databasepassword);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* JDBC 释放资源静态方法
* @author Administrator
* @param conn 连接对象
* @param stat 传输器对象
* @param rs 结果集
*/
public static void closeResou(Connection conn,Statement stat,ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
stat = null;
}
}
if(stat != null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
stat = null;
}
}
if(conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
}