?? bookcarddao.java
字號(hào):
} else if ("float".equals(fieldType)) { element.put(fieldName, String.valueOf(rs.getFloat(columnName))); } } list.add(element); } return list; } catch(SQLException e) { Debug.println(sql + "\n" + "SQLException while execute queryBySql mothod :\n" + e); throw new SysException("SQLException while execute queryBySql mothod :\n" + e); } finally { closeResultSet(rs); closeStatement(stmt); closeConnection(dbConnection); } } public static Collection queryBySql(String sql, int beginRow, int endRow) throws SysException { Connection dbConnection = null; Statement stmt = null; ResultSet rs = null; ArrayList list = new ArrayList(); try { dbConnection = getDBConnection(); stmt = dbConnection.createStatement(); rs = stmt.executeQuery(sql); ResultSetMetaData rsmd = rs.getMetaData(); int row = 0; while ( rs.next() ) { if (++row < beginRow) continue; if (row > endRow) break; Hashtable element = new Hashtable(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnName = rsmd.getColumnName(i); String fieldName = Function.format(columnName); String fieldType = ColumnType.getFieldType(Database.dbType, rsmd, i); if (fieldType == null) continue; if ("Timestamp".equals(fieldType)) { element.put(fieldName, rs.getTimestamp(columnName)); } else if ("String".equals(fieldType)) { String value = Function.readDBEncode(rs.getString(columnName)); if (value == null) value = ""; element.put(fieldName, value); } else if ("int".equals(fieldType)) { element.put(fieldName, String.valueOf(rs.getInt(columnName))); } else if ("long".equals(fieldType)) { element.put(fieldName, String.valueOf(rs.getLong(columnName))); } else if ("float".equals(fieldType)) { element.put(fieldName, String.valueOf(rs.getFloat(columnName))); } } list.add(element); } return list; } catch(SQLException e) { Debug.println(sql + "\n" + "SQLException while execute queryBySql mothod :\n" + e); throw new SysException("SQLException while execute queryBySql mothod :\n" + e); } finally { closeResultSet(rs); closeStatement(stmt); closeConnection(dbConnection); } } public static int getRowCountBySql(String sql) throws SysException { Connection dbConnection = null; Statement stmt = null; ResultSet rs = null; int rowCount = 0; try { dbConnection = getDBConnection(); stmt = dbConnection.createStatement(); rs = stmt.executeQuery(sql); if ( rs.next() ) { rowCount = rs.getInt(1); }else{ Debug.println(sql + "\n" + "ERROR in get row count!!"); throw new SysException("ERROR in get row count!!"); } return rowCount; } catch(SQLException e) { Debug.println(sql + "\n" + "SQLException while execute getRowCountBySql mothod :\n" + e); throw new SysException("SQLException while execute getRowCountBySql mothod :\n" + e); } finally { closeResultSet(rs); closeStatement(stmt); closeConnection(dbConnection); } } public static Collection queryAll() throws SysException { String sql = "SELECT * FROM WAP_BOOK_CARD"; return queryBySql(sql); } public static Collection queryAll(int beginRow, int endRow) throws SysException { String sql = "SELECT * FROM WAP_BOOK_CARD"; return queryBySql(sql, beginRow, endRow); } public static int getRowCountOfAll() throws SysException { String sql = "SELECT COUNT(*) FROM WAP_BOOK_CARD"; return getRowCountBySql(sql); } public static BookCardModel toModel(Hashtable element) { BookCardModel model = new BookCardModel(); model.setId(Long.parseLong((String)element.get("id"))); model.setUserMdn((String)element.get("userMdn")); model.setUserName((String)element.get("userName")); model.setUserJob((String)element.get("userJob")); model.setUserCom((String)element.get("userCom")); model.setUserTel((String)element.get("userTel")); model.setUserFax((String)element.get("userFax")); model.setUserEmail((String)element.get("userEmail")); model.setUserAdress((String)element.get("userAdress")); model.setUserPost((String)element.get("userPost")); return model; } public static Collection allToModel(Collection coll) { Collection value = new ArrayList(); Iterator it = coll.iterator(); while (it.hasNext()) { value.add(toModel((Hashtable)it.next())); } return value; } public static int getSequenceNextValue() throws SysException { String sql = "SELECT * FROM WAP_SEQUENCE WHERE TABLE_NAME = 'WAP_BOOK_CARD'"; Connection dbConnection = null; Statement stmt = null; ResultSet rs = null; int lastValue = 1; try { dbConnection = getDBConnection(); stmt = dbConnection.createStatement(); rs = stmt.executeQuery(sql); if ( rs.next() ) { lastValue = rs.getInt("LAST_VALUE"); int minValue = rs.getInt("MIN_VALUE"); int maxValue = rs.getInt("MAX_VALUE"); int incrementBy = rs.getInt("INCREMENT_BY"); int cycle = rs.getInt("CYCLE"); if (lastValue < 0) { Debug.println("id last value can't smaller than 0"); throw new SysException("id last value can't smaller than 0"); } if (minValue < 0) { Debug.println("id min value can't smaller than 0"); throw new SysException("id min value can't smaller than 0"); } if (maxValue < 0) { Debug.println("id max value can't smaller than 0"); throw new SysException("id max value can't smaller than 0"); } if (incrementBy < 1) { Debug.println("id increment value can't smaller than 1"); throw new SysException("id increment value can't smaller than 1"); } if (cycle != 0 && cycle != 1) { Debug.println("id cycle value error, it can be 0 or 1 only"); throw new SysException("id cycle value error, it can be 0 or 1 only"); } if (lastValue < minValue) { Debug.println("id value is " + lastValue + ": \n" + "id value can't smaller than min value"); throw new SysException("id value can't smaller than min value"); } if (minValue >= maxValue) { Debug.println("id max value must bigger than min value"); throw new SysException("id max value must bigger than min value"); } if (lastValue > maxValue) { if (cycle == 0) { Debug.println("id value is " + lastValue + ": \n" + "id value exceeds max value"); throw new SysException("id value exceeds max value"); } else { lastValue = minValue; } } sql = "UPDATE WAP_SEQUENCE SET LAST_VALUE = " + (lastValue + incrementBy) + " WHERE TABLE_NAME = 'WAP_BOOK_CARD'"; stmt.executeUpdate(sql); }else{ sql = "INSERT INTO WAP_SEQUENCE (TABLE_NAME, LAST_VALUE, MIN_VALUE, MAX_VALUE, INCREMENT_BY, CYCLE) VALUES ('WAP_BOOK_CARD', 2, 1, 999999999, 1, 1)"; stmt.executeUpdate(sql); } return lastValue; } catch(SQLException e) { Debug.println(sql + "\n" + "SQLException while execute getRowCountBySql method :\n" + e); throw new SysException("SQLException while execute getRowCountBySql method :\n" + e); } finally { closeResultSet(rs); closeStatement(stmt); closeConnection(dbConnection); } } protected static String getInsertSQL(BookCardModel model) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "INSERT INTO WAP_BOOK_CARD (ID, USER_MDN, USER_NAME, USER_JOB, USER_COM, USER_TEL, USER_FAX, USER_EMAIL, USER_ADRESS, USER_POST) VALUES (" + model.getId() + ", '" + Function.writeDBEncode(model.getUserMdn()) + "', '" + Function.writeDBEncode(model.getUserName()) + "', '" + Function.writeDBEncode(model.getUserJob()) + "', '" + Function.writeDBEncode(model.getUserCom()) + "', '" + Function.writeDBEncode(model.getUserTel()) + "', '" + Function.writeDBEncode(model.getUserFax()) + "', '" + Function.writeDBEncode(model.getUserEmail()) + "', '" + Function.writeDBEncode(model.getUserAdress()) + "', '" + Function.writeDBEncode(model.getUserPost()) + "')"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "INSERT INTO WAP_BOOK_CARD (ID, USER_MDN, USER_NAME, USER_JOB, USER_COM, USER_TEL, USER_FAX, USER_EMAIL, USER_ADRESS, USER_POST) VALUES (" + model.getId() + ", '" + Function.writeDBEncode(model.getUserMdn()) + "', '" + Function.writeDBEncode(model.getUserName()) + "', '" + Function.writeDBEncode(model.getUserJob()) + "', '" + Function.writeDBEncode(model.getUserCom()) + "', '" + Function.writeDBEncode(model.getUserTel()) + "', '" + Function.writeDBEncode(model.getUserFax()) + "', '" + Function.writeDBEncode(model.getUserEmail()) + "', '" + Function.writeDBEncode(model.getUserAdress()) + "', '" + Function.writeDBEncode(model.getUserPost()) + "')"; } else if (Database.dbType == DBType.MYSQL) { sql = "INSERT INTO WAP_BOOK_CARD (ID, USER_MDN, USER_NAME, USER_JOB, USER_COM, USER_TEL, USER_FAX, USER_EMAIL, USER_ADRESS, USER_POST) VALUES (" + model.getId() + ", '" + Function.writeDBEncode(model.getUserMdn()) + "', '" + Function.writeDBEncode(model.getUserName()) + "', '" + Function.writeDBEncode(model.getUserJob()) + "', '" + Function.writeDBEncode(model.getUserCom()) + "', '" + Function.writeDBEncode(model.getUserTel()) + "', '" + Function.writeDBEncode(model.getUserFax()) + "', '" + Function.writeDBEncode(model.getUserEmail()) + "', '" + Function.writeDBEncode(model.getUserAdress()) + "', '" + Function.writeDBEncode(model.getUserPost()) + "')"; } return sql; } protected static String getUpdateSQL(BookCardModel model) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "UPDATE WAP_BOOK_CARD SET USER_MDN = '" + Function.writeDBEncode(model.getUserMdn()) + "', USER_NAME = '" + Function.writeDBEncode(model.getUserName()) + "', USER_JOB = '" + Function.writeDBEncode(model.getUserJob()) + "', USER_COM = '" + Function.writeDBEncode(model.getUserCom()) + "', USER_TEL = '" + Function.writeDBEncode(model.getUserTel()) + "', USER_FAX = '" + Function.writeDBEncode(model.getUserFax()) + "', USER_EMAIL = '" + Function.writeDBEncode(model.getUserEmail()) + "', USER_ADRESS = '" + Function.writeDBEncode(model.getUserAdress()) + "', USER_POST = '" + Function.writeDBEncode(model.getUserPost()) + "' WHERE ID = " + model.getId(); } else if (Database.dbType == DBType.SQLSERVER) { sql = "UPDATE WAP_BOOK_CARD SET USER_MDN = '" + Function.writeDBEncode(model.getUserMdn()) + "', USER_NAME = '" + Function.writeDBEncode(model.getUserName()) + "', USER_JOB = '" + Function.writeDBEncode(model.getUserJob()) + "', USER_COM = '" + Function.writeDBEncode(model.getUserCom()) + "', USER_TEL = '" + Function.writeDBEncode(model.getUserTel()) + "', USER_FAX = '" + Function.writeDBEncode(model.getUserFax()) + "', USER_EMAIL = '" + Function.writeDBEncode(model.getUserEmail()) + "', USER_ADRESS = '" + Function.writeDBEncode(model.getUserAdress()) + "', USER_POST = '" + Function.writeDBEncode(model.getUserPost()) + "' WHERE ID = " + model.getId(); } else if (Database.dbType == DBType.MYSQL) { sql = "UPDATE WAP_BOOK_CARD SET USER_MDN = '" + Function.writeDBEncode(model.getUserMdn()) + "', USER_NAME = '" + Function.writeDBEncode(model.getUserName()) + "', USER_JOB = '" + Function.writeDBEncode(model.getUserJob()) + "', USER_COM = '" + Function.writeDBEncode(model.getUserCom()) + "', USER_TEL = '" + Function.writeDBEncode(model.getUserTel()) + "', USER_FAX = '" + Function.writeDBEncode(model.getUserFax()) + "', USER_EMAIL = '" + Function.writeDBEncode(model.getUserEmail()) + "', USER_ADRESS = '" + Function.writeDBEncode(model.getUserAdress()) + "', USER_POST = '"
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -