⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbreverse.java

📁 自动生成JAVA-Struts网站的程序
💻 JAVA
字号:
/* *  Copyright(C) 2002 Azrul Azwar (pikapikane@yahoo.co.jp) * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2.1 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  You should have received a copy of the GNU Lesser General Public *  License along with this library; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * */package com.javanovic.karapansapi.strutsgen;import com.javanovic.karapansapi.xml.*;import java.io.IOException;import java.sql.*;import java.util.*;/** * FileName: DBReverse.java * Aug 19, 2002 8:43:58 PM * @author roel * @version 1.1.2 */public class DBReverse {  private Util util = new Util();  private String dbName = null;  public static void main(String[] args) {    DBReverse rev = new DBReverse();    if(args.length < 6) {      System.out.println(        "Syntax: java DBReverse dbms dbname url user password result-file.xml");      System.exit(1);    }    try {      PropertyMgr prop = new PropertyMgr("SQLType.properties");      Properties p = prop.getProperties();      String driver = p.getProperty(args[0] + ".jdbc.driver", "");      if(driver.equals("")) {        System.out.println("No definition for dbms " + args[0]);        System.out.println("It should be defined in SQLTypes.properties file");        System.exit(1);      }      Strutscreator tree = rev.reverse(args[0], driver, args[1], args[2], args[3], args[4]);      XMLHandler handler = new XMLHandlerImpl();      handler.save(args[5], tree);    } catch(IOException e) {      e.printStackTrace();    } catch(XMLException e) {      e.printStackTrace();    }  }  private String convert(int type) {    switch(type) {    case Types.CHAR:    case Types.VARCHAR:    case Types.LONGVARCHAR:      return "string";    case Types.NUMERIC:    case Types.DECIMAL:    case Types.DOUBLE:      return "double";    case Types.BIT:      return "boolean";    case Types.TINYINT:      return "byte";    case Types.SMALLINT:      return "short";    case Types.INTEGER:      return "int";    case Types.BIGINT:      return "long";    case Types.REAL:    case Types.FLOAT:      return "float";    case Types.BINARY:    case Types.VARBINARY:    case Types.CLOB:    case Types.BLOB:    case Types.LONGVARBINARY:      return "memo";    case Types.DATE:      return "date";    case Types.TIME:      return "date";    case Types.TIMESTAMP:      return "timestamp";    default :      return "string";    }  }  /**   * Reverse database   * @param dbms   * @param driverClass   * @param dbName   * @param url   * @param user   * @param password   * @return   */  public Strutscreator reverse(    String dbms,    String driverClass,    String dbName,    String url,    String user,    String password) {    ResultSet rs = null;    this.dbName = dbName;    Connection conn = null;    Strutscreator tree = getDefaultTree();    Database db = tree.getDatabase();    db.setDbms(dbms);    db.setDriver(driverClass);    db.setUrl(url);    db.setUser(user);    db.setPassword(password);    try {      Class.forName(driverClass);      conn = DriverManager.getConnection(url, user, password);      DatabaseMetaData dmd = conn.getMetaData();      rs = dmd.getTables(dbName, null, "%", null);      while(rs.next()) {        if(rs.getString(4).equalsIgnoreCase("table")) {          System.out.println("Importing table " + rs.getString(3));          tree.addBean(getTableDescription(dmd, rs.getString(3)));        }      }    } catch(ClassNotFoundException e) {      e.printStackTrace();    } catch(SQLException e) {      e.printStackTrace();    } finally {      close(rs);      close(conn);    }    return tree;  }   /**   * Populate fresh copy of beans from database   * @param tree   *   */  public void freshenBeans( Strutscreator tree) {    ResultSet rs = null;    Connection conn = null;    try {      // clear existing beans.      // could have an issue with object dependent on these beans?      tree.clearBean();      Database db = tree.getDatabase();      Class.forName(db.getDriver());      conn = DriverManager.getConnection(db.getUrl(),db.getUser(), db.getPassword());      DatabaseMetaData dmd = conn.getMetaData();      rs = dmd.getTables(dbName, null, "%", null);      while(rs.next()) {        if(rs.getString(4).equalsIgnoreCase("table")) {          System.out.println("Importing table " + rs.getString(3));          tree.addBean(getTableDescription(dmd, rs.getString(3)));        }      }    } catch(ClassNotFoundException e) {      e.printStackTrace();    } catch(SQLException e) {      e.printStackTrace();    } finally {      close(rs);      close(conn);    }  }  private Strutscreator getDefaultTree() {    Strutscreator tree = new Strutscreator();    Property prop = new Property();    prop.setAuthor("Author");    prop.setCompany("Company");    prop.setName("Application Name");    prop.setVersion("1.0");    tree.setProperty(prop);    Database db = new Database();    db.setMaximumConnections(30);    db.setMinimumConnections(5);    db.setJndi("");    tree.setDatabase(db);    Build build = new Build();    build.setCompiler("modern");    build.setDateFormat("dd/MM/yyyy");    build.setDirectory(".");    build.setPackage("com.javanovic");    build.setWarFileName("app.war");    build.setServletContainerName("Servlet Container");    build.setServletContainerDir(".");    tree.setBuild(build);    return tree;  }  private Bean getTableDescription(DatabaseMetaData dmd, String tableName) {    ResultSet rs = null;    Bean bean = new Bean();    try {      bean.setName(util.firstUpper(util.sql2javaName(tableName)));      if(!tableName.equals(util.java2sqlName(bean.getName()))) {        bean.setSqlName(tableName);      }      bean.setGenerateDao(true);      bean.setGenerateProcess(false);      rs = dmd.getPrimaryKeys(dbName, null, tableName);      HashMap pkMap = new HashMap();      while(rs.next()) {        pkMap.put(util.sql2javaName(rs.getString(4)), "");      }      close(rs);      rs = dmd.getColumns(dbName, null, tableName, "%");      Attribute attr = new Attribute();      PrimaryKey pk = new PrimaryKey();      while(rs.next()) {        String sqlColName = rs.getString(4);        String javaColName = util.sql2javaName(sqlColName);        int colType = rs.getInt(5);        Column col = new Column();        Validation val = new Validation();        col.setValidation(val);        col.setName(javaColName);        if(!util.java2sqlName(javaColName).equals(sqlColName)) {          col.setSqlName(sqlColName);        }        col.setTitle(javaColName);        col.setType(convert(colType));        if(!pkMap.containsKey(javaColName)) {          val.setRequired(false);          attr.addColumn(col);        } else {          pk.addColumn(col);          val.setRequired(true);        }        if(col.getType().equals("string")) {          try {            val.setMaxLength(rs.getInt(7));          } catch(RuntimeException e) {            val.setMaxLength(30);          }        }      }      bean.setAttribute(attr);      bean.setPrimaryKey(pk);    } catch(SQLException e) {    } finally {      close(rs);    }    return bean;  }  /**   * Close ResultSet.   * @param rs   */  protected void close(ResultSet rs) {    if(rs != null) {      try {        rs.close();      } catch(SQLException e) {      }      rs = null;    }  }  /**   * Close Connection.   * @param conn   */  protected void close(Connection conn) {    if(conn != null) {      try {        conn.close();      } catch(SQLException e) {        e.printStackTrace();      }      conn = null;    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -