packagecom.gm.dao;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;/*** DBUtil:数据库工具类
*
*@authorXiangdongLee
**/
public classDBUtil {/*** 获得数据库连接
*
*@return
*/
public staticConnection getConnection() {
Connection conn= null;
String driverName= "com.mysql.jdbc.Driver";
String url= "jdbc:mysql://localhost:3306/dbmin";
String username= "scott";
String password= "tiger";try{
Class.forName(driverName);
conn=DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e) {
System.out.println("加载数据库驱动出错...");
e.printStackTrace();
}catch(SQLException e) {
System.out.println("获得数据库连接出错...");
e.printStackTrace();
}returnconn;
}/*** 关闭数据库连接
*
*@paramconn
*@paramps
*@paramrs*/
public static voidclose(Connection conn, PreparedStatement ps, ResultSet rs) {//关闭原则:先开后关,后开先关
if (rs != null) {try{
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (ps != null) {try{
ps.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (conn != null) {try{
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}public static voidmain(String[] args) {
System.out.println(getConnection());
}
}