javabean
package dbc;
import java.sql.*;
import java.util.Properties;
public class JdbcUtil {
static String driver;
static String url;
static String user;
static String password;
static Properties pr=new Properties();
JdbcUtil(){}
static{
try{pr.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
driver=pr.getProperty("driver");
url=pr.getProperty("url");
user=pr.getProperty("username");
password=pr.getProperty("password");
Class.forName(driver);
}catch(Exception e){
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection()throws SQLException{
return DriverManager.getConnection(url,user,password);
}
public static void free(ResultSet rs,Statement st,Connection conn){
try{
if(rs!=null)rs.close();
}catch(SQLException e){e.printStackTrace();}
finally{try{if(st!=null)rs.close();}catch(SQLException e){e.printStackTrace();}
finally{if(conn!=null)try{conn.close();}catch(SQLException e){e.printStackTrace();}}}
}
}
浙公网安备 33010602011771号