package com.yourcompany.bean;
import java.io.*;
import java.util.*;
import java.sql.*;
public class DBConnection {
/* 数据访问组件 */
private static DBConnection instance = null;
//取得连接
public static synchronized Connection getConnection() {
if (instance == null){
instance = new DBConnection();
}
return instance._getConnection();
}
private DBConnection(){
super();
}
/**
* 创建连接
* @return
*/
private Connection _getConnection(){
try{
String sDBDriver = null;
String sConnection = null;
String sUser = null;
String sPassword = null;
Properties p = new Properties();
InputStream is = getClass().getResourceAsStream("/com/yourcompany/struts/db.properties");
p.load(is);
sDBDriver = p.getProperty("DBDriver",sDBDriver);
sConnection = p.getProperty("Connection",sConnection);
sUser = p.getProperty("User","");
sPassword = p.getProperty("Password","");
Properties pr = new Properties();
pr.put("user",sUser);
pr.put("password",sPassword);
pr.put("characterEncoding", "GB2312");
pr.put("useUnicode", "TRUE");
Class.forName(sDBDriver).newInstance();
return DriverManager.getConnection(sConnection,pr);
}
catch(Exception se){
System.out.println(se);
return null;
}
}
/**
* 根据sql语句返回一个结果集
*/
public static ResultSet getResultSet(String sql) throws SQLException{
return DBConnection.getConnection().createStatement().executeQuery(sql);
}
/**
* 根据sql语句返回一个整型,主要用于增加,删除,修改
*/
public static int getSql(String sql) throws Exception{
return DBConnection.getConnection().createStatement().executeUpdate(sql);
}
/**
*
* @param ps
* @param rs
*/
public static void clearConn(PreparedStatement ps,ResultSet rs){
closeResultSet(rs);
closePreparedStatement(ps);
}
public static void clearConn(PreparedStatement ps,Connection con){
//closeResultSet(rs);
closePreparedStatement(ps);
freeConncetion(con);
}
public static void clearConn(Connection con, PreparedStatement ps,ResultSet rs){
closeResultSet(rs);
closePreparedStatement(ps);
freeConncetion(con);
}
/**
* 释放连接池中的所有Connection
*
* @throws SQLException
*/
public static void freeConncetion(Connection conn){
try{
if(conn != null) conn.close();
}catch(SQLException Ex_Connection){
System.out.println("SQLException when close Connection. "+Ex_Connection);
}
}
/**
* 释放连接池中的所有ResultSet
*
* @throws SQLException
*/
public static void closeResultSet(ResultSet resultset){
try{
if(resultset != null) resultset.close();
}catch(SQLException Ex_Resultset){
System.out.println("SQLException when close ResultSet. "+Ex_Resultset);
}
}
/**
* 释放连接池中的所有PreparedStatement
*
* @throws SQLException
*/
public static void closePreparedStatement(PreparedStatement ps){
try {
if (ps != null) ps.close();
} catch (SQLException Ex_Statement) {
System.out.println("SQLException when close PreparedStatement."+Ex_Statement);
}
}
public static void rollBack(Connection conn)
{
try{
conn.rollback();
}catch(SQLException se){
System.out.println("SQLException when close rollBack."+se);
}
}
public static void main(String[] args) {
Connection conn=DBConnection.getConnection();
System.out.print(conn);
// TODO 自动生成方法存根
}
}
- 1
- 2
- 3
前往页