一、前言
通过定义DAOGenerator代码生成器类、DbField表字段映射类、DbTable表映射类及UniqueIndex唯一索引类,不像mybatis_generator代码生成器,需依赖外部jar包及外部配置文件,通过简单修改主类中属性映射配置就可以生成主业务及关联表的VO/DAO等类源文件(减少大表字段编码)。
二、示例代码
1. DAOGenerator代码生成器类 (配置msMainPackageName - 包路径、msMainPath - 代码生成位置、msCommonImport - 源码导入import路径、CONN_STR/USR/PWD - 数据库连接串/用户名/密码)
package test.dao;@b@@b@import java.io.*;@b@import java.util.*;@b@import java.sql.*;@b@import java.util.Date;@b@@b@public class DAOGenerator {@b@ @b@ private final static String msMainPackageName = "com.xwood.pms";@b@ private final static String msMainPath="c:\\www\\test";@b@@b@ private final static String msCommonImport = "import test.dao;";@b@ private final static String CL = "\r\n";@b@@b@ private final static String CONN_STR = "jdbc:oracle:thin:@127.0.0.1:1521:xwood";@b@ private final static String USR = "user01";@b@ private final static String PWD = "123456";@b@@b@ private String msPackagePath = null;@b@ private String msPackageName = null;@b@@b@ private String msModelClassName = null;@b@ private String msDAOClassName = null;@b@ private String msPrimaryKeyClassName = null;@b@@b@ public DAOGenerator() {@b@ }@b@@b@ public static void main(String[] args) {@b@ String[] tables = {"ACCESS_LOG"};@b@ String[] views = null;@b@ Connection con = null;@b@ try {@b@ Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();@b@ con = DriverManager.getConnection(CONN_STR, USR, PWD);@b@@b@ DAOGenerator vogenerator = new DAOGenerator();@b@ for (int i = 0; i < tables.length; i++) {@b@ try {@b@ vogenerator.generateVO(con, USR.toUpperCase(), tables[i]@b@ .toUpperCase(),views!=null?views[i]:null);@b@ } catch (Exception ex) {@b@ System.out.println(tables[i] + ":" + ex);@b@ ex.printStackTrace();@b@ }@b@ }@b@@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ } finally {@b@ try {@b@ if (con != null)@b@ con.close();@b@ } catch (SQLException ex) {@b@ }@b@ //db.close();@b@ }@b@@b@ }@b@@b@ public void generateVO(Connection pCon, String pDbOwner, String pTableName, String vViewName)@b@ throws Exception {@b@ pTableName = pTableName.toUpperCase();@b@ String sql = "select * from all_tables where owner='" + pDbOwner@b@ + "' and table_name = '" + pTableName.toUpperCase() + "'";@b@ System.out.println(sql);@b@ Statement st = pCon.createStatement();@b@ ResultSet rs = st.executeQuery(sql);@b@ if (!rs.next()) {@b@ throw new Exception("Table not found!");@b@ }@b@ rs.close();@b@ DbTable table = new DbTable();@b@ table.setTableName(pTableName);@b@ sql = "select * from all_tab_columns where owner='" + pDbOwner@b@ + "' and data_type<>'CLOB' and table_name='" + pTableName@b@ + "' order by column_id";@b@ rs = st.executeQuery(sql);@b@ while (rs.next()) {@b@ String sColName = rs.getString("COLUMN_NAME");@b@ String sTypeName = rs.getString("data_type");@b@ int nColSize = rs.getInt("data_PRECISION");@b@ int nDecDigits = rs.getInt("DATA_SCALE");@b@ String sDefault = rs.getString("DATA_DEFAULT");@b@ DbField field = new DbField(sColName, sTypeName, nDecDigits);@b@ field.setPrimaryKey(false);@b@ sql = "select * from all_cons_columns where owner='"@b@ + pDbOwner@b@ + "' and constraint_name in (select constraint_name from all_constraints where owner='"@b@ + pDbOwner + "' and table_name='" + pTableName@b@ + "' and CONSTRAINT_TYPE='P' )";@b@ Statement st1 = pCon.createStatement();@b@ ResultSet rs1 = st1.executeQuery(sql);@b@ while (rs1.next()) {@b@ if (sColName.equals(rs1.getString("COLUMN_NAME"))) {@b@ ;@b@ field.setPrimaryKey(true);@b@ }@b@ }@b@ rs1.close();@b@ st1.close();@b@ field.setDefaultValue(sDefault);@b@ table.addField(field);@b@ }@b@ rs.close();@b@ if (table.getPrimaryKey().size() == 0) {@b@ throw new Exception("Primary key not found!");@b@ }@b@@b@ sql = "select * from all_ind_columns where table_owner='"@b@ + pDbOwner@b@ + "' and index_name in (select index_name from all_indexes where table_owner='"@b@ + pDbOwner@b@ + "' and table_name='"@b@ + pTableName@b@ + "' and uniqueness='UNIQUE') order by index_name,column_position";@b@ System.out.println(sql);@b@ rs = st.executeQuery(sql);@b@ Vector vIndex = null;@b@ String sPreIndex = "";@b@ while (rs.next()) {@b@ String sIndexName = rs.getString("index_name");@b@ String sColumnName = rs.getString("column_name");@b@ if (!sIndexName.equals(sPreIndex)) {@b@ if (vIndex != null) {@b@ UniqueIndex ui = new UniqueIndex();@b@ ui.setColumns(vIndex);@b@ table.addUniIndex(ui);@b@ }@b@ vIndex = new Vector();@b@ }@b@ vIndex.add(table.getField(sColumnName));@b@ sPreIndex = sIndexName;@b@ }@b@ if (vIndex != null) {@b@ UniqueIndex ui = new UniqueIndex();@b@ ui.setColumns(vIndex);@b@ table.addUniIndex(ui);@b@ }@b@ rs.close();@b@ st.close();@b@ if(vViewName!=null){@b@ table.setTableName(vViewName);@b@ }@b@ generateFiles(table);@b@ System.out.println(pTableName);@b@@b@ }@b@@b@ private void generateFiles(DbTable pTable) throws Exception {@b@ //msPackageName=msMainPackageName+"."+pTable.getConvertedJavaName().toLowerCase();@b@ msPackageName = msMainPackageName;@b@ msPackagePath = msMainPath + "/"+ this.msPackageName.toLowerCase().replace('.', '/');@b@ msModelClassName = pTable.getConvertedJavaName() + "VO";@b@ msDAOClassName = pTable.getConvertedJavaName() + "DAO";@b@ msPrimaryKeyClassName = pTable.getConvertedJavaName() + "PK";@b@@b@ this.createPath(pTable);@b@ this.generateModelClass(pTable);@b@ this.generateDAOClass(pTable);@b@ this.generatePrimaryKeyClass(pTable);@b@ }@b@ @b@ private void createPath(DbTable pTable) throws Exception {@b@ /* String sPath = this.msPackagePath+"/vo/";@b@ File file = new File(sPath);@b@ if (!file.exists()) {@b@ file.mkdirs();@b@ }@b@ */@b@ File file = null;@b@ String sPath = null;@b@ sPath = this.msPackagePath + "/"@b@ + pTable.getConvertedJavaName().toLowerCase();@b@ file = new File(sPath);@b@ if (!file.exists()) {@b@ file.mkdirs();@b@ }@b@ }@b@@b@ private void generateModelClass(DbTable pTable) throws Exception {@b@ FileOutputStream fos = new FileOutputStream(msPackagePath + "/" + pTable.getConvertedJavaName().toLowerCase() + "/" + this.msModelClassName + ".java");@b@ PrintStream ps = new PrintStream(fos);@b@ ps.println("package " + this.msPackageName + "."+ pTable.getConvertedJavaName().toLowerCase() + ";");@b@ ps.println();@b@@b@ ps.println("import java.io.Serializable;");@b@ ps.println("import java.math.*;");@b@ ps.println("import java.util.Date;");@b@ ps.println("import java.sql.Timestamp;");@b@ ps.println();@b@@b@ ps.println(this.getFileDescription(pTable));@b@ ps.println("public class " + this.msModelClassName+ " implements Serializable{");@b@ Vector v = pTable.getFields();@b@ for (int i = 0; i < v.size(); i++) {@b@ ps.println(" private " + ((DbField) v.get(i)).getJavaDataType()+ " " + ((DbField) v.get(i)).getJavaMemberVariableName()+ "=" + ((DbField) v.get(i)).getDefaultValue() + ";");@b@ ps.println(" public " + ((DbField) v.get(i)).getJavaDataType() + " " + ((DbField) v.get(i)).getJavaGetMethodName() + "(){return "+ ((DbField) v.get(i)).getJavaMemberVariableName() + ";}");@b@ if (((DbField) v.get(i)).isTimeStamp()) {@b@ ps.println(" public void " + ((DbField) v.get(i)).getJavaSetMethodName() + "(" + ((DbField) v.get(i)).getJavaDataType() + " "+ ((DbField) v.get(i)).getJavaParaVariableName()+ ") throws Exception{");@b@ ps.println(" if("+ ((DbField) v.get(i)).getJavaMemberVariableName()+ "!=" + ((DbField) v.get(i)).getJavaParaVariableName()+ "){");@b@ ps.println(" throw new Exception(\"Record has bean changed by others!\");");@b@ ps.println(" }");@b@ ps.println(" }");@b@ } else {@b@ ps.println(" public void " + ((DbField) v.get(i)).getJavaSetMethodName() + "(" + ((DbField) v.get(i)).getJavaDataType() + " "+ ((DbField) v.get(i)).getJavaParaVariableName() + "){" + ((DbField) v.get(i)).getJavaMemberVariableName()+ "=" + ((DbField) v.get(i)).getJavaParaVariableName()+ ";}");@b@ }@b@ }@b@ ps.println("}");@b@ ps.close();@b@ fos.close();@b@ }@b@@b@ private String getFileDescription(DbTable pTable) throws Exception {@b@ String s = "";@b@ s += "/**" + CL;@b@ s += " * Title: VO Class " + CL;@b@ s += " * Description: vo java beans and servlets" + CL;@b@ s += " * Copyright: Copyright (c) "+ ((new Date()).getYear() + 1900) + CL;@b@ s += " * Company: tplife" + CL;@b@ s += " * @author: auto" + CL;@b@ s += " * @version 1.0" + CL;@b@ s += " * Relate table: " + pTable.getTableName() + CL;@b@ s += " * Auto Generated by VOGen" + CL;@b@ s += " * Generate Date:" + new java.util.Date() + CL;@b@ s += " */";@b@ return s;@b@ }@b@@b@ private void generateDAOClass(DbTable pTable) throws Exception {@b@ FileOutputStream fos = new FileOutputStream(msPackagePath + "/" + pTable.getConvertedJavaName().toLowerCase() + "/" + this.msDAOClassName + ".java");@b@ PrintStream ps = new PrintStream(fos);@b@ String s;@b@ ps.println("package " + this.msPackageName + "." + pTable.getConvertedJavaName().toLowerCase() + ";");@b@ ps.println();@b@@b@ ps.println("import java.sql.*;");@b@ ps.println("import java.util.*;");@b@ ps.println("import java.util.Date;");@b@ ps.println("import java.math.*;");@b@ ps.println("import org.apache.commons.logging.*;");@b@ ps.println();@b@ ps.println(msCommonImport);@b@ ps.println();@b@@b@ ps.println(this.getFileDescription(pTable));@b@ ps.println("public class " + this.msDAOClassName + "{");@b@ ps.println(" private static Log log =LogFactory.getLog("+this.msDAOClassName+".class);");@b@@b@ //create@b@ ps.println();@b@ ps.println(" public static " + this.msPrimaryKeyClassName + " create(" + this.msModelClassName + " p" + this.msModelClassName + ") throws Exception{");@b@ ps.println(" PreparedStatement pst=null;");@b@ ps.println(" String sql=null;");@b@ ps.println(" Connection conn=DBUtil.getConnection();");@b@ ps.println(" " + this.msPrimaryKeyClassName + " pk=null;");@b@ ps.println(" try{");@b@ s = " sql=\"insert into " + pTable.getTableName() + " (";@b@ for (int i = 0; i < pTable.getFields().size(); i++) {@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ s += ((DbField) pTable.getFields().get(i)).getFieldName();@b@ }@b@ s += ") values(";@b@ for (int i = 0; i < pTable.getFields().size(); i++) {@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ s += "?";@b@ }@b@ s += ")\";";@b@ ps.println(s);@b@ ps.println(" pst=conn.prepareStatement(sql);");@b@ ps.println(" int pos=1;");@b@ for (int i = 0; i < pTable.getFields().size(); i++) {@b@ DbField field = (DbField) pTable.getFields().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType()+ "(pos,p" + this.msModelClassName + "."+ field.getJavaGetMethodName() + "());");@b@ ps.println(" pos++;");@b@ }@b@ ps.println(" int nRow=pst.executeUpdate();");@b@ ps.println(" if(nRow!=1){");@b@ ps.println(" String error = \"DAOCreate: " + this.msDAOClassName + "(\"+p" + this.msModelClassName + ".toString()" + "+\") not found\";");@b@ ps.println(" throw new Exception (error);");@b@ ps.println(" }");@b@ ps.println(" }catch(Exception e){");@b@ ps.println(" log.error(\"DAO Create Error\",e);");@b@ ps.println(" throw e;");@b@ ps.println(" }finally{");@b@ ps.println(" DBUtil.close(null,pst,conn);");@b@ ps.println(" }");@b@ s = " pk=new " + this.msPrimaryKeyClassName + "(";@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ s += "p" + this.msModelClassName + "."+ field.getJavaGetMethodName() + "()";@b@ }@b@ s += ");";@b@ ps.println(s);@b@ ps.println(" return pk;");@b@ ps.println(" }");@b@@b@ //batchCreate@b@ ps.println();@b@ ps.println(" public static void batchCreate(List p" + this.msModelClassName + "List) throws Exception{");@b@ ps.println(" if (p"+this.msModelClassName+"List==null || p"+this.msModelClassName+"List.size()==0){");@b@ ps.println(" return;");@b@ ps.println(" }");@b@ ps.println(" PreparedStatement pst=null;");@b@ ps.println(" String sql=null;");@b@ ps.println(" Connection conn=DBUtil.getConnection();");@b@ ps.println(" "+this.msModelClassName+" vo=null;");@b@ ps.println(" try{");@b@ s = " sql=\"insert into " + pTable.getTableName() + " (";@b@ for (int i = 0; i < pTable.getFields().size(); i++) {@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ s += ((DbField) pTable.getFields().get(i)).getFieldName();@b@ }@b@ s += ") values(";@b@ for (int i = 0; i < pTable.getFields().size(); i++) {@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ s += "?";@b@ }@b@ s += ")\";";@b@ ps.println(s);@b@ ps.println(" pst=conn.prepareStatement(sql);");@b@ ps.println(" for(int i=0;i<p"+this.msModelClassName+"List.size();i++){");@b@ ps.println(" vo=("+this.msModelClassName+") p"+this.msModelClassName+"List.get(i);");@b@ ps.println(" int pos=1;");@b@ for (int i = 0; i < pTable.getFields().size(); i++) {@b@ DbField field = (DbField) pTable.getFields().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType()+ "(pos,vo."+ field.getJavaGetMethodName() + "());");@b@ ps.println(" pos++;");@b@ }@b@ ps.println(" pst.addBatch();");@b@ ps.println(" }");@b@ ps.println(" int nRow[]=pst.executeBatch();");@b@ ps.println(" for(int j=0;j<nRow.length;j++){");@b@ ps.println(" if(nRow[j]!=1 && nRow[j]!=-2){");@b@ ps.println(" String error = \"DAOBatchCreate: "+this.msDAOClassName+"(\"+(("+this.msModelClassName+") p"+this.msModelClassName+"List.get(j)).toString()"+"+\") not found\";");@b@ ps.println(" throw new Exception (error);");@b@ ps.println(" }");@b@ ps.println(" }");@b@ ps.println(" }catch(Exception e){");@b@ ps.println(" log.error(\"DAO Batch Create Error\",e);");@b@ ps.println(" throw e;");@b@ ps.println(" }finally{");@b@ ps.println(" DBUtil.close(null,pst,conn);");@b@ ps.println(" }");@b@ ps.println(" }");@b@@b@ //store@b@ ps.println();@b@ ps.println(" public static void store(" + this.msModelClassName + " p"+ this.msModelClassName + ") throws Exception{");@b@ ps.println(" PreparedStatement pst=null;");@b@ ps.println(" String sql=null;");@b@ ps.println(" Connection conn=DBUtil.getConnection();");@b@ ps.println(" try{");@b@ s = " sql=\"update " + pTable.getTableName() + " set ";@b@ for (int i = 0; i < pTable.getNonPrimaryKeyField().size(); i++) {@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ DbField field = (DbField) pTable.getNonPrimaryKeyField().get(i);@b@ s += field.getFieldName() + " = ? ";@b@ }@b@@b@ s += " where ";@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ if (i > 0) {@b@ s += " and ";@b@ }@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ s += field.getFieldName() + " = ? ";@b@ }@b@ s += "\";";@b@ ps.println(s);@b@ ps.println(" pst=conn.prepareStatement(sql);");@b@ ps.println(" int pos=1;");@b@ for (int i = 0; i < pTable.getNonPrimaryKeyField().size(); i++) {@b@ DbField field = (DbField) pTable.getNonPrimaryKeyField().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType()+ "(pos,p" + this.msModelClassName + "." + field.getJavaGetMethodName() + "());");@b@ ps.println(" pos++;");@b@ }@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType()+ "(pos,p" + this.msModelClassName + "."+ field.getJavaGetMethodName() + "());");@b@ ps.println(" pos++;");@b@ }@b@ ps.println(" int nRow=pst.executeUpdate();");@b@ ps.println(" if(nRow!=1){");@b@ ps.println(" String error = \"DAOStore: "+ this.msDAOClassName + "(\"+p" + this.msModelClassName + ".toString()" + "+\") not found\";");@b@ ps.println(" throw new Exception (error);");@b@ ps.println(" }");@b@ ps.println(" }catch(Exception e){");@b@ ps.println(" log.error(\"DAO Store Error\",e);");@b@ ps.println(" throw e;");@b@ ps.println(" }finally{");@b@ ps.println(" DBUtil.close(null,pst,conn);");@b@ ps.println(" }");@b@ ps.println(" }");@b@@b@ //batchStore@b@ ps.println();@b@ ps.println(" public static void batchStore(List p"+this.msModelClassName+"List) throws Exception{");@b@ ps.println(" if (p"+this.msModelClassName+"List==null || p"+this.msModelClassName+"List.size()==0){");@b@ ps.println(" return;");@b@ ps.println(" }");@b@ ps.println(" PreparedStatement pst=null;");@b@ ps.println(" String sql=null;");@b@ ps.println(" Connection conn=DBUtil.getConnection();");@b@ ps.println(" "+this.msModelClassName+" vo=null;");@b@ ps.println(" try{");@b@ s = " sql=\"update " + pTable.getTableName() + " set ";@b@ for (int i = 0; i < pTable.getNonPrimaryKeyField().size(); i++) {@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ DbField field = (DbField) pTable.getNonPrimaryKeyField().get(i);@b@ s += field.getFieldName() + " = ? ";@b@ }@b@@b@ s += " where ";@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ if (i > 0) {@b@ s += " and ";@b@ }@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ s += field.getFieldName() + " = ? ";@b@ }@b@ s += "\";";@b@ ps.println(s);@b@ ps.println(" pst=conn.prepareStatement(sql);");@b@ ps.println(" for(int i=0;i<p"+this.msModelClassName+"List.size();i++){");@b@ ps.println(" vo=("+this.msModelClassName+") p"+this.msModelClassName+"List.get(i);");@b@ ps.println(" int pos=1;");@b@ for (int i = 0; i < pTable.getNonPrimaryKeyField().size(); i++) {@b@ DbField field = (DbField) pTable.getNonPrimaryKeyField().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType()+ "(pos,vo." + field.getJavaGetMethodName() + "());");@b@ ps.println(" pos++;");@b@ }@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType()+ "(pos,vo."+ field.getJavaGetMethodName() + "());");@b@ ps.println(" pos++;");@b@ }@b@ ps.println(" pst.addBatch();");@b@ ps.println(" }");@b@ ps.println(" int nRow[]=pst.executeBatch();");@b@ ps.println(" for(int j=0;j<nRow.length;j++){");@b@ ps.println(" if(nRow[j]!=1 && nRow[j]!=-2){");@b@ ps.println(" String error = \"DAOBatchStore: "+this.msDAOClassName+"(\"+(("+this.msModelClassName+") p"+this.msModelClassName+"List.get(j)).toString()"+"+\") not found\";");@b@ ps.println(" throw new Exception (error);");@b@ ps.println(" }");@b@ ps.println(" }");@b@ ps.println(" }catch(Exception e){");@b@ ps.println(" log.error(\"DAO Batch Store Error\",e);");@b@ ps.println(" throw e;");@b@ ps.println(" }finally{");@b@ ps.println(" DBUtil.close(null,pst,conn);");@b@ ps.println(" }");@b@ ps.println(" }");@b@ @b@ //load@b@ ps.println("");@b@ ps.println(" public static " + this.msModelClassName + " load(" + this.msPrimaryKeyClassName + " p" + this.msPrimaryKeyClassName + ") throws Exception{");@b@ ps.println(" PreparedStatement pst=null;");@b@ ps.println(" ResultSet rs=null;");@b@ ps.println(" String sql=null;");@b@ ps.println(" Connection conn=DBUtil.getConnection();");@b@ ps.println(" " + this.msModelClassName + " p"+ this.msModelClassName + "=null;");@b@ ps.println(" try{");@b@@b@ s = " sql=\"select * from " + pTable.getTableName() + " where ";@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ if (i > 0) {@b@ s += " and ";@b@ }@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ s += field.getFieldName() + " = ? ";@b@ }@b@ s += "\";";@b@ ps.println(s);@b@ ps.println(" pst=conn.prepareStatement(sql);");@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType() + "("+ String.valueOf(i + 1) + ",p"+ this.msPrimaryKeyClassName + "."+ field.getJavaGetMethodName() + "());");@b@ }@b@ ps.println(" rs=pst.executeQuery();");@b@ ps.println(" if(!rs.next()){");@b@ ps.println(" String error = \"DAOLoad: " + this.msDAOClassName + "(\"+p" + this.msPrimaryKeyClassName + ".toString()"+ "+\") not found\";");@b@ ps.println(" throw new Exception (error);");@b@ ps.println(" }else{");@b@ ps.println(" p"+ this.msModelClassName + "=fetch(rs);");@b@ ps.println(" }");@b@ ps.println(" }catch(Exception e){");@b@ ps.println(" log.error(\"DAO Load Error\",e);");@b@ ps.println(" throw e;");@b@ ps.println(" }finally{");@b@ ps.println(" DBUtil.close(rs,pst,conn);");@b@ ps.println(" }");@b@ ps.println(" return p" + this.msModelClassName + ";");@b@ ps.println(" }");@b@@b@ //remove@b@ ps.println();@b@ ps.println(" public static void remove(" + this.msPrimaryKeyClassName + " p" + this.msPrimaryKeyClassName + ") throws Exception{");@b@ ps.println(" PreparedStatement pst=null;");@b@ ps.println(" String sql=null;");@b@ ps.println(" Connection conn=DBUtil.getConnection();");@b@ ps.println(" try{");@b@@b@ s = " sql=\"delete from " + pTable.getTableName()+ " where ";@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ if (i > 0) {@b@ s += " and ";@b@ }@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ s += field.getFieldName() + " = ? ";@b@ }@b@ s += "\";";@b@ ps.println(s);@b@ ps.println(" pst=conn.prepareStatement(sql);");@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType() + "("+ String.valueOf(i + 1) + ",p"+ this.msPrimaryKeyClassName + "."+ field.getJavaGetMethodName() + "());");@b@ }@b@ ps.println(" int nRow=pst.executeUpdate();");@b@ ps.println(" if(nRow!=1){");@b@ ps.println(" String error = \"DAORemove: " + this.msDAOClassName + "(\"+p" + this.msPrimaryKeyClassName + ".toString()+\") not found\";");@b@ ps.println(" throw new Exception (error);");@b@ ps.println(" }");@b@ ps.println(" }catch(Exception e){");@b@ ps.println(" log.error(\"DAO Remove Error\",e);");@b@ ps.println(" throw e;");@b@ ps.println(" }finally{");@b@ ps.println(" DBUtil.close(null,pst,conn);");@b@ ps.println(" }");@b@ ps.println(" }");@b@@b@ //find by primary key@b@ ps.println();@b@ ps.println(" public static " + this.msPrimaryKeyClassName + " findByPrimaryKey(" + this.msPrimaryKeyClassName + " p"+ this.msPrimaryKeyClassName + ") throws Exception{");@b@ ps.println(" PreparedStatement pst=null;");@b@ ps.println(" ResultSet rs=null;");@b@ ps.println(" String sql=null;");@b@ ps.println(" Connection conn=DBUtil.getConnection();");@b@ ps.println(" try{");@b@@b@ s = " sql=\"select " + ((DbField) pTable.getPrimaryKey().get(0)).getFieldName()+ " from " + pTable.getTableName() + " where ";@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ if (i > 0) {@b@ s += " and ";@b@ }@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ s += field.getFieldName() + " = ? ";@b@ }@b@ s += "\";";@b@ ps.println(s);@b@ ps.println(" pst=conn.prepareStatement(sql);");@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType() + "("+ String.valueOf(i + 1) + ",p"+ this.msPrimaryKeyClassName + "."+ field.getJavaGetMethodName() + "());");@b@ }@b@ ps.println(" rs=pst.executeQuery();");@b@ ps.println(" if(!rs.next()){");@b@ ps.println(" String error = \"DAOFindByPrimaryKey: " + this.msDAOClassName + "(\"+p"+ this.msPrimaryKeyClassName + ".toString()+\") not found\";");@b@ ps.println(" throw new Exception (error);");@b@ ps.println(" }");@b@ ps.println(" }catch(Exception e){");@b@ ps.println(" log.error(\"DAO findByPrimaryKey Error\",e);");@b@ ps.println(" throw e;");@b@ ps.println(" }finally{");@b@ ps.println(" DBUtil.close(rs,pst,conn);");@b@ ps.println(" }");@b@ ps.println(" return p" + this.msPrimaryKeyClassName + ";");@b@ ps.println(" }");@b@@b@ //find by unique index@b@ ps.println();@b@ for (int j = 0; j < pTable.getAllUniIndex().size(); j++) {@b@ UniqueIndex ui = (UniqueIndex) pTable.getAllUniIndex().get(j);@b@ ps.println(" public static " + this.msPrimaryKeyClassName + " findBy" + ui.getPartMethodName()+ ui.getMethodDeclarePara() + " throws Exception{");@b@ ps.println(" PreparedStatement pst=null;");@b@ ps.println(" ResultSet rs=null;");@b@ ps.println(" String sql=null;");@b@ ps.println(" Connection conn=DBUtil.getConnection();");@b@ ps.println(" " + this.msPrimaryKeyClassName+ " pk=null;");@b@ ps.println(" try{");@b@@b@ s = " sql=\"select ";@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ s += field.getFieldName();@b@ }@b@ s = s + " from " + pTable.getTableName() + " where ";@b@ for (int i = 0; i < ui.getColumns().size(); i++) {@b@ if (i > 0) {@b@ s += " and ";@b@ }@b@ DbField field = (DbField) ui.getColumns().get(i);@b@ s += field.getFieldName() + "=? ";@b@ }@b@@b@ s += "\";";@b@ ps.println(s);@b@ ps.println(" pst=conn.prepareStatement(sql);");@b@ for (int i = 0; i < ui.getColumns().size(); i++) {@b@ DbField field = (DbField) ui.getColumns().get(i);@b@ ps.println(" pst.set" + field.getJavaObjectType()+ "(" + String.valueOf(i + 1) + "," + field.getJavaParaVariableName() + ");");@b@ }@b@ ps.println(" rs=pst.executeQuery();");@b@ ps.println(" if(!rs.next()){");@b@ s = " String error = \"DAOFindBy"+ ui.getPartMethodName() + ": " + this.msModelClassName + "(\"+";@b@ for (int i = 0; i < ui.getColumns().size(); i++) {@b@ if (i > 0) {@b@ s += "+\",\"+";@b@ }@b@ DbField field = (DbField) ui.getColumns().get(i);@b@ s += field.getJavaParaVariableName();@b@ }@b@ s = s + "+\") not found\";";@b@ ps.println(s);@b@ ps.println(" throw new Exception (error);");@b@ ps.println(" }else{");@b@ s = " pk=new " + this.msPrimaryKeyClassName + "(";@b@ for (int i = 0; i < pTable.getPrimaryKey().size(); i++) {@b@ DbField field = (DbField) pTable.getPrimaryKey().get(i);@b@ if (i > 0) {@b@ s = s + ",";@b@ }@b@ s = s + "rs.get" + field.getJavaObjectType() + "(\""+ field.getFieldName() + "\")";@b@ }@b@ s = s + ");";@b@ ps.println(s);@b@ ps.println(" }");@b@ ps.println(" }catch(Exception e){");@b@ ps.println(" log.error(\"DAO findByPrimaryKey Error\",e);");@b@ ps.println(" throw e;");@b@ ps.println(" }finally{");@b@ ps.println(" DBUtil.close(rs,pst,conn);");@b@ ps.println(" }");@b@ ps.println(" return pk;");@b@ ps.println(" }");@b@@b@ }@b@ //clear@b@@b@ //fetch@b@ ps.println();@b@ generateUtilFunc(pTable,ps);@b@ @b@ //end@b@ ps.println("}");@b@ ps.close();@b@ fos.close();@b@ }@b@@b@ private void generateUtilFunc(DbTable pTable,PrintStream ps) throws Exception {@b@ //get model from rs@b@ ps.println(" public static " + this.msModelClassName + " fetch(ResultSet rs) throws Exception{");@b@@b@ ps.println(" " + this.msModelClassName + " p"+ this.msModelClassName + "=new " + this.msModelClassName + "();");@b@ for (int i = 0; i < pTable.getFields().size(); i++) {@b@ DbField field = (DbField) pTable.getFields().get(i);@b@ ps.println(" p" + this.msModelClassName + "." + field.getJavaSetMethodName() + "(rs.get" + field.getJavaObjectType() + "(\"" + field.getFieldName() + "\"));");@b@ }@b@ ps.println(" return p" + this.msModelClassName + ";");@b@ ps.println(" }");@b@ }@b@@b@ //Primary key class@b@ private void generatePrimaryKeyClass(DbTable pTable) throws Exception {@b@ //Primary key can only be String and long data type@b@ Vector v = pTable.getPrimaryKey();@b@ int i;@b@ FileOutputStream fos = new FileOutputStream(msPackagePath + "/" + pTable.getConvertedJavaName().toLowerCase() + "/" + this.msPrimaryKeyClassName + ".java");@b@ PrintStream ps = new PrintStream(fos);@b@ ps.println("package " + this.msPackageName + "."+ pTable.getConvertedJavaName().toLowerCase() + ";");@b@ ps.println();@b@@b@ ps.println("import java.io.Serializable;");@b@ ps.println();@b@@b@ ps.println(this.getFileDescription(pTable));@b@ @b@ ps.println("public class " + this.msPrimaryKeyClassName + " implements Serializable{");@b@ for (i = 0; i < v.size(); i++) {@b@ ps.println(" private " + ((DbField) v.get(i)).getJavaDataType()+ " " + ((DbField) v.get(i)).getJavaMemberVariableName()+ ";");@b@ }@b@ //construct@b@ String s = "";@b@ s = " public " + this.msPrimaryKeyClassName + "(";@b@ for (i = 0; i < v.size(); i++) {@b@ if (i > 0) {@b@ s += ",";@b@ }@b@ s += ((DbField) v.get(i)).getJavaDataType() + " "@b@ + ((DbField) v.get(i)).getJavaParaVariableName();@b@ }@b@ s += "){";@b@ ps.println(s);@b@ for (i = 0; i < v.size(); i++) {@b@ ps.println(" "+ ((DbField) v.get(i)).getJavaMemberVariableName() + "=" + ((DbField) v.get(i)).getJavaParaVariableName() + ";");@b@ }@b@ ps.println(" }");@b@ //get method@b@ for (i = 0; i < v.size(); i++) {@b@ ps.println(" public " + ((DbField) v.get(i)).getJavaDataType() + " " + ((DbField) v.get(i)).getJavaGetMethodName() + "(){return "+ ((DbField) v.get(i)).getJavaMemberVariableName() + ";}");@b@ }@b@ //hash code@b@ ps.println(" public int hashCode(){");@b@ s = " return (";@b@ for (i = 0; i < v.size(); i++) {@b@ if (i > 0) {@b@ s += "+";@b@ }@b@ s += "String.valueOf("@b@ + ((DbField) v.get(i)).getJavaMemberVariableName() + ")";@b@ }@b@ s += ").hashCode();";@b@ ps.println(s);@b@ ps.println(" }");@b@@b@ //toString@b@ ps.println(" public String toString(){");@b@ s = " return (";@b@ for (i = 0; i < v.size(); i++) {@b@ if (i > 0) {@b@ s += "+";@b@ }@b@ s += "String.valueOf("@b@ + ((DbField) v.get(i)).getJavaMemberVariableName() + ")";@b@ }@b@ s += ");";@b@ ps.println(s);@b@ ps.println(" }");@b@@b@ //equals@b@ ps.println(" public boolean equals(Object obj) {");@b@ ps.println(" if (!this.getClass().equals(obj.getClass())) {return false;}");@b@ for (i = 0; i < v.size(); i++) {@b@ if ("String".equals(((DbField) v.get(i)).getJavaDataType())) {@b@ ps.println(" if(!" + ((DbField) v.get(i)).getJavaMemberVariableName()+ ".equals(((" + this.msPrimaryKeyClassName + ")obj)."+ ((DbField) v.get(i)).getJavaMemberVariableName()+ ")){return false;}");@b@ } else {@b@ ps.println(" if("+ ((DbField) v.get(i)).getJavaMemberVariableName()+ "!=((" + this.msPrimaryKeyClassName + ")obj)."+ ((DbField) v.get(i)).getJavaMemberVariableName()+ "){return false;}");@b@ }@b@ }@b@ ps.println(" return true;");@b@ ps.println(" }");@b@ ps.println("}");@b@ ps.close();@b@ fos.close();@b@ }@b@ @b@}
2. DbTable、DbField数据库表字段类,UniqueIndex唯一索引类
package test.dao;@b@@b@import java.util.*;@b@@b@public class DbTable {@b@ @b@ public DbTable() {@b@ mvFields = new Vector();@b@ mvUniIndex = new Vector();@b@ }@b@@b@ private String msTableName = null;;@b@@b@ private Vector mvFields = null;@b@@b@ private Vector mvUniIndex = null;@b@@b@ public void setTableName(String pTableName) {@b@ msTableName = pTableName;@b@ }@b@@b@ public void addUniIndex(UniqueIndex uIndex) {@b@ Vector vIndex = uIndex.getColumns();@b@ if (vIndex != null) {@b@ boolean isPK = true;@b@ for (int i = 0; i < vIndex.size(); i++) {@b@ if (!((DbField) vIndex.get(i)).isPrimarykey()) {@b@ isPK = false;@b@ }@b@ }@b@ if (!isPK) {@b@ mvUniIndex.add(uIndex);@b@ }@b@ }@b@ }@b@@b@ public Vector getAllUniIndex() {@b@ return mvUniIndex;@b@ }@b@@b@ public String getTableName() {@b@ return msTableName;@b@ }@b@@b@ public String getConvertedJavaName() {@b@ return this.convertTableName(msTableName);@b@ }@b@@b@ public Vector getNonPrimaryKeyField() {@b@ Vector v = new Vector();@b@ for (int i = 0; i < mvFields.size(); i++) {@b@ if (!((DbField) mvFields.get(i)).isPrimarykey()) {@b@ v.add(mvFields.get(i));@b@ }@b@ }@b@ return v;@b@ }@b@@b@ public Vector getPrimaryKey() {@b@ Vector v = new Vector();@b@ for (int i = 0; i < mvFields.size(); i++) {@b@ if (((DbField) mvFields.get(i)).isPrimarykey()) {@b@ v.add(mvFields.get(i));@b@ }@b@ }@b@ return v;@b@ }@b@@b@ public void addField(DbField pField) {@b@ mvFields.addElement(pField);@b@ }@b@@b@ public Vector getFields() {@b@ return mvFields;@b@ }@b@@b@ public DbField getField(String sFieldName) {@b@ DbField field = null;@b@ for (int i = 0; i < mvFields.size(); i++) {@b@ if (((DbField) mvFields.get(i)).getFieldName().equals(sFieldName)) {@b@ field = (DbField) mvFields.get(i);@b@ }@b@ }@b@ return field;@b@ }@b@@b@ private String convertTableName(String sName) {@b@ if (sName == null) {@b@ return null;@b@ }@b@ if (sName.length() <= 1) {@b@ return sName.toUpperCase();@b@ }@b@ String tmp;@b@ String src;@b@ String dst = "";@b@ src = sName.toUpperCase().replaceAll("PMS_", "");@b@ boolean beFirst = true;@b@ boolean beUpCase = true;@b@ while (src.length() > 0) {@b@ tmp = src.substring(0, 1);@b@ src = src.substring(1, src.length());@b@ if ("T".equals(tmp.toUpperCase())) {@b@ if (beFirst) {@b@ continue;//skip prefix of table name;eg T_customer;@b@ }@b@ }@b@ if ("_".equals(tmp)) {@b@ beUpCase = true;@b@ continue;@b@ }@b@ if (beUpCase) {@b@ dst += tmp.toUpperCase();@b@ } else {@b@ dst += tmp.toLowerCase();@b@ }@b@ beUpCase = false;@b@ beFirst = false;@b@ }@b@ return dst;@b@ }@b@@b@}
package test.dao;@b@@b@import test.dao.Tools;@b@@b@public class DbField {@b@ @b@ private String msFieldName = null;@b@ private int mnDataType = -1;@b@ private boolean mbPrimaryKey = false;@b@ private String msDefaultValue = null;@b@@b@ final static int typeUnknown=-1;@b@ final static int typeString = 0;@b@ final static int typeLong = 1;@b@ final static int typeDouble = 2;@b@ final static int typeDate = 3;@b@ final static int typeBlob = 4;@b@ final static int typeClob = 5;@b@@b@ public boolean isTimeStamp() {@b@ return "TIME_STAMP".equalsIgnoreCase(msFieldName);@b@ }@b@@b@ public void setPrimaryKey(boolean bePrimaryKey) {@b@ mbPrimaryKey = bePrimaryKey;@b@ }@b@@b@ public boolean isPrimarykey() {@b@ return mbPrimaryKey;@b@ }@b@@b@ public String getFieldName() {@b@ return msFieldName;@b@ }@b@@b@ public DbField(String pFieldName, String pDataType, int pDecDigits)@b@ throws Exception {@b@ msFieldName = pFieldName;@b@ mnDataType = mapSqlDataType(pDataType, pDecDigits);@b@ }@b@@b@ public String getJavaObjectType() {@b@ switch (mnDataType) {@b@ case typeString:@b@ return "String";@b@ case typeDouble:@b@ return "BigDecimal";@b@ case typeLong:@b@ if (mbPrimaryKey) {@b@ return "Long";@b@ } else {@b@ return "BigDecimal";@b@ }@b@ case typeDate:@b@ return "Timestamp";@b@ case typeBlob:@b@ return "BLOB";@b@ case typeClob:@b@ return "CLOB";@b@ default:@b@ return null;@b@ }@b@ }@b@@b@ public String getJavaDataType() {@b@ switch (mnDataType) {@b@ case typeString:@b@ return "String";@b@ case typeDouble:@b@ return "BigDecimal";@b@ case typeLong:@b@ if (mbPrimaryKey) {@b@ return "long";@b@ } else {@b@ return "BigDecimal";@b@ }@b@ case typeDate:@b@ return "Timestamp";@b@ case typeBlob:@b@ case typeClob:@b@ return "byte[]";@b@ default:@b@ return null;@b@ }@b@ }@b@@b@ private String getDataTypePrefix(int nDataType) {@b@ switch (nDataType) {@b@ case typeString:@b@ return "str";@b@ case typeDouble:@b@ return "dec";@b@ case typeLong:@b@ if (mbPrimaryKey) {@b@ return "lng";@b@ } else {@b@ return "dec";@b@ }@b@ case typeDate:@b@ return "date";@b@ case typeBlob:@b@ case typeClob:@b@ return "blob";@b@ default:@b@ return null;@b@ }@b@ }@b@@b@ public String getJavaMemberVariableName() throws Exception {@b@ return "m" + this.getDataTypePrefix(mnDataType)@b@ + this.convertFieldName(msFieldName);@b@ }@b@@b@ public String getJavaParaVariableName() throws Exception {@b@ return "p" + this.convertFieldName(msFieldName);@b@ }@b@@b@ public String getJavaGetMethodName() throws Exception {@b@ return "get" + this.convertFieldName(msFieldName);@b@ }@b@@b@ public String getJavaSetMethodName() throws Exception {@b@ return "set" + this.convertFieldName(msFieldName);@b@ }@b@@b@ public String getConvertFieldName() throws Exception {@b@ return this.convertFieldName(msFieldName);@b@ }@b@@b@ public String getDefaultValue() throws Exception {@b@ return msDefaultValue;@b@ }@b@@b@ private int mapSqlDataType(String sTypeName, int nDecDigits)@b@ throws Exception {@b@ if (sTypeName == null) {@b@ return typeUnknown;@b@ }@b@ String sUcaseType = sTypeName.toUpperCase();@b@ try {@b@ if ("NUMBER".equals(sUcaseType)) {@b@ if (nDecDigits > 0) {@b@ return typeDouble;@b@ } else {@b@ return typeLong;@b@ }@b@ }else if ("DATE".equals(sUcaseType)) {@b@ return typeDate;@b@ }else if ("CHAR".equals(sUcaseType) || "NCHAR".equals(sUcaseType)@b@ || "VARCHAR".equals(sUcaseType)@b@ || "NVARCHAR".equals(sUcaseType)@b@ || "NVARCHAR2".equals(sUcaseType)@b@ || "VARCHAR2".equals(sUcaseType)) {@b@ return typeString;@b@ }else if ("BLOB".equals(sUcaseType)) {@b@ return typeBlob;@b@ }else if ("CLOB".equals(sUcaseType)) {@b@ return typeClob;@b@ }@b@ return typeUnknown;@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ throw e;@b@ }@b@ }@b@@b@ public String convertFieldName(String sName) {@b@ if (sName == null) {@b@ return null;@b@ }@b@ if (sName.length() <= 1) {@b@ return sName.toUpperCase();@b@ }@b@ String tmp;@b@ String src;@b@ String dst = "";@b@ src = sName;@b@ boolean beFirst = true;@b@ boolean beUpCase = true;@b@ while (src.length() > 0) {@b@ tmp = src.substring(0, 1);@b@ src = src.substring(1, src.length());@b@ if ("_".equals(tmp)) {@b@ beUpCase = true;@b@ continue;@b@ }@b@ if (beUpCase) {@b@ dst += tmp.toUpperCase();@b@ } else {@b@ dst += tmp.toLowerCase();@b@ }@b@ beUpCase = false;@b@ beFirst = false;@b@ }@b@ return dst;@b@ }@b@ @b@ public void setDefaultValue(String sDefault) {@b@ String sVal = null;@b@ switch (mnDataType) {@b@ case typeString:@b@ if (sDefault == null) {@b@ sVal = "null";@b@ } else {@b@ sDefault = sDefault.replace('"', ' ');@b@ sDefault = sDefault.replace('\'', ' ');@b@ sDefault = sDefault.replace('(', ' ');@b@ sDefault = sDefault.replace(')', ' ');@b@ sDefault = sDefault.trim();@b@ if ("null".equals(sDefault)) {@b@ sDefault = null;@b@ sVal = "null";@b@ } else {@b@ sVal = "\"" + sDefault + "\"";@b@ }@b@ }@b@ break;@b@ case typeDouble:@b@ if (sDefault == null) {@b@ sVal = "null";@b@ } else if ("".equals(sDefault)) {@b@ sVal = "null";@b@ } else {@b@ sVal = "new BigDecimal(" + Tools.toDouble(sDefault) + ")";@b@ }@b@ break;@b@ case typeLong:@b@ if (mbPrimaryKey) {@b@ sVal = String.valueOf(Tools.toLong(sDefault));@b@ } else {@b@ if (sDefault == null) {@b@ sVal = "null";@b@ } else if ("".equals(sDefault)) {@b@ sVal = "null";@b@ } else {@b@ sVal = "BigDecimal.valueOf(" + Tools.toLong(sDefault) + "L)";@b@ }@b@ }@b@@b@ break;@b@ case typeDate:@b@ if (sDefault == null) {@b@ sVal = "null";@b@ } else if (sDefault.toUpperCase().indexOf("SYSDATE") >= 0) {@b@ sVal = "new Timestamp((new Date()).getTime())";@b@ } else {@b@ sVal = "null";@b@ }@b@ break;@b@ case typeBlob:@b@ sVal = "null";@b@ break;@b@ case typeClob:@b@ sVal = "null";@b@ break;@b@ default:@b@ sVal = "ERROR!";@b@ break;@b@ }@b@ msDefaultValue = sVal;@b@ }@b@}
3. Tools工具类
package test.dao;@b@@b@import java.math.BigDecimal;@b@import java.sql.Blob;@b@import java.sql.Connection;@b@import java.sql.PreparedStatement;@b@import java.sql.ResultSet;@b@import java.sql.Timestamp;@b@import java.text.DecimalFormat;@b@import java.text.NumberFormat;@b@import java.text.SimpleDateFormat;@b@import java.util.Calendar;@b@import java.util.Date;@b@@b@public class Tools {@b@ @b@ private static final int nMaxBufferSize = 15 * 1024;@b@@b@ public static String toGB(String inStr) {@b@ return (inStr);@b@ }@b@@b@ public static String fromGB(String inStr) {@b@ return (inStr);@b@ }@b@@b@ public static String fromGB(byte[] inBytes) {@b@ try {@b@ if (inBytes == null) {@b@ return "";@b@ }@b@@b@ return (new String(inBytes));@b@ } catch (Exception e) {@b@ return "";@b@ }@b@ }@b@@b@ public static long toLong(String inStr) {@b@ try {@b@ if (inStr == null) {@b@ return 0;@b@ } else {@b@ inStr = replace(inStr, "\n", "");@b@ inStr = replace(inStr, " ", "");@b@ return Long.parseLong(inStr);@b@ }@b@ } catch (Exception e) {@b@ return 0;@b@ }@b@ }@b@@b@ public static String toString(Object obj) {@b@ return (obj == null) ? "" : obj.toString();@b@ }@b@@b@ public static int toInteger(String inStr) {@b@ try {@b@ if (inStr == null) {@b@ return 0;@b@ } else {@b@ return Integer.parseInt(inStr);@b@ }@b@ } catch (Exception e) {@b@ return 0;@b@ }@b@ }@b@@b@ public static double toDouble(String inStr) {@b@ try {@b@ if (inStr == null) {@b@ return 0;@b@ } else {@b@ String src = replace(inStr, ",", "");@b@ return Double.parseDouble(src);@b@ }@b@ } catch (Exception e) {@b@ return 0;@b@ }@b@ }@b@ @b@ public static String getYearOne(String dateStr){@b@ String splitStr[]=dateStr.split("-");@b@ int year=Integer.parseInt(splitStr[0]);@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.set(Calendar.YEAR, year);@b@ calendar.set(Calendar.MONTH, 0);@b@ calendar.set(Calendar.DAY_OF_MONTH, 1);@b@ return toString(calendar.getTime());@b@ }@b@ @b@ public static String getMonthEnd(String dateStr){@b@ String splitStr[]=dateStr.split("-");@b@ int year=Integer.parseInt(splitStr[0]);@b@ int month=Integer.parseInt(splitStr[1]);@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.set(Calendar.YEAR, year);@b@ calendar.set(Calendar.MONTH, month);@b@ calendar.set(Calendar.DAY_OF_MONTH, 0);@b@ return toString(calendar.getTime());@b@ }@b@ @b@ public static float toFloat(String inStr) {@b@ try {@b@ if (inStr == null) {@b@ return 0;@b@ } else {@b@ return Float.parseFloat(inStr);@b@ }@b@ } catch (Exception e) {@b@ return 0;@b@ }@b@ }@b@@b@ public static BigDecimal toBigDecimal(String inStr) {@b@ try {@b@ if (inStr == null) {@b@ return null;@b@ } else {@b@ return new BigDecimal(inStr);@b@ }@b@ } catch (Exception e) {@b@ return null;@b@ }@b@ }@b@@b@ public static BigDecimal toBigDecimal(String inStr, double base, int scale) {@b@ try {@b@ if (inStr == null) {@b@ return null;@b@ } else {@b@ return new BigDecimal(inStr).divide(new BigDecimal(base), scale, BigDecimal.ROUND_HALF_UP);@b@ }@b@ } catch (Exception e) {@b@ return null;@b@ }@b@ }@b@@b@ public static long tolong(BigDecimal val) {@b@ if (val == null) {@b@ return 0;@b@ } else {@b@ return Long.parseLong("" + val);@b@ }@b@ }@b@@b@ public static double round(double dOrigin, int nCount, boolean bDischarge) {@b@ long lTemp = (long) Math.pow(10, nCount);@b@ if (bDischarge == true) {@b@ return (long) (dOrigin * lTemp) / (double) lTemp;@b@ } else {@b@ return Math.round(dOrigin * lTemp) / (double) lTemp;@b@ }@b@ }@b@@b@ public static boolean isZero(double value, int digits) {@b@ return round(value, digits, false) == 0;@b@ }@b@@b@ public static String toString(double val) {@b@ String str = NumberFormat.getInstance().format(val);@b@ return replace(str, ",", "");@b@ }@b@@b@ public static String toString(double val, int nCount, boolean bDischarge) {@b@ return toString(round(val, nCount, bDischarge));@b@ }@b@@b@ private static byte[] blobToBytes(Blob pBlob) throws Exception {@b@ byte[] buffer = null;@b@ if (pBlob != null) {@b@ buffer = pBlob.getBytes((long) 1, (int) pBlob.length());@b@ }@b@ return buffer;@b@ }@b@@b@ public static Timestamp toTimestamp(String sDate) {@b@ if (sDate == null || "".equals(sDate)) {@b@ return null;@b@ }@b@ return Timestamp.valueOf(sDate + " 00:00:00.000000000");@b@ }@b@@b@ public static Timestamp toTimestampEnd(String sDate) {@b@ if (sDate == null || "".equals(sDate)) {@b@ return null;@b@ }@b@ return Timestamp.valueOf(sDate + " 23:59:59.999999999");@b@ }@b@@b@ public static java.util.Date toDate(String sDate) throws java.text.ParseException {@b@ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@ return simpleDateFormat.parse(sDate);@b@ }@b@@b@ public static java.util.Date toDate(int year, int month, int date, int hour, int min, int sec) {@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.set(year, month - 1, date, hour, min, sec);@b@ return calendar.getTime();@b@ }@b@@b@ public static java.util.Date toDate(int year, int month, int date) {@b@ return toDate(year, month, date, 0, 0, 0);@b@ }@b@@b@ public static String toString(java.util.Date date) {@b@ if (date == null) {@b@ return null;@b@ }@b@ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@ return simpleDateFormat.format(date);@b@ }@b@@b@ public static int getDateAttr(java.util.Date date, String type) {@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(date);@b@ if ("YEAR".equals(type)) {@b@ return calendar.get(Calendar.YEAR);@b@ } else if ("MONTH".equals(type)) {@b@ return calendar.get(Calendar.MONTH) + 1;@b@ } else if ("DATE".equals(type)) {@b@ return calendar.get(Calendar.DAY_OF_MONTH);@b@ } else if ("HOUR".equals(type)) {@b@ return calendar.get(Calendar.HOUR);@b@ } else if ("MIN".equals(type)) {@b@ return calendar.get(Calendar.YEAR);@b@ } else if ("SEC".equals(type)) {@b@ return calendar.get(Calendar.SECOND);@b@ }@b@ return -1;@b@ }@b@@b@ public static int getDateAttr(long date, String type) {@b@ return getDateAttr(new java.util.Date(date), type);@b@ }@b@@b@ public static String getChineseDate(java.util.Date date) {@b@ String sDate = toString(date);@b@ if (sDate == null || "".equals(sDate)) {@b@ return sDate;@b@ } else {@b@ return sDate.substring(0, 4) + "_" + sDate.substring(5, 7) + "_" + sDate.substring(8) + "_";@b@ }@b@ }@b@@b@ public static long DateBetween(Date date_fst, Date date_lst) {@b@ return (date_lst.getTime() - date_fst.getTime()) / 86400000;@b@ }@b@@b@ public static int MonthBetween(Date date_fst, Date date_lst) {@b@ int nYear, nMonth, nDate;@b@ Calendar cld_fst = Calendar.getInstance();@b@ Calendar cld_lst = Calendar.getInstance();@b@@b@ cld_fst.setTime(date_fst);@b@ cld_lst.setTime(date_lst);@b@@b@ nYear = cld_lst.get(Calendar.YEAR) - cld_fst.get(Calendar.YEAR);@b@ nMonth = cld_lst.get(Calendar.MONTH) - cld_fst.get(Calendar.MONTH);@b@ nDate = cld_lst.get(Calendar.DATE) - cld_fst.get(Calendar.DATE);@b@@b@ return nYear * 12 + nMonth + (nDate >= 0 ? 0 : -1);@b@ }@b@@b@ public static Date DateAdd(Date date, int interval) {@b@ Date rt = new Date();@b@ rt.setTime(date.getTime() + 86400000 * interval);@b@ return rt;@b@ }@b@@b@ public static Date MonthAdd(Date date, int interval) {@b@ Calendar cld = Calendar.getInstance();@b@ cld.setTime(date);@b@ cld.add(Calendar.MONTH, interval);@b@ return cld.getTime();@b@ }@b@@b@ public static String NullToBlank(String inStr) {@b@ return inStr == null ? "" : inStr;@b@ }@b@ @b@ public static String NullToBlank(Object inStr) {@b@ return inStr == null ? "" : inStr.toString();@b@ }@b@ @b@ public static String BlankToNull(String value) {@b@ if (value == null) {@b@ return null;@b@ }@b@ String rt = value.trim();@b@ return rt.length() == 0 ? null : rt;@b@ }@b@@b@ public static String replace(String str, String oldSub, String newSub) {@b@ if (str == null || oldSub == null) {@b@ return str;@b@ }@b@@b@ int start = 0, next;@b@ StringBuffer buff = new StringBuffer();@b@@b@ next = str.indexOf(oldSub, start);@b@ while (next > -1) {@b@ buff.append(str.substring(start, next));@b@ buff.append(newSub == null ? "" : newSub);@b@ start = next + oldSub.length();@b@ next = str.indexOf(oldSub, start);@b@ }@b@ buff.append(str.substring(start));@b@ return buff.toString();@b@ }@b@@b@ public static String replaceIgnoreCase(String str, String oldSub, String newSub) {@b@ if (str == null || oldSub == null) {@b@ return str;@b@ }@b@@b@ String STR = str.toUpperCase();@b@ String OLDSUB = oldSub.toUpperCase();@b@@b@ int start = 0, next;@b@ StringBuffer buff = new StringBuffer();@b@@b@ next = STR.indexOf(OLDSUB, start);@b@ while (next > -1) {@b@ buff.append(str.substring(start, next));@b@ buff.append(newSub == null ? "" : newSub);@b@ start = next + oldSub.length();@b@ next = STR.indexOf(OLDSUB, start);@b@ }@b@ buff.append(str.substring(start));@b@ return buff.toString();@b@ }@b@ @b@ public static String lpad(String val, int length, String chs) {@b@ if (val == null) {@b@ return null;@b@ }@b@ if (val.length() > length) {@b@ return val.substring(0, val.length());@b@ } else if (val.length() == length) {@b@ return val;@b@ }@b@@b@ String ch = chs;@b@ if (chs == null || "".equals(chs)) {@b@ ch = " ";@b@ }@b@@b@ StringBuffer buff = new StringBuffer();@b@ int pos = 0, llen = length - val.length(), k;@b@ while (pos < llen) {@b@ k = Math.min(ch.length(), llen - pos);@b@ buff.append(ch.substring(0, k));@b@ pos += k;@b@ }@b@ buff.append(val);@b@ return buff.toString();@b@ }@b@@b@ public static String rpad(String val, int length, String chs) {@b@ if (val == null) {@b@ return null;@b@ }@b@ if (val.length() > length) {@b@ return val.substring(0, val.length());@b@ } else if (val.length() == length) {@b@ return val;@b@ }@b@ String ch = chs;@b@ if (chs == null || "".equals(chs)) {@b@ ch = " ";@b@ }@b@@b@ StringBuffer buff = new StringBuffer();@b@ int pos = 0, rlen = length - val.length(), k;@b@ buff.append(val);@b@ while (pos < rlen) {@b@ k = Math.min(ch.length(), rlen - pos);@b@ buff.append(ch.substring(0, k));@b@ pos += k;@b@ }@b@ return buff.toString();@b@ }@b@@b@ public static String jointStrings(String[] array, String token) {@b@ StringBuffer buff = new StringBuffer();@b@ for (int i = 0; i < array.length - 1; i++) {@b@ buff.append(array[i]);@b@ buff.append(token);@b@ }@b@ if (array.length > 0) {@b@ buff.append(array[array.length - 1]);@b@ }@b@ return buff.toString();@b@ }@b@@b@ public static String jointStrings(String[] array, String tag_head, String tag_tail) {@b@ StringBuffer buff = new StringBuffer();@b@ for (int i = 0; i < array.length; i++) {@b@ buff.append(tag_head);@b@ buff.append(array[i]);@b@ buff.append(tag_tail);@b@ }@b@ return buff.toString();@b@ }@b@@b@ public static String[] splitStrings(String val, String token) {@b@ int curr = 0, k = 0;@b@ int l_value = val.length();@b@ int l_token = token.length();@b@ int next = val.indexOf(token, curr);@b@@b@ while (next >= 0) {@b@ k++;@b@ curr = next + l_token;@b@ next = val.indexOf(token, curr);@b@ }@b@ String[] array = new String[k + 1];@b@@b@ curr = 0;@b@ next = val.indexOf(token, curr);@b@ for (k = 0; k < array.length; k++) {@b@ if (next < 0) {@b@ array[k] = val.substring(curr);@b@ } else {@b@ array[k] = val.substring(curr, next);@b@ }@b@ curr = next + l_token;@b@ next = val.indexOf(token, curr);@b@ }@b@@b@ return array;@b@ }@b@@b@ public static String getCard18Tail(String card18) throws Exception {@b@ int[] wi = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };@b@ char[] tail = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };@b@ int i_lst = 0, i;@b@ for (i = 0; i < 17; i++) {@b@ i_lst = i_lst + (card18.charAt(i) - '0') * wi[i];@b@ }@b@ return String.valueOf(tail[i_lst % 11]);@b@ }@b@@b@ public static boolean billno_check(String billno) throws Exception {@b@ double num;@b@@b@ if ("".equals(NullToBlank(billno))) {@b@ return false;@b@ }@b@ if (billno.length() <= 5) {@b@ return false;@b@ }@b@@b@ billno = "0000000000".substring(0, 15 - billno.length()) + billno;@b@ num = Double.parseDouble(billno.substring(0, 10)) % 97;@b@ num = (num * 1000 + Double.parseDouble(billno.substring(12, 15))) % 97;@b@ num = (num * 100 + Double.parseDouble(billno.substring(10, 12))) % 97;@b@ if (num == 1) {@b@ return true;@b@ } else {@b@ return false;@b@ }@b@ }@b@ @b@ public static String trans(double doubleValue){@b@ String retValue = ""; @b@ DecimalFormat df = new DecimalFormat(); @b@ df.setMinimumFractionDigits(2); @b@ df.setMaximumFractionDigits(2); @b@ retValue = df.format(doubleValue); @b@ retValue = retValue.replaceAll(",",""); @b@ return retValue; @b@ }@b@ public static String transthree(double doubleValue){@b@ String retValue = ""; @b@ DecimalFormat df = new DecimalFormat(); @b@ df.setMinimumFractionDigits(1); @b@ df.setMaximumFractionDigits(1); @b@ retValue = df.format(doubleValue); @b@ retValue = retValue.replaceAll(",",""); @b@ return retValue; @b@ }@b@ @b@ public static String transtwo(Object str){@b@ String retValue = ""; @b@ DecimalFormat df = new DecimalFormat(); @b@ df.setMinimumFractionDigits(2); @b@ df.setMaximumFractionDigits(2); @b@ retValue = df.format(str); @b@ //retValue = retValue.replaceAll(",",""); @b@ return retValue; @b@ }@b@ @b@ public static void main(String[] args) throws Exception {@b@ Date date = Tools.toDate("2007-03-01");@b@ System.out.println(Tools.toString(MonthAdd(date, 12)));@b@ @b@ System.out.println(billno_check("005454512344050"));@b@ System.out.println(getYearOne("2007-12-11"));@b@ System.out.println(getMonthEnd("2007-12-11"));@b@ }@b@}
三、运行结果
通过运行DAOGenerator类,生成AccessLogDAO.java、AccessLogPK.java、AccessLogVO.java类
1. AccessLogDAO.java类
package com.xwood.pms.accesslog;@b@@b@import java.sql.*;@b@import java.util.*;@b@import java.util.Date;@b@import java.math.*;@b@import org.apache.commons.logging.*;@b@@b@import test.dao;@b@@b@/**@b@ * Title: VO Class @b@ * Description: vo java beans and servlets@b@ * Copyright: Copyright (c) 2018@b@ * Company: tplife@b@ * @author: auto@b@ * @version 1.0@b@ * Relate table: ACCESS_LOG@b@ * Auto Generated by VOGen@b@ * Generate Date:Fri Oct 05 17:58:25 CST 2018@b@ */@b@public class AccessLogDAO{@b@ private static Log log =LogFactory.getLog(AccessLogDAO.class);@b@@b@ public static AccessLogPK create(AccessLogVO pAccessLogVO) throws Exception{@b@ PreparedStatement pst=null;@b@ String sql=null;@b@ Connection conn=DBUtil.getConnection();@b@ AccessLogPK pk=null;@b@ try{@b@ sql="insert into ACCESS_LOG (ACC_IP,ACC_DATE,ACC_URL,ACC_BROWSER,ACC_ID,ACC_STATE,STAT_DATE) values(?,?,?,?,?,?,?)";@b@ pst=conn.prepareStatement(sql);@b@ int pos=1;@b@ pst.setString(pos,pAccessLogVO.getAccIp());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccDate());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccUrl());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccBrowser());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccId());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccState());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getStatDate());@b@ pos++;@b@ int nRow=pst.executeUpdate();@b@ if(nRow!=1){@b@ String error = "DAOCreate: AccessLogDAO("+pAccessLogVO.toString()+") not found";@b@ throw new Exception (error);@b@ }@b@ }catch(Exception e){@b@ log.error("DAO Create Error",e);@b@ throw e;@b@ }finally{@b@ DBUtil.close(null,pst,conn);@b@ }@b@ pk=new AccessLogPK(pAccessLogVO.getAccId());@b@ return pk;@b@ }@b@@b@ public static void batchCreate(List pAccessLogVOList) throws Exception{@b@ if (pAccessLogVOList==null || pAccessLogVOList.size()==0){@b@ return;@b@ }@b@ PreparedStatement pst=null;@b@ String sql=null;@b@ Connection conn=DBUtil.getConnection();@b@ AccessLogVO vo=null;@b@ try{@b@ sql="insert into ACCESS_LOG (ACC_IP,ACC_DATE,ACC_URL,ACC_BROWSER,ACC_ID,ACC_STATE,STAT_DATE) values(?,?,?,?,?,?,?)";@b@ pst=conn.prepareStatement(sql);@b@ for(int i=0;i<pAccessLogVOList.size();i++){@b@ vo=(AccessLogVO) pAccessLogVOList.get(i);@b@ int pos=1;@b@ pst.setString(pos,vo.getAccIp());@b@ pos++;@b@ pst.setString(pos,vo.getAccDate());@b@ pos++;@b@ pst.setString(pos,vo.getAccUrl());@b@ pos++;@b@ pst.setString(pos,vo.getAccBrowser());@b@ pos++;@b@ pst.setString(pos,vo.getAccId());@b@ pos++;@b@ pst.setString(pos,vo.getAccState());@b@ pos++;@b@ pst.setString(pos,vo.getStatDate());@b@ pos++;@b@ pst.addBatch();@b@ }@b@ int nRow[]=pst.executeBatch();@b@ for(int j=0;j<nRow.length;j++){@b@ if(nRow[j]!=1 && nRow[j]!=-2){@b@ String error = "DAOBatchCreate: AccessLogDAO("+((AccessLogVO) pAccessLogVOList.get(j)).toString()+") not found";@b@ throw new Exception (error);@b@ }@b@ }@b@ }catch(Exception e){@b@ log.error("DAO Batch Create Error",e);@b@ throw e;@b@ }finally{@b@ DBUtil.close(null,pst,conn);@b@ }@b@ }@b@@b@ public static void store(AccessLogVO pAccessLogVO) throws Exception{@b@ PreparedStatement pst=null;@b@ String sql=null;@b@ Connection conn=DBUtil.getConnection();@b@ try{@b@ sql="update ACCESS_LOG set ACC_IP = ? ,ACC_DATE = ? ,ACC_URL = ? ,ACC_BROWSER = ? ,ACC_STATE = ? ,STAT_DATE = ? where ACC_ID = ? ";@b@ pst=conn.prepareStatement(sql);@b@ int pos=1;@b@ pst.setString(pos,pAccessLogVO.getAccIp());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccDate());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccUrl());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccBrowser());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccState());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getStatDate());@b@ pos++;@b@ pst.setString(pos,pAccessLogVO.getAccId());@b@ pos++;@b@ int nRow=pst.executeUpdate();@b@ if(nRow!=1){@b@ String error = "DAOStore: AccessLogDAO("+pAccessLogVO.toString()+") not found";@b@ throw new Exception (error);@b@ }@b@ }catch(Exception e){@b@ log.error("DAO Store Error",e);@b@ throw e;@b@ }finally{@b@ DBUtil.close(null,pst,conn);@b@ }@b@ }@b@@b@ public static void batchStore(List pAccessLogVOList) throws Exception{@b@ if (pAccessLogVOList==null || pAccessLogVOList.size()==0){@b@ return;@b@ }@b@ PreparedStatement pst=null;@b@ String sql=null;@b@ Connection conn=DBUtil.getConnection();@b@ AccessLogVO vo=null;@b@ try{@b@ sql="update ACCESS_LOG set ACC_IP = ? ,ACC_DATE = ? ,ACC_URL = ? ,ACC_BROWSER = ? ,ACC_STATE = ? ,STAT_DATE = ? where ACC_ID = ? ";@b@ pst=conn.prepareStatement(sql);@b@ for(int i=0;i<pAccessLogVOList.size();i++){@b@ vo=(AccessLogVO) pAccessLogVOList.get(i);@b@ int pos=1;@b@ pst.setString(pos,vo.getAccIp());@b@ pos++;@b@ pst.setString(pos,vo.getAccDate());@b@ pos++;@b@ pst.setString(pos,vo.getAccUrl());@b@ pos++;@b@ pst.setString(pos,vo.getAccBrowser());@b@ pos++;@b@ pst.setString(pos,vo.getAccState());@b@ pos++;@b@ pst.setString(pos,vo.getStatDate());@b@ pos++;@b@ pst.setString(pos,vo.getAccId());@b@ pos++;@b@ pst.addBatch();@b@ }@b@ int nRow[]=pst.executeBatch();@b@ for(int j=0;j<nRow.length;j++){@b@ if(nRow[j]!=1 && nRow[j]!=-2){@b@ String error = "DAOBatchStore: AccessLogDAO("+((AccessLogVO) pAccessLogVOList.get(j)).toString()+") not found";@b@ throw new Exception (error);@b@ }@b@ }@b@ }catch(Exception e){@b@ log.error("DAO Batch Store Error",e);@b@ throw e;@b@ }finally{@b@ DBUtil.close(null,pst,conn);@b@ }@b@ }@b@@b@ public static AccessLogVO load(AccessLogPK pAccessLogPK) throws Exception{@b@ PreparedStatement pst=null;@b@ ResultSet rs=null;@b@ String sql=null;@b@ Connection conn=DBUtil.getConnection();@b@ AccessLogVO pAccessLogVO=null;@b@ try{@b@ sql="select * from ACCESS_LOG where ACC_ID = ? ";@b@ pst=conn.prepareStatement(sql);@b@ pst.setString(1,pAccessLogPK.getAccId());@b@ rs=pst.executeQuery();@b@ if(!rs.next()){@b@ String error = "DAOLoad: AccessLogDAO("+pAccessLogPK.toString()+") not found";@b@ throw new Exception (error);@b@ }else{@b@ pAccessLogVO=fetch(rs);@b@ }@b@ }catch(Exception e){@b@ log.error("DAO Load Error",e);@b@ throw e;@b@ }finally{@b@ DBUtil.close(rs,pst,conn);@b@ }@b@ return pAccessLogVO;@b@ }@b@@b@ public static void remove(AccessLogPK pAccessLogPK) throws Exception{@b@ PreparedStatement pst=null;@b@ String sql=null;@b@ Connection conn=DBUtil.getConnection();@b@ try{@b@ sql="delete from ACCESS_LOG where ACC_ID = ? ";@b@ pst=conn.prepareStatement(sql);@b@ pst.setString(1,pAccessLogPK.getAccId());@b@ int nRow=pst.executeUpdate();@b@ if(nRow!=1){@b@ String error = "DAORemove: AccessLogDAO("+pAccessLogPK.toString()+") not found";@b@ throw new Exception (error);@b@ }@b@ }catch(Exception e){@b@ log.error("DAO Remove Error",e);@b@ throw e;@b@ }finally{@b@ DBUtil.close(null,pst,conn);@b@ }@b@ }@b@@b@ public static AccessLogPK findByPrimaryKey(AccessLogPK pAccessLogPK) throws Exception{@b@ PreparedStatement pst=null;@b@ ResultSet rs=null;@b@ String sql=null;@b@ Connection conn=DBUtil.getConnection();@b@ try{@b@ sql="select ACC_ID from ACCESS_LOG where ACC_ID = ? ";@b@ pst=conn.prepareStatement(sql);@b@ pst.setString(1,pAccessLogPK.getAccId());@b@ rs=pst.executeQuery();@b@ if(!rs.next()){@b@ String error = "DAOFindByPrimaryKey: AccessLogDAO("+pAccessLogPK.toString()+") not found";@b@ throw new Exception (error);@b@ }@b@ }catch(Exception e){@b@ log.error("DAO findByPrimaryKey Error",e);@b@ throw e;@b@ }finally{@b@ DBUtil.close(rs,pst,conn);@b@ }@b@ return pAccessLogPK;@b@ }@b@@b@@b@ public static AccessLogVO fetch(ResultSet rs) throws Exception{@b@ AccessLogVO pAccessLogVO=new AccessLogVO();@b@ pAccessLogVO.setAccIp(rs.getString("ACC_IP"));@b@ pAccessLogVO.setAccDate(rs.getString("ACC_DATE"));@b@ pAccessLogVO.setAccUrl(rs.getString("ACC_URL"));@b@ pAccessLogVO.setAccBrowser(rs.getString("ACC_BROWSER"));@b@ pAccessLogVO.setAccId(rs.getString("ACC_ID"));@b@ pAccessLogVO.setAccState(rs.getString("ACC_STATE"));@b@ pAccessLogVO.setStatDate(rs.getString("STAT_DATE"));@b@ return pAccessLogVO;@b@ }@b@}
2. AccessLogVO.java类
package com.xwood.pms.accesslog;@b@@b@import java.io.Serializable;@b@import java.math.*;@b@import java.util.Date;@b@import java.sql.Timestamp;@b@@b@/**@b@ * Title: VO Class @b@ * Description: vo java beans and servlets@b@ * Copyright: Copyright (c) 2018@b@ * Company: tplife@b@ * @author: auto@b@ * @version 1.0@b@ * Relate table: ACCESS_LOG@b@ * Auto Generated by VOGen@b@ * Generate Date:Fri Oct 05 17:58:25 CST 2018@b@ */@b@public class AccessLogVO implements Serializable{@b@ private String mstrAccIp=null;@b@ public String getAccIp(){return mstrAccIp;}@b@ public void setAccIp(String pAccIp){mstrAccIp=pAccIp;}@b@ private String mstrAccDate=null;@b@ public String getAccDate(){return mstrAccDate;}@b@ public void setAccDate(String pAccDate){mstrAccDate=pAccDate;}@b@ private String mstrAccUrl=null;@b@ public String getAccUrl(){return mstrAccUrl;}@b@ public void setAccUrl(String pAccUrl){mstrAccUrl=pAccUrl;}@b@ private String mstrAccBrowser=null;@b@ public String getAccBrowser(){return mstrAccBrowser;}@b@ public void setAccBrowser(String pAccBrowser){mstrAccBrowser=pAccBrowser;}@b@ private String mstrAccId=null;@b@ public String getAccId(){return mstrAccId;}@b@ public void setAccId(String pAccId){mstrAccId=pAccId;}@b@ private String mstrAccState=null;@b@ public String getAccState(){return mstrAccState;}@b@ public void setAccState(String pAccState){mstrAccState=pAccState;}@b@ private String mstrStatDate=null;@b@ public String getStatDate(){return mstrStatDate;}@b@ public void setStatDate(String pStatDate){mstrStatDate=pStatDate;}@b@}
3. AccessLogPK类
package com.xwood.pms.accesslog;@b@@b@import java.io.Serializable;@b@@b@/**@b@ * Title: VO Class @b@ * Description: vo java beans and servlets@b@ * Copyright: Copyright (c) 2018@b@ * Company: tplife@b@ * @author: auto@b@ * @version 1.0@b@ * Relate table: ACCESS_LOG@b@ * Auto Generated by VOGen@b@ * Generate Date:Fri Oct 05 17:58:25 CST 2018@b@ */@b@public class AccessLogPK implements Serializable{@b@ private String mstrAccId;@b@ public AccessLogPK(String pAccId){@b@ mstrAccId=pAccId;@b@ }@b@ public String getAccId(){return mstrAccId;}@b@ public int hashCode(){@b@ return (String.valueOf(mstrAccId)).hashCode();@b@ }@b@ public String toString(){@b@ return (String.valueOf(mstrAccId));@b@ }@b@ public boolean equals(Object obj) {@b@ if (!this.getClass().equals(obj.getClass())) {return false;}@b@ if(!mstrAccId.equals(((AccessLogPK)obj).mstrAccId)){return false;}@b@ return true;@b@ }@b@}