标签:
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();
}
}
}
标签:
原文地址:http://www.cnblogs.com/bmbi/p/5027434.html