package dbconnection;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.DbUtils;
public class DBConnection implements Serializable{
private java.lang.String dbUserID;
private java.lang.String dbPassword;
private java.lang.String dbDriver;
private java.lang.String dbURL;
private java.lang.String sqlString;
private java.sql.ResultSet result;
private java.sql.ResultSetMetaData meta;
private java.sql.Connection conn;
private java.sql.Statement sqlStmt;
private java.lang.String drcName;
private final static java.lang.String className = "dbconnection.DBConnection";
private java.lang.String initialContext;
/**
* DBConnection constructor comment.
*/
public DBConnection (){
super ();
}
/**
* Insert the method's description here.
* Creation date: (10/31/00 3:44:45 PM)
*/
public void close (){
String methodName = "close()";
DBConnection.closeResultSetAndStatement (sqlStmt, result);
DBConnection.closeConnection (conn);
}
/**
* Insert the method's description here.
* Creation date: (7/25/2003 5:34:40 PM)
*/
public void closeStatement (){
try{
if (sqlStmt != null){
sqlStmt.clearBatch ();
sqlStmt.clearWarnings ();
sqlStmt.close ();
}
} catch (SQLException e){
}
}
/**
* Insert the method's description here.
* Creation date: (11/2/00 10:51:57 AM)
* @exception java.sql.SQLException The exception description.
*/
public void commit (){
String methodName = "commit()";
try{
if (conn != null && !conn.isClosed ()){
conn.commit ();
}
} catch (SQLException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
}
}
/**
* Insert the method's description here.
* Creation date: (10/6/00 4:04:34 PM)
*/
public void connect (){
String methodName = "connect()";
try{
if (conn == null){
if (!registerDriver (dbDriver)){
throw new SQLException ("Can't load JDBC Driver - " + dbDriver + ".");
}
/*
InitialContext context = new InitialContext ();
DataSource ds = (DataSource)context.lookup (drcName);
*/
DataSource ds = setupDataSource ();
Connection connection = ds.getConnection ();
conn = connection;
}
} catch (SQLException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
}
/*
catch (NamingException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
}
*/
}
/**
* Insert the method's description here.
* Creation date: (10/16/00 2:17:02 PM)
* @return dbconnection.DBConnection
*/
public static DBConnection dbconnect (){
String methodName = "dbconnect(String, String)";
DBConnection dbconn = null;
try{
// initialize dbconnection
dbconn = new DBConnection ();
// Get DB connection parameters from central getDBConnectionParm bean
getParm getDBConnectionParm = new getParm ();
getDBConnectionParm.init ();
dbconn.setDbUserID (getDBConnectionParm.getParameter ("userID"));
// out.println("After setDbUserID");
dbconn.setDbPassword (getDBConnectionParm.getParameter ("password"));
dbconn.setDbURL (getDBConnectionParm.getParameter ("URL"));
dbconn.setDbDriver (getDBConnectionParm.getParameter ("driver"));
dbconn.setDrcName (getDBConnectionParm.getParameter (
"ConnectionPoolLocation"));
dbconn.setInitialContext (getDBConnectionParm.getParameter (
"initial_context"));
System.out.println (dbconn.getDbUserID () + ", " + dbconn.getDbPassword () +
", " + dbconn.getDbURL () + ", " + dbconn.getDbDriver () +
", " + dbconn.getDrcName () + ", " +
dbconn.getInitialContext ());
dbconn.connect ();
System.out.println ("After dbconn.connect()");
} catch (SQLException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
} catch (Exception e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
e.printStackTrace ();
}
return dbconn;
}
public static DBConnection dbconnect (String filename){
String methodName = "dbconnect(String, String)";
DBConnection dbconn = null;
try{
// initialize dbconnection
dbconn = new DBConnection ();
// Get DB connection parameters from central getDBConnectionParm bean
getParm getDBConnectionParm = new getParm ();
getDBConnectionParm.init (filename);
dbconn.setDbUserID (getDBConnectionParm.getParameter ("userID"));
// out.println("After setDbUserID");
dbconn.setDbPassword (getDBConnectionParm.getParameter ("password"));
dbconn.setDbURL (getDBConnectionParm.getParameter ("URL"));
dbconn.setDbDriver (getDBConnectionParm.getParameter ("driver"));
dbconn.setDrcName (getDBConnectionParm.getParameter (
"ConnectionPoolLocation"));
dbconn.setInitialContext (getDBConnectionParm.getParameter (
"initial_context"));
System.out.println (dbconn.getDbUserID () + ", " + dbconn.getDbPassword () +
", " + dbconn.getDbURL () + ", " + dbconn.getDbDriver () +
", " + dbconn.getDrcName () + ", " +
dbconn.getInitialContext ());
dbconn.connect ();
System.out.println ("After dbconn.connect()");
} catch (SQLException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
} catch (Exception e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
}
return dbconn;
}
/**
* This method have Response Object to allow you to print some information on the browser.
* Creation date: (9/29/00 4:35:14 PM)
*/
private void execute (String sqltype) throws SQLException{
if (conn != null && !conn.isClosed ()){
sqlStmt = conn.createStatement ();
sqlStmt.executeQuery (getSqlString ());
if (sqltype.equals ("s") || sqltype.equals ("S")){
result = sqlStmt.getResultSet ();
meta = result.getMetaData ();
}
}
}
/**
* This method have Response Object to allow you to print some information on the browser.
* Creation date: (9/29/00 4:35:14 PM)
*/
public void execute (String sqlString, String sqlType) throws SQLException{
this.sqlString = sqlString;
execute (sqlType);
}
/**
* Insert the method's description here.
* Creation date: (10/6/00 4:04:06 PM)
* @return java.sql.Connection
*/
public java.sql.Connection getConn (){
return conn;
}
/**
* return the driver of db
* Creation date: (9/29/00 4:29:45 PM)
* @return java.lang.String
*/
public java.lang.String getDbDriver (){
return dbDriver;
}
/**
* return the password of db
* Creation date: (9/29/00 4:29:25 PM)
* @return java.lang.String
*/
public java.lang.String getDbPassword (){
return dbPassword;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 4:34:01 PM)
* @return java.lang.String
*/
public java.lang.String getDbURL (){
return dbURL;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 4:29:04 PM)
* @return java.lang.String
*/
public java.lang.String getDbUserID (){
return dbUserID;
}
/**
* Insert the method's description here.
* Creation date: (7/17/2002 12:17:51 PM)
* @return java.lang.String
*/
public java.lang.String getDrcName (){
return drcName;
}
/**
* Insert the method's description here.
* Creation date: (10/8/02 10:07:36 AM)
* @return java.lang.String
*/
public java.lang.String getInitialContext (){
return initialContext;
}
/**
* Insert the method's description here.
* Creation date: (10/2/00 1:35:04 PM)
* @return java.sql.ResultSetMetaData
*/
public java.sql.ResultSetMetaData getMeta (){
return meta;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 5:05:13 PM)
* @return java.sql.ResultSet
*/
public java.sql.ResultSet getResult (){
return result;
}
/**
* Insert the method's description here.
* Creation date: (10/6/00 4:06:40 PM)
* @return java.sql.Statement
*/
public java.sql.Statement getSqlStmt (){
return sqlStmt;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 4:55:48 PM)
* @return java.lang.String
*/
public java.lang.String getSqlString (){
return sqlString;
}
/**
* Insert the method's description here.
* Creation date: (7/18/02 5:05:30 PM)
*/
public void rollback (){
try{
if (conn != null && !conn.isClosed ()){
conn.rollback ();
}
} catch (SQLException e){
}
}
/**
* Insert the method's description here.
* Creation date: (7/18/02 5:04:34 PM)
* @param autoCommit boolean
*/
public void setAutoCommit (boolean autoCommit){
String methodName = "setAutoCommit()";
try{
if (conn != null && !conn.isClosed ()){
conn.setAutoCommit (autoCommit);
}
} catch (SQLException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
}
}
/**
* Insert the method's description here.
* Creation date: (10/6/00 4:04:06 PM)
* @param newConn java.sql.Connection
*/
public void setConn (java.sql.Connection newConn){
conn = newConn;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 4:29:45 PM)
* @param newDbDriver java.lang.String
*/
public void setDbDriver (java.lang.String newDbDriver){
dbDriver = newDbDriver;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 4:29:25 PM)
* @param newDbPassword java.lang.String
*/
public void setDbPassword (java.lang.String newDbPassword){
dbPassword = newDbPassword;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 4:34:01 PM)
* @param newDbURL java.lang.String
*/
public void setDbURL (java.lang.String newDbURL){
dbURL = newDbURL;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 4:29:04 PM)
* @param newDbUserID java.lang.String
*/
public void setDbUserID (java.lang.String newDbUserID){
dbUserID = newDbUserID;
}
/**
* Insert the method's description here.
* Creation date: (7/17/2002 12:17:51 PM)
* @param newDrcName java.lang.String
*/
public void setDrcName (java.lang.String newDrcName){
drcName = newDrcName;
}
/**
* Insert the method's description here.
* Creation date: (10/8/02 10:07:36 AM)
* @param newInitialContext java.lang.String
*/
public void setInitialContext (java.lang.String newInitialContext){
initialContext = newInitialContext;
}
/**
* Insert the method's description here.
* Creation date: (10/2/00 1:35:04 PM)
* @param newMeta java.sql.ResultSetMetaData
*/
public void setMeta (java.sql.ResultSetMetaData newMeta){
meta = newMeta;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 5:05:13 PM)
* @param newResult java.sql.ResultSet
*/
void setResult (java.sql.ResultSet newResult){
result = newResult;
}
/**
* Insert the method's description here.
* Creation date: (10/6/00 4:06:40 PM)
* @param newSqlStmt java.sql.Statement
*/
public void setSqlStmt (java.sql.Statement newSqlStmt){
sqlStmt = newSqlStmt;
}
/**
* Insert the method's description here.
* Creation date: (9/29/00 4:55:48 PM)
* @param newSqlString java.lang.String
*/
private void setSqlString (java.lang.String newSqlString){
sqlString = newSqlString;
}
public DataSource setupDataSource (){
BasicDataSource ds = new BasicDataSource ();
ds.setDriverClassName (dbDriver);
ds.setUsername (dbUserID);
ds.setPassword (dbPassword);
ds.setUrl (dbURL);
return ds;
}
public boolean registerDriver (String driverName){
return DbUtils.loadDriver (driverName);
}
public static void closeResultSetAndStatement (java.sql.ResultSet rs){
String methodName = "closeResultSetAndStatement(java.sql.ResultSet)";
java.sql.Statement stmt = null;
try{
stmt = rs.getStatement ();
} catch (java.sql.SQLException e){
System.err.println ("com.netstarmacau.sampleorder.util.DbConnection.closeResultSetAndStatement(Resultset) ==> " +
e.getMessage ());
} finally{
closeResultSetAndStatement (stmt, rs);
}
}
public static void closeResultSetAndStatement (java.sql.Statement stmt){
String methodName = "closeResultSetAndStatement(java.sql.Statement)";
java.sql.ResultSet rs = null;
try{
rs = stmt.getResultSet ();
} catch (java.sql.SQLException e){
System.err.println ("com.netstarmacau.sampleorder.util.DbConnection.closeResultSetAndStatement(Resultset) ==> " +
e.getMessage ());
} finally{
closeResultSetAndStatement (stmt, rs);
}
}
public static void closeResultSetAndStatement (java.sql.Statement stmt,
java.sql.ResultSet rs){
String methodName =
"closeResultSetAndStatement(java.sql.Statement, ava.sql.ResultSet)";
closeResultSet (rs);
closeStatement (stmt);
}
public static void closeStatement (java.sql.Statement stmt){
DbUtils.closeQuietly (stmt);
}
public static void closeResultSet (java.sql.ResultSet rs){
DbUtils.closeQuietly (rs);
}
public static void closeConnection (Connection conn){
DbUtils.closeQuietly (conn);
}
public static void closeAll (Connection conn, Statement stmt, ResultSet rs){
DbUtils.closeQuietly (conn, stmt, rs);
}
public static void closeAll (DBConnection dbconn, Statement stmt,
ResultSet rs){
if (dbconn != null){
dbconn.close ();
}
closeResultSetAndStatement (stmt, rs);
}
public static void commit (Connection conn){
String methodName = "commit()";
try{
if (conn != null && !conn.isClosed ()){
conn.commit ();
}
} catch (SQLException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
}
}
public static void setAutoCommit (Connection conn, boolean autoCommit){
String methodName = "setAutoCommit()";
try{
if (conn != null && !conn.isClosed ()){
conn.setAutoCommit (autoCommit);
}
} catch (SQLException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
}
}
public static void rollback (Connection conn){
String methodName = "rollback()";
try{
if (conn != null && !conn.isClosed ()){
conn.rollback ();
}
} catch (SQLException e){
System.out.println (className + "." + methodName + " ==> " +
e.getClass ().getName () + ": " + e.getMessage ());
}
}
public static void closePrePareStatement (java.sql.PreparedStatement pstmt){
DbUtils.closeQuietly (pstmt);
}
}