📄 projectdaoimpl.java~
字号:
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 USER_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++); String phone = result.getString(i++); String email = result.getString(i++); String title = result.getString(i++); String role = 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); 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; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -