📄 userdaoimpl.java
字号:
String password = result.getString(i++); String f_name = result.getString(i++); String l_name = result.getString(i++); String location = 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 UserModel(user_ID, password, f_name, l_name, location, phone, email, title, role)); } catch(SQLException ae) { throw new UserDAOSysException("SQLException while getting " + "user; id = " + userId + " :\n" + ae); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } } private void deleteuser (String userId) throws UserDAODBUpdateException, UserDAOSysException { String queryStr = "DELETE FROM " + DatabaseNames.USER_TABLE + " WHERE user_ID = " + "'" + userId.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 UserDAODBUpdateException ("ERROR deleteing user from USER_TABLE!! resultCount = "+ resultCount); } catch(SQLException se) { throw new UserDAOSysException("SQLException while removing " + "user; id = " + userId + " :\n" + se); } finally { closeStatement(stmt); closeConnection(); } } private void updateuser(UserModel userinfo) throws UserDAODBUpdateException, UserDAOAppException, UserDAOSysException { if (!isValidData(userinfo)) throw new UserDAOAppException("Illegal data values for update"); String queryStr = "UPDATE " + DatabaseNames.USER_TABLE + " SET " +"password = " + "'" + userinfo.getPassword().trim() + "'," +"f_name = " + "'" + userinfo.getF_name().trim() + "'," +"l_name = " + "'" + userinfo.getL_name().trim() + "'," +"location = " + "'" + userinfo.getLocation().trim() + "'," +"phone = " + "'" + userinfo.getPhone().trim() +"'," +"email = " + "'" + userinfo.getEmail().trim() +"'," +"title = " + "'" + userinfo.getTitle().trim() +"'," +"role = " + "'" + userinfo.getRole().trim() +"'" + " WHERE user_ID = " + "'" + userinfo.getUser_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 UserDAODBUpdateException ("ERROR updating user in USER_TABLE!! resultCount = " + resultCount); } catch(SQLException se) { throw new UserDAOSysException("SQLException while updating " + "user; id = " + userinfo.getUser_ID() + " :\n" + se); } finally { closeStatement(stmt); closeConnection(); } } private void getDBConnection() throws UserDAOSysException { try { Debug.println("getDBConnection"); dbConnection = datasource.getConnection(); Debug.println(" after getDBConnection"); } catch (SQLException se) { System.out.println(se); se.printStackTrace(System.out); throw new UserDAOSysException("SQL Exception while getting " + "DB connection : \n" + se); } return; } private void closeConnection() throws UserDAOSysException { try { if (dbConnection != null && !dbConnection.isClosed()) { dbConnection.close(); } } catch (SQLException se) { throw new UserDAOSysException("SQL Exception while closing " + "DB connection : \n" + se); } } private void closeResultSet(ResultSet result) throws UserDAOSysException { try { if (result != null) { result.close(); } } catch (SQLException se) { throw new UserDAOSysException("SQL Exception while closing " + "Result Set : \n" + se); } } private void closeStatement(PreparedStatement stmt) throws UserDAOSysException { try { if (stmt != null) { stmt.close(); } } catch (SQLException se) { throw new UserDAOSysException("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; } } public Collection findByField(String fieldname, String fieldkey) throws UserDAOFinderException, UserDAOSysException { PreparedStatement stmt = null; ResultSet result = null; UserModel umodel = null; boolean returnValue = false; ArrayList results = new ArrayList(); String queryStr ="SELECT * FROM " + DatabaseNames.USER_TABLE + " WHERE "+fieldname+" = " + "'" + fieldkey.trim() + "'"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); result = stmt.executeQuery(); while(result.next()) { umodel = new UserModel(result.getString(1),result.getString(2), result.getString(3),result.getString(4), result.getString(5), result.getString(6),result.getString(7), result.getString(8), result.getString(9)); results.add(umodel); } } catch(SQLException se) { throw new UserDAOSysException( "SQLException while selecting" + " existing user " + fieldname + " -> " + fieldkey + " :\n" + se); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } return (results); } public Collection findAll() throws UserDAOFinderException, UserDAOSysException { PreparedStatement stmt = null; ResultSet result = null; UserModel umodel = null; boolean returnValue = false; ArrayList results = new ArrayList(); String queryStr ="SELECT * FROM " + DatabaseNames.USER_TABLE; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); result = stmt.executeQuery(); while(result.next()) { umodel = new UserModel(result.getString(1),result.getString(2), result.getString(3),result.getString(4), result.getString(5), result.getString(6),result.getString(7), result.getString(8), result.getString(9)); results.add(umodel); } } catch(SQLException se) { throw new UserDAOSysException( "SQLException while selecting" + " existing all User -> " + " :\n" + se); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } return (results); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -