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

📄 projectdaoimpl.java~

📁 J2EE & Tomcat books published by hope
💻 JAVA~
📖 第 1 页 / 共 2 页
字号:
        + "'" + projectinfo.getDesc().trim() +"')";                Debug.println("queryString is: "+ queryStr);                try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            int resultCount = stmt.executeUpdate();                        if ( resultCount != 1 ) {                throw new ProjectDAODBUpdateException(                "ERROR in PROJECT_TABLE INSERT !! resultCount = " +                resultCount);            }        } catch(SQLException ae) {            throw new ProjectDAOSysException(            "SQLException while inserting new " +            "project; name = " + projectinfo.getProj_name() + " :\n" + ae);        } finally {            closeStatement(stmt);            closeConnection();        }    }        private ProjectModel selectproject(String projectId) throws    ProjectDAOSysException,    ProjectDAOFinderException {                PreparedStatement stmt = null;        ResultSet result = null;                String queryStr = "SELECT *"+        " FROM " + DatabaseNames.PROJECT_TABLE +        " WHERE PROJ_ID = " + "'" + projectId.trim() + "'";        Debug.println("queryString is: "+ queryStr);                try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            result = stmt.executeQuery();            if ( !result.next() )                throw new ProjectDAOFinderException(                "No record for primary key " + projectId);                        int i = 1;            String proj_ID = result.getString(i++);            String proj_name = result.getString(i++);            String group = result.getString(i++);            String lead = result.getString(i++);            String desc = result.getString(i++);            return(new ProjectModel(proj_ID, proj_name,  group,  lead, desc ));        } catch(SQLException ae) {            throw new ProjectDAOSysException("SQLException while getting " +            "project; id = " + projectId + " :\n" + ae);        } finally {            closeResultSet(result);            closeStatement(stmt);            closeConnection();        }    }        private void deleteproject(String projectId) throws    ProjectDAODBUpdateException,    ProjectDAOSysException {        String queryStr = "DELETE FROM " + DatabaseNames.PROJECT_TABLE        + " WHERE proj_ID = " + "'" + projectId.trim() + "'";        PreparedStatement stmt = null;        Debug.println("queryString is: "+ queryStr);                try {            getDBConnection();            //stmt = dbConnection.createStatement();            //int resultCount = stmt.executeUpdate(queryStr);            //here need add queries to delete all of the task and assignement of the project            stmt = createPreparedStatement(dbConnection, queryStr);            int resultCount = stmt.executeUpdate();                        if (resultCount != 1)                throw new ProjectDAODBUpdateException                ("ERROR deleteing project from PROJECT_TABLE!! resultCount = "+                resultCount);        } catch(SQLException se) {            throw new ProjectDAOSysException("SQLException while removing " +            "project; id = " + projectId + " :\n" + se);        } finally {            closeStatement(stmt);            closeConnection();        }    }        private void updateproject(ProjectModel projectinfo) throws    ProjectDAODBUpdateException,    ProjectDAOAppException,    ProjectDAOSysException {        if (!isValidData(projectinfo))            throw new ProjectDAOAppException("Illegal data values for update");                String queryStr = "UPDATE " + DatabaseNames.PROJECT_TABLE + " SET "        +"proj_name = " + "'" + projectinfo.getProj_name().trim() + "',"        +"group = " + "'" + projectinfo.getGroup().trim() + "',"        +"lead = " + "'" + projectinfo.getLead().trim() + "',"        +"desc = " + "'" + projectinfo.getDesc().trim() +"'"        + " WHERE proj_ID = " + "'" + projectinfo.getProj_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 ProjectDAODBUpdateException                ("ERROR updating project in PROJECT_TABLE!! resultCount = " +                resultCount);        } catch(SQLException se) {            throw new ProjectDAOSysException("SQLException while updating " +            "project; id = " + projectinfo.getProj_ID() + " :\n" + se);        } finally {            closeStatement(stmt);            closeConnection();        }    }        private void getDBConnection() throws ProjectDAOSysException {        try {            dbConnection = datasource.getConnection();        } catch (SQLException se) {            throw new ProjectDAOSysException("SQL Exception while getting " +            "DB connection : \n" + se);        }        return;    }        private void closeConnection() throws ProjectDAOSysException {        try {            if (dbConnection != null && !dbConnection.isClosed()) {                dbConnection.close();            }        } catch (SQLException se) {            throw new ProjectDAOSysException("SQL Exception while closing " +            "DB connection : \n" + se);        }    }        private void closeResultSet(ResultSet result) throws ProjectDAOSysException {        try {            if (result != null) {                result.close();            }        } catch (SQLException se) {            throw new ProjectDAOSysException("SQL Exception while closing " +            "Result Set : \n" + se);        }    }        private void closeStatement(PreparedStatement stmt) throws ProjectDAOSysException {        try {            if (stmt != null) {                stmt.close();            }        } catch (SQLException se) {            throw new ProjectDAOSysException("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;        }    }        private String generateID() throws  ProjectDAOSysException, ProjectDAOFinderException {                PreparedStatement stmt = null;        ResultSet result = null;        int lastint = 0;        String queryStr = "SELECT PROJ_ID"+        " FROM " + DatabaseNames.PROJECT_TABLE;        Debug.println("queryString is: "+ queryStr);                try {            getDBConnection();            stmt = createPreparedStatement(dbConnection, queryStr);            result = stmt.executeQuery();            while (result.next()) {                String lastproj_ID = result.getString(1);                int last = Integer.valueOf(lastproj_ID).intValue();                if (last>lastint) lastint = last;            }                        if (lastint == -1) {                throw new ProjectDAOFinderException(                "No record for generateID ");            }            return( String.valueOf(lastint+1) );        } catch(SQLException ae) {            throw new ProjectDAOSysException("SQLException while getting last ID " + ae);        } finally {            closeResultSet(result);            closeStatement(stmt);            closeConnection();        }    }}

⌨️ 快捷键说明

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