?? assignmentdaoimpl.java~
字號:
} 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 + -