📄 bookuserdao.java
字号:
closeStatement(stmt); closeConnection(dbConnection); } } public static Collection queryAll() throws SysException { String sql = "SELECT * FROM WAP_BOOK_USER"; return queryBySql(sql); } public static Collection queryAll(int beginRow, int endRow) throws SysException { String sql = "SELECT * FROM WAP_BOOK_USER"; return queryBySql(sql, beginRow, endRow); } public static int getRowCountOfAll() throws SysException { String sql = "SELECT COUNT(*) FROM WAP_BOOK_USER"; return getRowCountBySql(sql); } public static BookUserModel toModel(Hashtable element) { BookUserModel model = new BookUserModel(); model.setUserMdn((String)element.get("userMdn")); model.setCardId(Long.parseLong((String)element.get("cardId"))); model.setRegDate((Timestamp)element.get("regDate")); model.setRegType(Long.parseLong((String)element.get("regType"))); model.setUserPwd((String)element.get("userPwd")); 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_USER'"; 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_USER'"; stmt.executeUpdate(sql); }else{ sql = "INSERT INTO WAP_SEQUENCE (TABLE_NAME, LAST_VALUE, MIN_VALUE, MAX_VALUE, INCREMENT_BY, CYCLE) VALUES ('WAP_BOOK_USER', 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(BookUserModel model) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "INSERT INTO WAP_BOOK_USER (USER_MDN, CARD_ID, REG_DATE, REG_TYPE, USER_PWD) VALUES ('" + Function.writeDBEncode(model.getUserMdn()) + "', " + model.getCardId() + ", TO_DATE('" + Function.DateToString(model.getRegDate()) + "', 'YYYY/MM/DD HH24:MI:SS'), " + model.getRegType() + ", '" + Function.writeDBEncode(model.getUserPwd()) + "')"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "INSERT INTO WAP_BOOK_USER (USER_MDN, CARD_ID, REG_DATE, REG_TYPE, USER_PWD) VALUES ('" + Function.writeDBEncode(model.getUserMdn()) + "', " + model.getCardId() + ", '" + Function.DateToString(model.getRegDate()) + "', " + model.getRegType() + ", '" + Function.writeDBEncode(model.getUserPwd()) + "')"; } else if (Database.dbType == DBType.MYSQL) { sql = "INSERT INTO WAP_BOOK_USER (USER_MDN, CARD_ID, REG_DATE, REG_TYPE, USER_PWD) VALUES ('" + Function.writeDBEncode(model.getUserMdn()) + "', " + model.getCardId() + ", '" + Function.DateToString(model.getRegDate()) + "', " + model.getRegType() + ", '" + Function.writeDBEncode(model.getUserPwd()) + "')"; } return sql; } protected static String getUpdateSQL(BookUserModel model) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "UPDATE WAP_BOOK_USER SET CARD_ID = " + model.getCardId() + ", REG_DATE = TO_DATE('" + Function.DateToString(model.getRegDate()) + "', 'YYYY-MM-DD HH24:MI:SS'), REG_TYPE = " + model.getRegType() + ", USER_PWD = '" + Function.writeDBEncode(model.getUserPwd()) + "' WHERE USER_MDN = '" + model.getUserMdn() + "'"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "UPDATE WAP_BOOK_USER SET CARD_ID = " + model.getCardId() + ", REG_DATE = '" + Function.DateToString(model.getRegDate()) + "', REG_TYPE = " + model.getRegType() + ", USER_PWD = '" + Function.writeDBEncode(model.getUserPwd()) + "' WHERE USER_MDN = '" + model.getUserMdn() + "'"; } else if (Database.dbType == DBType.MYSQL) { sql = "UPDATE WAP_BOOK_USER SET CARD_ID = " + model.getCardId() + ", REG_DATE = '" + Function.DateToString(model.getRegDate()) + "', REG_TYPE = " + model.getRegType() + ", USER_PWD = '" + Function.writeDBEncode(model.getUserPwd()) + "' WHERE USER_MDN = '" + model.getUserMdn() + "'"; } return sql; } protected static String getDeleteSQL(String userMdn) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "DELETE FROM WAP_BOOK_USER WHERE USER_MDN = '" + userMdn + "'"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "DELETE FROM WAP_BOOK_USER WHERE USER_MDN = '" + userMdn + "'"; } else if (Database.dbType == DBType.MYSQL) { sql = "DELETE FROM WAP_BOOK_USER WHERE USER_MDN = '" + userMdn + "'"; } return sql; } protected static String getDeleteSQL(String[] ids) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "DELETE FROM WAP_BOOK_USER WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR USER_MDN = '" + ids[i] + "'"; } } else if (Database.dbType == DBType.SQLSERVER) { sql = "DELETE FROM WAP_BOOK_USER WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR USER_MDN = '" + ids[i] + "'"; } } else if (Database.dbType == DBType.MYSQL) { sql = "DELETE FROM WAP_BOOK_USER WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR USER_MDN = '" + ids[i] + "'"; } } return sql; } protected static String getFindByPKSQL(String userMdn) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "SELECT * FROM WAP_BOOK_USER WHERE USER_MDN = '" + userMdn + "'"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "SELECT * FROM WAP_BOOK_USER WHERE USER_MDN = '" + userMdn + "'"; } else if (Database.dbType == DBType.MYSQL) { sql = "SELECT * FROM WAP_BOOK_USER WHERE USER_MDN = '" + userMdn + "'"; } return sql; } protected static boolean isValidData(BookUserModel model) { return true; } private static Connection getDBConnection() throws SysException { return Database.getConnection(); } private static void closeConnection(Connection dbConnection) throws SysException { try { if (dbConnection != null && !dbConnection.isClosed()) { dbConnection.close(); dbConnection = null; } } catch (SQLException se) { //throw new SysException("SQL Exception while closing DB connection : \n" + se); } } private static void closeResultSet(ResultSet result) throws SysException { try { if (result != null) { result.close(); } } catch (SQLException se) { //throw new SysException("SQL Exception while closing Result Set : \n" + se); } } private static void closeStatement(Statement stmt) throws SysException { try { if (stmt != null) { stmt.close(); } } catch (SQLException se) { //throw new SysException("SQL Exception while closing Statement : \n" + se); } } public static void main(String[] args) throws Exception{ String action = ""; if (args.length == 1) { action = args[0]; } if (action == null || "".equals(action)) { } else if ("insert".equalsIgnoreCase(action)) { BookUserModel model=new BookUserModel(); model.setUserMdn("abc"); model.setCardId(999999999999999999L); model.setRegDate(Function.getCurrTime()); model.setRegType(999999999999999999L); model.setUserPwd("abc"); BookUserModel bookUserModel = BookUserBean.insert(model); }else if ("update".equalsIgnoreCase(action)){ BookUserModel bookUserModel = BookUserBean.findByPK("1"); String userMdn = bookUserModel.getUserMdn(); long cardId = bookUserModel.getCardId(); Timestamp regDate = bookUserModel.getRegDate(); long regType = bookUserModel.getRegType(); String userPwd = bookUserModel.getUserPwd(); bookUserModel.setCardId(cardId); bookUserModel.setRegDate(regDate); bookUserModel.setRegType(regType); bookUserModel.setUserPwd(userPwd); update(bookUserModel); }else if ("delete".equalsIgnoreCase(action)){ String[] ids = new String[3]; ids[0] = "1"; ids[1] = "2"; ids[2] = "3"; BookUserBean.delete(ids); }else if ("findByPK".equalsIgnoreCase(action)){ BookUserModel bookUserModel = BookUserBean.findByPK("1"); String userMdn = bookUserModel.getUserMdn(); long cardId = bookUserModel.getCardId(); Timestamp regDate = bookUserModel.getRegDate(); long regType = bookUserModel.getRegType(); String userPwd = bookUserModel.getUserPwd(); System.out.println("userMdn = " + userMdn); System.out.println("cardId = " + cardId); System.out.println("regDate = " + Function.getYYYYMMDDHHMISS(regDate)); System.out.println("regType = " + regType); System.out.println("userPwd = " + userPwd); }else if ("queryBySql".equalsIgnoreCase(action)){ Collection coll = BookUserBean.queryBySql("SELECT * FROM WAP_BOOK_USER"); }else if ("getRowCountBySql".equalsIgnoreCase(action)){ int rowCount = BookUserBean.getRowCountBySql("SELECT COUNT(*) FROM WAP_BOOK_USER"); System.out.println("rowCount = " + rowCount); }else if ("queryAll".equalsIgnoreCase(action)){ Collection coll = BookUserBean.queryAll(); Iterator it = coll.iterator(); while ( it.hasNext() ) { Hashtable element = (Hashtable)it.next(); String userMdn = (String)element.get("userMdn"); String cardId = (String)element.get("cardId"); String regDate = Function.getYYYYMMDDHHMISS((Timestamp)element.get("regDate")); String regType = (String)element.get("regType"); String userPwd = (String)element.get("userPwd"); System.out.print("userMdn = " + userMdn + ",\t"); System.out.print("cardId = " + cardId + ",\t"); System.out.print("regDate = " + regDate + ",\t"); System.out.print("regType = " + regType + ",\t"); System.out.print("userPwd = " + userPwd + "\n"); } }else if ("getRowCountOfAll".equalsIgnoreCase(action)){ int rowCount = BookUserBean.getRowCountOfAll(); System.out.println("rowCount = " + rowCount); }else if ("getSequenceNextValue".equalsIgnoreCase(action)){ int sequenceValue = BookUserBean.getSequenceNextValue(); System.out.println("sequenceValue = " + sequenceValue); } }}/*String userMdn = (model==null?"":model.getUserMdn());long cardId = (model==null?0:model.getCardId());Timestamp regDate = (model==null?null:model.getRegDate());long regType = (model==null?0:model.getRegType());String userPwd = (model==null?"":model.getUserPwd());String userMdn = PageUtil.parseStringField(request.getParameter("userMdn"), "userMdn", false, 19);long cardId = PageUtil.parseLongField(request.getParameter("cardId"), "cardId", false, 0, 19);long regType = PageUtil.parseLongField(request.getParameter("regType"), "regType", false, 0, 19);String userPwd = PageUtil.parseStringField(request.getParameter("userPwd"), "userPwd", false, 19);String userMdn = PageUtil.parseStringField(request.getParameter("userMdn"), "userMdn", false, 20);long cardId = PageUtil.parseLongField(request.getParameter("cardId"), "cardId", true, 0, 0);Timestamp regDate = PageUtil.parseDateField(request.getParameter("regDate"), "regDate", true);long regType = PageUtil.parseLongField(request.getParameter("regType"), "regType", true, 0, 0);String userPwd = PageUtil.parseStringField(request.getParameter("userPwd"), "userPwd", true, 20);String userMdn = (model==null?"":""+model.getUserMdn());String cardId = (model==null?"0":""+model.getCardId());Timestamp regDate = (model==null?null:model.getRegDate());String regType = (model==null?"0":""+model.getRegType());String userPwd = (model==null?"":""+model.getUserPwd());Collection coll = BookUserBean.queryAll((pageNum-1) * pageRow + 1, pageNum * pageRow);Iterator it = coll.iterator();while ( it.hasNext() ) { Hashtable element = (Hashtable)it.next(); BookUserModel bookUserModel = BookUserBean.toModel(element); String userMdn = bookUserModel.getUserMdn(); long cardId = bookUserModel.getCardId(); Timestamp regDate = bookUserModel.getRegDate(); long regType = bookUserModel.getRegType(); String userPwd = bookUserModel.getUserPwd(); excel.setCellData(new ExcelCell(ExcelUtil.STRING_CELL_TYTE, currRow, currCol++), userMdn+""); excel.setCellData(new ExcelCell(ExcelUtil.INT_CELL_TYTE, currRow, currCol++), cardId+""); excel.setCellData(new ExcelCell(ExcelUtil.DATE_CELL_TYTE, currRow, currCol++), regDate); excel.setCellData(new ExcelCell(ExcelUtil.INT_CELL_TYTE, currRow, currCol++), regType+""); excel.setCellData(new ExcelCell(ExcelUtil.STRING_CELL_TYTE, currRow, currCol++), userPwd+"");*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -