importjava.io.InputStream;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;importjava.util.Properties;public classjdbcUtil {private staticString driver;private staticString url;private staticString id;private staticString password;//加载文件,获取配置参数
static{
Properties prop=newProperties();
InputStream is;try{
is= jdbcUtil.class.getClassLoader()
.getResourceAsStream("com/sy/db/db.properties");
prop.load(is);
driver= prop.getProperty("driver");
url= prop.getProperty("url");
id= prop.getProperty("id");
password= prop.getProperty("password");
}catch(Exception e1) {
e1.printStackTrace();
}
}//注册驱动
static{try{
Class.forName(driver);
}catch(Exception e) {
e.printStackTrace();
}
}//取得连接
public staticConnection getMysqlConnection(){
Connection conn=null;try{
conn=DriverManager.getConnection(url,id,password);
}catch(Exception e) {
e.printStackTrace();
}returnconn;
}//关闭流
public static voidclose(ResultSet rs){if(null!=rs){try{
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}public static voidclose((Statement state){if(null!=rs){try{
state.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}public static voidclose(Connection conn){if(null!=rs){try{
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}