📄 wap2mobiledao.java
字号:
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_WAP2_MOBILE"; return queryBySql(sql); } public static Collection queryAll(int beginRow, int endRow) throws SysException { String sql = "SELECT * FROM WAP_WAP2_MOBILE"; return queryBySql(sql, beginRow, endRow); } public static int getRowCountOfAll() throws SysException { String sql = "SELECT COUNT(*) FROM WAP_WAP2_MOBILE"; return getRowCountBySql(sql); } public static Wap2MobileModel toModel(Hashtable element) { Wap2MobileModel model = new Wap2MobileModel(); model.setMobileType((String)element.get("mobileType")); model.setUserAgent((String)element.get("userAgent")); 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_WAP2_MOBILE'"; 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_WAP2_MOBILE'"; stmt.executeUpdate(sql); }else{ sql = "INSERT INTO WAP_SEQUENCE (TABLE_NAME, LAST_VALUE, MIN_VALUE, MAX_VALUE, INCREMENT_BY, CYCLE) VALUES ('WAP_WAP2_MOBILE', 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(Wap2MobileModel model) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "INSERT INTO WAP_WAP2_MOBILE (MOBILE_TYPE, USER_AGENT) VALUES ('" + Function.writeDBEncode(model.getMobileType()) + "', '" + Function.writeDBEncode(model.getUserAgent()) + "')"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "INSERT INTO WAP_WAP2_MOBILE (MOBILE_TYPE, USER_AGENT) VALUES ('" + Function.writeDBEncode(model.getMobileType()) + "', '" + Function.writeDBEncode(model.getUserAgent()) + "')"; } else if (Database.dbType == DBType.MYSQL) { sql = "INSERT INTO WAP_WAP2_MOBILE (MOBILE_TYPE, USER_AGENT) VALUES ('" + Function.writeDBEncode(model.getMobileType()) + "', '" + Function.writeDBEncode(model.getUserAgent()) + "')"; } return sql; } protected static String getUpdateSQL(Wap2MobileModel model) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "UPDATE WAP_WAP2_MOBILE SET USER_AGENT = '" + Function.writeDBEncode(model.getUserAgent()) + "' WHERE MOBILE_TYPE = '" + model.getMobileType() + "'"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "UPDATE WAP_WAP2_MOBILE SET USER_AGENT = '" + Function.writeDBEncode(model.getUserAgent()) + "' WHERE MOBILE_TYPE = '" + model.getMobileType() + "'"; } else if (Database.dbType == DBType.MYSQL) { sql = "UPDATE WAP_WAP2_MOBILE SET USER_AGENT = '" + Function.writeDBEncode(model.getUserAgent()) + "' WHERE MOBILE_TYPE = '" + model.getMobileType() + "'"; } return sql; } protected static String getDeleteSQL(String mobileType) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "DELETE FROM WAP_WAP2_MOBILE WHERE MOBILE_TYPE = '" + mobileType + "'"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "DELETE FROM WAP_WAP2_MOBILE WHERE MOBILE_TYPE = '" + mobileType + "'"; } else if (Database.dbType == DBType.MYSQL) { sql = "DELETE FROM WAP_WAP2_MOBILE WHERE MOBILE_TYPE = '" + mobileType + "'"; } return sql; } protected static String getDeleteSQL(String[] ids) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "DELETE FROM WAP_WAP2_MOBILE WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR MOBILE_TYPE = '" + ids[i] + "'"; } } else if (Database.dbType == DBType.SQLSERVER) { sql = "DELETE FROM WAP_WAP2_MOBILE WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR MOBILE_TYPE = '" + ids[i] + "'"; } } else if (Database.dbType == DBType.MYSQL) { sql = "DELETE FROM WAP_WAP2_MOBILE WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR MOBILE_TYPE = '" + ids[i] + "'"; } } return sql; } protected static String getFindByPKSQL(String mobileType) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "SELECT * FROM WAP_WAP2_MOBILE WHERE MOBILE_TYPE = '" + mobileType + "'"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "SELECT * FROM WAP_WAP2_MOBILE WHERE MOBILE_TYPE = '" + mobileType + "'"; } else if (Database.dbType == DBType.MYSQL) { sql = "SELECT * FROM WAP_WAP2_MOBILE WHERE MOBILE_TYPE = '" + mobileType + "'"; } return sql; } protected static boolean isValidData(Wap2MobileModel 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)) { Wap2MobileModel model=new Wap2MobileModel(); model.setMobileType("abc"); model.setUserAgent("abc"); Wap2MobileModel wap2MobileModel = Wap2MobileBean.insert(model); }else if ("update".equalsIgnoreCase(action)){ Wap2MobileModel wap2MobileModel = Wap2MobileBean.findByPK("1"); String mobileType = wap2MobileModel.getMobileType(); String userAgent = wap2MobileModel.getUserAgent(); wap2MobileModel.setUserAgent(userAgent); update(wap2MobileModel); }else if ("delete".equalsIgnoreCase(action)){ String[] ids = new String[3]; ids[0] = "1"; ids[1] = "2"; ids[2] = "3"; Wap2MobileBean.delete(ids); }else if ("findByPK".equalsIgnoreCase(action)){ Wap2MobileModel wap2MobileModel = Wap2MobileBean.findByPK("1"); String mobileType = wap2MobileModel.getMobileType(); String userAgent = wap2MobileModel.getUserAgent(); System.out.println("mobileType = " + mobileType); System.out.println("userAgent = " + userAgent); }else if ("queryBySql".equalsIgnoreCase(action)){ Collection coll = Wap2MobileBean.queryBySql("SELECT * FROM WAP_WAP2_MOBILE"); }else if ("getRowCountBySql".equalsIgnoreCase(action)){ int rowCount = Wap2MobileBean.getRowCountBySql("SELECT COUNT(*) FROM WAP_WAP2_MOBILE"); System.out.println("rowCount = " + rowCount); }else if ("queryAll".equalsIgnoreCase(action)){ Collection coll = Wap2MobileBean.queryAll(); Iterator it = coll.iterator(); while ( it.hasNext() ) { Hashtable element = (Hashtable)it.next(); String mobileType = (String)element.get("mobileType"); String userAgent = (String)element.get("userAgent"); System.out.print("mobileType = " + mobileType + ",\t"); System.out.print("userAgent = " + userAgent + "\n"); } }else if ("getRowCountOfAll".equalsIgnoreCase(action)){ int rowCount = Wap2MobileBean.getRowCountOfAll(); System.out.println("rowCount = " + rowCount); }else if ("getSequenceNextValue".equalsIgnoreCase(action)){ int sequenceValue = Wap2MobileBean.getSequenceNextValue(); System.out.println("sequenceValue = " + sequenceValue); } }}/*String mobileType = (model==null?"":model.getMobileType());String userAgent = (model==null?"":model.getUserAgent());String mobileType = PageUtil.parseStringField(request.getParameter("mobileType"), "mobileType", false, 49);String userAgent = PageUtil.parseStringField(request.getParameter("userAgent"), "userAgent", false, 99);String mobileType = PageUtil.parseStringField(request.getParameter("mobileType"), "mobileType", false, 50);String userAgent = PageUtil.parseStringField(request.getParameter("userAgent"), "userAgent", true, 100);String mobileType = (model==null?"":""+model.getMobileType());String userAgent = (model==null?"":""+model.getUserAgent());Collection coll = Wap2MobileBean.queryAll((pageNum-1) * pageRow + 1, pageNum * pageRow);Iterator it = coll.iterator();while ( it.hasNext() ) { Hashtable element = (Hashtable)it.next(); Wap2MobileModel wap2MobileModel = Wap2MobileBean.toModel(element); String mobileType = wap2MobileModel.getMobileType(); String userAgent = wap2MobileModel.getUserAgent(); excel.setCellData(new ExcelCell(ExcelUtil.STRING_CELL_TYTE, currRow, currCol++), mobileType+""); excel.setCellData(new ExcelCell(ExcelUtil.STRING_CELL_TYTE, currRow, currCol++), userAgent+"");*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -