📄 netstatlogdao.java
字号:
closeStatement(stmt); closeConnection(dbConnection); } } public static Collection queryAll() throws SysException { String sql = "SELECT * FROM WAP_NETSTAT_LOG"; return queryBySql(sql); } public static Collection queryAll(int beginRow, int endRow) throws SysException { String sql = "SELECT * FROM WAP_NETSTAT_LOG"; return queryBySql(sql, beginRow, endRow); } public static int getRowCountOfAll() throws SysException { String sql = "SELECT COUNT(*) FROM WAP_NETSTAT_LOG"; return getRowCountBySql(sql); } public static NetstatLogModel toModel(Hashtable element) { NetstatLogModel model = new NetstatLogModel(); model.setId(Integer.parseInt((String)element.get("id"))); model.setLogIp((String)element.get("logIp")); model.setLogStarttime((Timestamp)element.get("logStarttime")); model.setLogEndtime((Timestamp)element.get("logEndtime")); model.setLogMemo((String)element.get("logMemo")); 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_NETSTAT_LOG'"; 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_NETSTAT_LOG'"; stmt.executeUpdate(sql); }else{ sql = "INSERT INTO WAP_SEQUENCE (TABLE_NAME, LAST_VALUE, MIN_VALUE, MAX_VALUE, INCREMENT_BY, CYCLE) VALUES ('WAP_NETSTAT_LOG', 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(NetstatLogModel model) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "INSERT INTO WAP_NETSTAT_LOG (ID, LOG_IP, LOG_STARTTIME, LOG_ENDTIME, LOG_MEMO) VALUES (" + model.getId() + ", '" + Function.writeDBEncode(model.getLogIp()) + "', TO_DATE('" + Function.DateToString(model.getLogStarttime()) + "', 'YYYY/MM/DD HH24:MI:SS'), TO_DATE('" + Function.DateToString(model.getLogEndtime()) + "', 'YYYY/MM/DD HH24:MI:SS'), '" + Function.writeDBEncode(model.getLogMemo()) + "')"; } else if (Database.dbType == DBType.SQLSERVER) { sql = "INSERT INTO WAP_NETSTAT_LOG (ID, LOG_IP, LOG_STARTTIME, LOG_ENDTIME, LOG_MEMO) VALUES (" + model.getId() + ", '" + Function.writeDBEncode(model.getLogIp()) + "', '" + Function.DateToString(model.getLogStarttime()) + "', '" + Function.DateToString(model.getLogEndtime()) + "', '" + Function.writeDBEncode(model.getLogMemo()) + "')"; } else if (Database.dbType == DBType.MYSQL) { sql = "INSERT INTO WAP_NETSTAT_LOG (ID, LOG_IP, LOG_STARTTIME, LOG_ENDTIME, LOG_MEMO) VALUES (" + model.getId() + ", '" + Function.writeDBEncode(model.getLogIp()) + "', '" + Function.DateToString(model.getLogStarttime()) + "', '" + Function.DateToString(model.getLogEndtime()) + "', '" + Function.writeDBEncode(model.getLogMemo()) + "')"; } return sql; } protected static String getUpdateSQL(NetstatLogModel model) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "UPDATE WAP_NETSTAT_LOG SET LOG_IP = '" + Function.writeDBEncode(model.getLogIp()) + "', LOG_STARTTIME = TO_DATE('" + Function.DateToString(model.getLogStarttime()) + "', 'YYYY-MM-DD HH24:MI:SS'), LOG_ENDTIME = TO_DATE('" + Function.DateToString(model.getLogEndtime()) + "', 'YYYY-MM-DD HH24:MI:SS'), LOG_MEMO = '" + Function.writeDBEncode(model.getLogMemo()) + "' WHERE ID = " + model.getId(); } else if (Database.dbType == DBType.SQLSERVER) { sql = "UPDATE WAP_NETSTAT_LOG SET LOG_IP = '" + Function.writeDBEncode(model.getLogIp()) + "', LOG_STARTTIME = '" + Function.DateToString(model.getLogStarttime()) + "', LOG_ENDTIME = '" + Function.DateToString(model.getLogEndtime()) + "', LOG_MEMO = '" + Function.writeDBEncode(model.getLogMemo()) + "' WHERE ID = " + model.getId(); } else if (Database.dbType == DBType.MYSQL) { sql = "UPDATE WAP_NETSTAT_LOG SET LOG_IP = '" + Function.writeDBEncode(model.getLogIp()) + "', LOG_STARTTIME = '" + Function.DateToString(model.getLogStarttime()) + "', LOG_ENDTIME = '" + Function.DateToString(model.getLogEndtime()) + "', LOG_MEMO = '" + Function.writeDBEncode(model.getLogMemo()) + "' WHERE ID = " + model.getId(); } return sql; } protected static String getDeleteSQL(int id) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "DELETE FROM WAP_NETSTAT_LOG WHERE ID = " + id; } else if (Database.dbType == DBType.SQLSERVER) { sql = "DELETE FROM WAP_NETSTAT_LOG WHERE ID = " + id; } else if (Database.dbType == DBType.MYSQL) { sql = "DELETE FROM WAP_NETSTAT_LOG WHERE ID = " + id; } return sql; } protected static String getDeleteSQL(int[] ids) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "DELETE FROM WAP_NETSTAT_LOG WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR ID = " + ids[i]; } } else if (Database.dbType == DBType.SQLSERVER) { sql = "DELETE FROM WAP_NETSTAT_LOG WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR ID = " + ids[i]; } } else if (Database.dbType == DBType.MYSQL) { sql = "DELETE FROM WAP_NETSTAT_LOG WHERE 1 = 2"; for ( int i = 0; i < ids.length; i++ ) { sql += " OR ID = " + ids[i]; } } return sql; } protected static String getFindByPKSQL(int id) { String sql = ""; if (Database.dbType == DBType.ORACLE) { sql = "SELECT * FROM WAP_NETSTAT_LOG WHERE ID = " + id; } else if (Database.dbType == DBType.SQLSERVER) { sql = "SELECT * FROM WAP_NETSTAT_LOG WHERE ID = " + id; } else if (Database.dbType == DBType.MYSQL) { sql = "SELECT * FROM WAP_NETSTAT_LOG WHERE ID = " + id; } return sql; } protected static boolean isValidData(NetstatLogModel 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)) { NetstatLogModel model=new NetstatLogModel(); model.setId(NetstatLogBean.getSequenceNextValue()); model.setLogIp("abc"); model.setLogStarttime(Function.getCurrTime()); model.setLogEndtime(Function.getCurrTime()); model.setLogMemo("abc"); NetstatLogModel netstatLogModel = NetstatLogBean.insert(model); }else if ("update".equalsIgnoreCase(action)){ NetstatLogModel netstatLogModel = NetstatLogBean.findByPK(1); int id = netstatLogModel.getId(); String logIp = netstatLogModel.getLogIp(); Timestamp logStarttime = netstatLogModel.getLogStarttime(); Timestamp logEndtime = netstatLogModel.getLogEndtime(); String logMemo = netstatLogModel.getLogMemo(); netstatLogModel.setLogIp(logIp); netstatLogModel.setLogStarttime(logStarttime); netstatLogModel.setLogEndtime(logEndtime); netstatLogModel.setLogMemo(logMemo); update(netstatLogModel); }else if ("delete".equalsIgnoreCase(action)){ int[] ids = new int[3]; ids[0] = 1; ids[1] = 2; ids[2] = 3; NetstatLogBean.delete(ids); }else if ("findByPK".equalsIgnoreCase(action)){ NetstatLogModel netstatLogModel = NetstatLogBean.findByPK(1); int id = netstatLogModel.getId(); String logIp = netstatLogModel.getLogIp(); Timestamp logStarttime = netstatLogModel.getLogStarttime(); Timestamp logEndtime = netstatLogModel.getLogEndtime(); String logMemo = netstatLogModel.getLogMemo(); System.out.println("id = " + id); System.out.println("logIp = " + logIp); System.out.println("logStarttime = " + Function.getYYYYMMDDHHMISS(logStarttime)); System.out.println("logEndtime = " + Function.getYYYYMMDDHHMISS(logEndtime)); System.out.println("logMemo = " + logMemo); }else if ("queryBySql".equalsIgnoreCase(action)){ Collection coll = NetstatLogBean.queryBySql("SELECT * FROM WAP_NETSTAT_LOG"); }else if ("getRowCountBySql".equalsIgnoreCase(action)){ int rowCount = NetstatLogBean.getRowCountBySql("SELECT COUNT(*) FROM WAP_NETSTAT_LOG"); System.out.println("rowCount = " + rowCount); }else if ("queryAll".equalsIgnoreCase(action)){ Collection coll = NetstatLogBean.queryAll(); Iterator it = coll.iterator(); while ( it.hasNext() ) { Hashtable element = (Hashtable)it.next(); String id = (String)element.get("id"); String logIp = (String)element.get("logIp"); String logStarttime = Function.getYYYYMMDDHHMISS((Timestamp)element.get("logStarttime")); String logEndtime = Function.getYYYYMMDDHHMISS((Timestamp)element.get("logEndtime")); String logMemo = (String)element.get("logMemo"); System.out.print("id = " + id + ",\t"); System.out.print("logIp = " + logIp + ",\t"); System.out.print("logStarttime = " + logStarttime + ",\t"); System.out.print("logEndtime = " + logEndtime + ",\t"); System.out.print("logMemo = " + logMemo + "\n"); } }else if ("getRowCountOfAll".equalsIgnoreCase(action)){ int rowCount = NetstatLogBean.getRowCountOfAll(); System.out.println("rowCount = " + rowCount); }else if ("getSequenceNextValue".equalsIgnoreCase(action)){ int sequenceValue = NetstatLogBean.getSequenceNextValue(); System.out.println("sequenceValue = " + sequenceValue); } }}/*int id = (model==null?0:model.getId());String logIp = (model==null?"":model.getLogIp());Timestamp logStarttime = (model==null?null:model.getLogStarttime());Timestamp logEndtime = (model==null?null:model.getLogEndtime());String logMemo = (model==null?"":model.getLogMemo());int id = PageUtil.parseIntField(request.getParameter("id"), "id", false, 0, );String logIp = PageUtil.parseStringField(request.getParameter("logIp"), "logIp", false, 14);String logMemo = PageUtil.parseStringField(request.getParameter("logMemo"), "logMemo", false, 199);int id = PageUtil.parseIntField(request.getParameter("id"), "id", true, 0, 999999999);String logIp = PageUtil.parseStringField(request.getParameter("logIp"), "logIp", true, 15);Timestamp logStarttime = PageUtil.parseDateField(request.getParameter("logStarttime"), "logStarttime", true);Timestamp logEndtime = PageUtil.parseDateField(request.getParameter("logEndtime"), "logEndtime", true);String logMemo = PageUtil.parseStringField(request.getParameter("logMemo"), "logMemo", true, 200);String id = (model==null?"0":""+model.getId());String logIp = (model==null?"":""+model.getLogIp());Timestamp logStarttime = (model==null?null:model.getLogStarttime());Timestamp logEndtime = (model==null?null:model.getLogEndtime());String logMemo = (model==null?"":""+model.getLogMemo());Collection coll = NetstatLogBean.queryAll((pageNum-1) * pageRow + 1, pageNum * pageRow);Iterator it = coll.iterator();while ( it.hasNext() ) { Hashtable element = (Hashtable)it.next(); NetstatLogModel netstatLogModel = NetstatLogBean.toModel(element); int id = netstatLogModel.getId(); String logIp = netstatLogModel.getLogIp(); Timestamp logStarttime = netstatLogModel.getLogStarttime(); Timestamp logEndtime = netstatLogModel.getLogEndtime(); String logMemo = netstatLogModel.getLogMemo(); excel.setCellData(new ExcelCell(ExcelUtil.INT_CELL_TYTE, currRow, currCol++), id+""); excel.setCellData(new ExcelCell(ExcelUtil.STRING_CELL_TYTE, currRow, currCol++), logIp+""); excel.setCellData(new ExcelCell(ExcelUtil.DATE_CELL_TYTE, currRow, currCol++), logStarttime); excel.setCellData(new ExcelCell(ExcelUtil.DATE_CELL_TYTE, currRow, currCol++), logEndtime); excel.setCellData(new ExcelCell(ExcelUtil.STRING_CELL_TYTE, currRow, currCol++), logMemo+"");*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -