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

📄 assignmentdaoimpl.java~

📁 J2EE & Tomcat books published by hope
💻 JAVA~
📖 第 1 页 / 共 2 页
字号:
        } catch(SQLException ae) {            throw new AssignmentDAOSysException("SQLException while getting " +                      "assignment; id = " + assignmentId + " :\n" + ae);        } finally {            closeResultSet(result);            closeStatement(stmt);            closeConnection();        }    }    private void deleteassignment (String assignmentId) throws                                         AssignmentDAODBUpdateException,                                         AssignmentDAOSysException {        String queryStr = "DELETE FROM " + DatabaseNames.ASSIGNMENT_TABLE        + " WHERE assign_ID = " + "'" + assignmentId.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 AssignmentDAODBUpdateException                ("ERROR deleteing assignment from ASSIGNMENT_TABLE!! resultCount = "+                 resultCount);        } catch(SQLException se) {            throw new AssignmentDAOSysException("SQLException while removing " +                            "assignment; id = " + assignmentId + " :\n" + se);        } finally {            closeStatement(stmt);            closeConnection();        }    }    private void updateassignment(AssignmentModel assignmentinfo) throws                                         AssignmentDAODBUpdateException,                                         AssignmentDAOAppException,                                         AssignmentDAOSysException {        if (!isValidData(assignmentinfo))            throw new AssignmentDAOAppException("Illegal data values for update");        String queryStr = "UPDATE " + DatabaseNames.ASSIGNMENT_TABLE + " SET "            +"assign_name = " + "'" + assignmentinfo.getAssign_name().trim() + "',"            +"task = " + "'" + assignmentinfo.getTask().trim() + "',"            +"user = " + "'" + assignmentinfo.getUser().trim() + "',"            +"desc = " + "'" + assignmentinfo.getDesc().trim() + "',"            +"comment = " + "'" + assignmentinfo.getComment().trim() + "'"            + " WHERE assign_ID = " + "'" + assignmentinfo.getAssign_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 AssignmentDAODBUpdateException                ("ERROR updating assignment in ASSIGNMENT_TABLE!! resultCount = " +                 resultCount);        } catch(SQLException se) {            throw new AssignmentDAOSysException("SQLException while updating " +                     "assignment; id = " + assignmentinfo.getAssign_ID() + " :\n" + se);        } finally {            closeStatement(stmt);            closeConnection();        }    }    private void getDBConnection() throws AssignmentDAOSysException {        try {            dbConnection = datasource.getConnection();        } catch (SQLException se) {            throw new AssignmentDAOSysException("SQL Exception while getting " +                                "DB connection : \n" + se);        }        return;    }    private void closeConnection() throws AssignmentDAOSysException {        try {            if (dbConnection != null && !dbConnection.isClosed()) {                dbConnection.close();        }        } catch (SQLException se) {            throw new AssignmentDAOSysException("SQL Exception while closing " +                                        "DB connection : \n" + se);        }    }    private void closeResultSet(ResultSet result) throws AssignmentDAOSysException {        try {            if (result != null) {                result.close();            }        } catch (SQLException se) {            throw new AssignmentDAOSysException("SQL Exception while closing " +                                        "Result Set : \n" + se);        }    }    private void closeStatement(PreparedStatement stmt) throws AssignmentDAOSysException {        try {            if (stmt != null) {                stmt.close();            }        } catch (SQLException se) {            throw new AssignmentDAOSysException("SQL Exception while closing " +                                        "Statement : \n" + se);        }    }        public Collection findAll() throws    AssignmentDAOFinderException,    AssignmentDAOSysException {        PreparedStatement stmt = null;        ResultSet result = null;        AssignmentModel tmodel = null;        boolean returnValue = false;        ArrayList results = new ArrayList();        String queryStr ="SELECT * FROM " +        DatabaseNames.ASSIGNMENT_TABLE;        Debug.println("queryString is: "+ queryStr);        try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            result = stmt.executeQuery();            while(result.next()) {                                tmodel = new AssignmentModel(result.getString(1),result.getString(2), result.getString(3),result.getString(4),                                       result.getString(5), result.getString(6));                results.add(tmodel);            }        } catch(SQLException se) {            throw new AssignmentDAOSysException(            "SQLException while selecting"            + " existing all assignment  -> "  + " :\n" + se);        } finally {            closeResultSet(result);            closeStatement(stmt);            closeConnection();        }        return (results);    }    public Collection findByField(String fieldname, String fieldkey) throws    AssignmentDAOFinderException,    AssignmentDAOSysException {        PreparedStatement stmt = null;        ResultSet result = null;        AssignmentModel tmodel = null;        boolean returnValue = false;        ArrayList results = new ArrayList();        String queryStr ="SELECT * FROM " +        DatabaseNames.ASSIGNMENT_TABLE        + " WHERE "+fieldname+" = " + "'" + fieldkey.trim() + "'";        Debug.println("queryString is: "+ queryStr);                try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            result = stmt.executeQuery();            while(result.next()) {                                tmodel = new AssignmentModel(result.getString(1),result.getString(2), result.getString(3),result.getString(4),                                       result.getString(5), result.getString(6));                results.add(tmodel);            }        } catch(SQLException se) {            throw new AssignmentDAOSysException(            "SQLException while selecting"            + " existing task " + fieldname + " -> " + fieldkey + " :\n" + se);        } finally {            closeResultSet(result);            closeStatement(stmt);            closeConnection();        }        return (results);    }    /**     * 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 + -