⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statedaoimpl.java

📁 J2EE & Tomcat books published by hope
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* */package com.sun.j2ee.workflow.state.dao;import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Iterator;import javax.sql.DataSource;import javax.naming.InitialContext;import javax.naming.Context;import javax.naming.NamingException;import com.sun.j2ee.workflow.util.JNDINames;import com.sun.j2ee.workflow.state.dao.StateDAO;import com.sun.j2ee.workflow.util.DatabaseNames;import com.sun.j2ee.workflow.state.model.StateModel;import com.sun.j2ee.workflow.state.exceptions.StateDAOSysException;import com.sun.j2ee.workflow.state.exceptions.StateDAOAppException;import com.sun.j2ee.workflow.state.exceptions.StateDAODBUpdateException;import com.sun.j2ee.workflow.state.exceptions.StateDAOFinderException;import com.sun.j2ee.workflow.state.exceptions.StateDAODupKeyException;import com.sun.j2ee.workflow.util.Debug;/** * This class calls the simple Datasource from Tomcat to get connection * No connection pooling is used. */public class StateDAOImpl implements StateDAO {    private transient Connection dbConnection = null;    private transient DataSource datasource   = null;    public StateDAOImpl() throws StateDAOSysException {        try {            InitialContext ic = new InitialContext();            datasource = (DataSource) ic.lookup(JNDINames.WORKFLOW_DATASOURCE);        } catch (NamingException ne) {            throw new StateDAOSysException("Naming Exception while looking "                                             + " up DataSource Connection " +                                                JNDINames.WORKFLOW_DATASOURCE +                                                    ": \n" + ne.getMessage());        }    }/*    public void query{                // load the PoolMan JDBC Driver        try {            Class.forName("com.codestudio.sql.PoolMan").newInstance();        } catch (Exception ex) {            System.out.println("Could Not Find the PoolMan Driver. " +                               "Is poolman.jar in your CLASSPATH?");            System.exit(0);        }        // establish a Connection to the last database listed in the 'poolman.props' file        Connection con = DriverManager.getConnection("jdbc:poolman");        try {            Statement s = con.createStatement();            ResultSet res = s.executeQuery(sql);            ResultSetMetaData meta = res.getMetaData();            int cols = meta.getColumnCount();            while (res.next()) {                for (int i = 1; i <= cols; i++) {                    Object val = res.getObject(i);                    System.out.print("\t" + meta.getColumnLabel(i) + ": ");                    System.out.print(val == null ? " " : val.toString());                }                System.out.print("\n");            }        } catch (SQLException sqe) {        }        finally {            // this close method merely returns the Connection to the pool            // after implicitly closing related resources (Statements and ResultSets)            con.close();            System.out.println("SAMPLE: Closed Con");        }    } */    public void create(StateModel stateinfo) throws StateDAOSysException,                                StateDAODupKeyException,                                StateDAODBUpdateException,                                StateDAOAppException {        insertstate(stateinfo);    }    public StateModel load(String id) throws StateDAOSysException,                              StateDAOFinderException {        return(selectstate(id));    }    public void store(StateModel stateinfo) throws StateDAODBUpdateException,                               StateDAOAppException,                               StateDAOSysException  {        updatestate(stateinfo);    }    public void remove(String id) throws StateDAODBUpdateException,                                StateDAOSysException {        deletestate(id);    }    public String findByPrimaryKey(String stateId) throws                                            StateDAOFinderException,                                            StateDAOSysException {        if (stateExists(stateId))            return (stateId);        throw new StateDAOFinderException("primary key not found :"+stateId);    }    private boolean stateExists (String stateId) throws StateDAOSysException {        PreparedStatement stmt = null;        ResultSet result = null;        boolean returnValue = false;        String queryStr ="SELECT state_ID FROM " +                    DatabaseNames.TASK_TABLE                        + " WHERE state_ID = " + "'" + stateId.trim() + "'";        Debug.println("queryString is: "+ queryStr);        try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            result = stmt.executeQuery();            if ( !result.next() ) {                returnValue = false;            } else {                stateId = result.getString(1);                returnValue = true;            }        } catch(SQLException se) {            throw new StateDAOSysException(                           "SQLException while checking for an"                           + " existing state - id -> " + stateId + " :\n" + se);        } finally {            closeResultSet(result);            closeStatement(stmt);            closeConnection();        }        return returnValue;    }    private boolean isValidData(StateModel stateinfo) {        if ( (stateinfo.getState_ID() == null) ||             (stateinfo.getState_name() == null) ||              (stateinfo.getProject() == null) )            return (false);        else            return (true);    }    private void insertstate(StateModel stateinfo) throws                                 StateDAOSysException,                                 StateDAODupKeyException,                                 StateDAODBUpdateException,                                 StateDAOAppException {        if (!isValidData(stateinfo))            throw new StateDAOAppException("Illegal data values for insert");        if (stateExists(stateinfo.getState_ID()))            throw new StateDAODupKeyException("state exists for "+                                                stateinfo.getState_ID());        PreparedStatement stmt = null;        String queryStr = "INSERT INTO " + DatabaseNames.TASK_TABLE + "VALUES ("            + "'" + stateinfo.getState_ID().trim() + "',"            + "'" + stateinfo.getState_name().trim() + "',"            + "'" + stateinfo.getPlan_start_dt().toString() + "',"            + "'" + stateinfo.getPlan_end_dt().toString() + "',"            + "'" + stateinfo.getAct_start_dt().toString() + "',"            + "'" + stateinfo.getAct_end_dt().toString() + "',"            + "'" + stateinfo.getStatus().trim() + "',"           // + "'" + stateinfo.getPredecessor().trim() + "',"            + "'" + stateinfo.getProject().trim() + "',"            + "'" + stateinfo.getState_desc().trim() + "',"           // + "'" + stateinfo.getType().trim() + "',"            + "'" + stateinfo.getComment().trim() + "'";         Debug.println("queryString is: "+ queryStr);        try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            int resultCount = stmt.executeUpdate();            if ( resultCount != 1 ) {                throw new StateDAODBUpdateException(                    "ERROR in TASK_TABLE INSERT !! resultCount = " +                                   resultCount);            }        } catch(SQLException ae) {            throw new StateDAOSysException(                        "SQLException while inserting new " +                        "state; id = " + stateinfo.getState_ID() + " :\n" + ae);        } finally {            closeStatement(stmt);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -