import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {
private static final String URL="jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8";
private static final String UER_NAME = "root";
private static final String PDW="123123";
private static DBUtils me = new DBUtils();
public static DBUtils getInstance(){
return me;
}
//為了是單例模式成為唯一可以獲取該對象的方法,將該類的無參構造函數設為私有
private DBUtils(){}
/*
* 數據庫連接
*/
public Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, UER_NAME,PDW);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/*
* 釋放資源
*/
public void releaseRes(Connection conn,PreparedStatement pstmt,ResultSet rs){
try {
if(conn!=null) conn.close();
if(pstmt!=null) pstmt.close();
if(rs!=null) rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}