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

📄 statedaoimpl.java

📁 J2EE & Tomcat books published by hope
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            closeConnection();        }    }    private StateModel selectstate(String stateId) throws                                         StateDAOSysException,                                         StateDAOFinderException {        PreparedStatement stmt = null;        ResultSet result = null;        String queryStr = "SELECT *"+                    " FROM " + DatabaseNames.TASK_TABLE +                        " WHERE TASK_ID = " + "'" + stateId.trim() + "'";        Debug.println("queryString is: "+ queryStr);        try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            result = stmt.executeQuery();            if ( !result.next() )                throw new StateDAOFinderException(                                  "No record for primary key " + stateId);            int i = 1;            String state_ID = result.getString(i++);            String state_name = result.getString(i++);            Date plan_start_dt = result.getDate(i++);            Date plan_end_dt = result.getDate(i++);            Date act_start_dt = result.getDate(i++);            Date act_end_dt = result.getDate(i++);            String status = result.getString(i++);            String predecessor = result.getString(i++);            String project = result.getString(i++);            String state_desc = result.getString(i++);            String type = result.getString(i++);            String comment = result.getString(i++);                        return(new StateModel(state_ID, state_name, plan_start_dt, plan_end_dt, act_start_dt, act_end_dt, status,  project, state_desc, comment ));        } catch(SQLException ae) {            throw new StateDAOSysException("SQLException while getting " +                      "state; id = " + stateId + " :\n" + ae);        } finally {            closeResultSet(result);            closeStatement(stmt);            closeConnection();        }    }    private void deletestate (String stateId) throws                                         StateDAODBUpdateException,                                         StateDAOSysException {        String queryStr = "DELETE FROM " + DatabaseNames.TASK_TABLE        + " WHERE state_ID = " + "'" + stateId.trim() + "'";        PreparedStatement stmt = null;        Debug.println("queryString is: "+ queryStr);        try {            getDBConnection();            //stmt = dbConnection.createStatement();            //int resultCount = stmt.executeUpdate(queryStr);            stmt = createPreparedStatement(dbConnection, queryStr);            int resultCount = stmt.executeUpdate();            if (resultCount != 1)                throw new StateDAODBUpdateException                ("ERROR deleteing state from TASK_TABLE!! resultCount = "+                 resultCount);        } catch(SQLException se) {            throw new StateDAOSysException("SQLException while removing " +                            "state; id = " + stateId + " :\n" + se);        } finally {            closeStatement(stmt);            closeConnection();        }    }    private void updatestate(StateModel stateinfo) throws                                         StateDAODBUpdateException,                                         StateDAOAppException,                                         StateDAOSysException {        if (!isValidData(stateinfo))            throw new StateDAOAppException("Illegal data values for update");        String queryStr = "UPDATE " + DatabaseNames.TASK_TABLE + " SET "            +"state_name = " + "'" + stateinfo.getState_name().trim() + "',"            +"plan_start_dt = " + "'" + stateinfo.getPlan_start_dt().toString() + "',"            +"plan_end_dt = " + "'" + stateinfo.getPlan_end_dt().toString() + "',"            +"act_start_dt = " + "'" + stateinfo.getAct_start_dt().toString() + "',"            +"act_end_dt = " + "'" + stateinfo.getAct_end_dt().toString() + "',"            +"status = " + "'" + stateinfo.getStatus().trim() + "',"            //+"predecessor = " + "'" + stateinfo.getPredecessor().trim() + "',"            +"project = " + "'" + stateinfo.getProject().trim() + "',"            +"state_desc = " + "'" + stateinfo.getState_desc().trim() + "',"            //+"type = " + "'" + stateinfo.getType().trim() + "',"            +"comment = " + "'" + stateinfo.getComment().trim() + "'"            + " WHERE state_ID = " + "'" + stateinfo.getState_ID().trim() + "'";        Debug.println("queryString is: "+ queryStr);        PreparedStatement stmt = null;        try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            int resultCount = stmt.executeUpdate();            if (resultCount != 1)                throw new StateDAODBUpdateException                ("ERROR updating state in TASK_TABLE!! resultCount = " +                 resultCount);        } catch(SQLException se) {            throw new StateDAOSysException("SQLException while updating " +                     "state; id = " + stateinfo.getState_ID() + " :\n" + se);        } finally {            closeStatement(stmt);            closeConnection();        }    }    private void getDBConnection() throws StateDAOSysException {        try {            dbConnection = datasource.getConnection();        } catch (SQLException se) {            throw new StateDAOSysException("SQL Exception while getting " +                                "DB connection : \n" + se);        }        return;    }    private void closeConnection() throws StateDAOSysException {        try {            if (dbConnection != null && !dbConnection.isClosed()) {                dbConnection.close();        }        } catch (SQLException se) {            throw new StateDAOSysException("SQL Exception while closing " +                                        "DB connection : \n" + se);        }    }    private void closeResultSet(ResultSet result) throws StateDAOSysException {        try {            if (result != null) {                result.close();            }        } catch (SQLException se) {            throw new StateDAOSysException("SQL Exception while closing " +                                        "Result Set : \n" + se);        }    }    private void closeStatement(PreparedStatement stmt) throws StateDAOSysException {        try {            if (stmt != null) {                stmt.close();            }        } catch (SQLException se) {            throw new StateDAOSysException("SQL Exception while closing " +                                        "Statement : \n" + se);        }    }    /**     * This method allows us to create a prepared search statement that will be friendly     * To Japanese in cloudscape and other databases.     * Basically we use a prepared statement that contants '?' where Japanese characters     * may occur and then we use the stmt.setString(index, "search string")     *     * This technique should not affect the English searchs.     *    */    private PreparedStatement createPreparedStatement(Connection con, String querry)            throws SQLException {        ArrayList targetStrings = new ArrayList();        String processedQuerry = "";        int startIndex = 0;        if (startIndex != -1) {            int index = startIndex;            int literalStart = -1;            while (index < querry.length()) {                if (querry.charAt(index) == '\'') {                    if (literalStart == -1 && index + 1 < querry.length()) {                        literalStart = index +1;                    } else {                        String targetString = querry.substring(literalStart, index);                        targetStrings.add(targetString);                        literalStart = -1;                        processedQuerry += "?";                        index++;                    }                }               if (index < querry.length() && literalStart == -1) {                    processedQuerry += querry.charAt(index);                }                index++;            }            PreparedStatement stmt = con.prepareStatement(processedQuerry + " ");            Iterator it = targetStrings.iterator();            int counter =1;            while (it.hasNext()) {                String arg = (String)it.next();                stmt.setString(counter++, arg);            }            return stmt;        } else {            PreparedStatement stmt = con.prepareStatement(querry);            return stmt;        }    }}

⌨️ 快捷键说明

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