按照一定的格式生成Bean

package tool.generate;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Locale;

public class GenerateBean {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Connection conn = null;

  try {
   String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:DBName";
   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
   conn = DriverManager.getConnection(dbUrl, "user", "password");

   String tableName = "";

   FileWriter beanFile = null;
   FileWriter propFile = null;

   for (int i = 0; i < args.length; i++) {
    tableName = args[i];

    beanFile = new FileWriter("C://GenerateFiles//"
      + GetClassName(tableName) + ".java");
    propFile = new FileWriter("C://GenerateFiles//"
      + GetClassName(tableName) + ".properties");

    beanFile.write("package com.industrycommerce.bean.m12315;/r/n");
    beanFile.write("import java.io.Serializable;/r/n");
    beanFile.write("public class "+GetClassName(tableName)+" implements Serializable{/r/n");
    beanFile.write(" private static final long serialVersionUID = 1L;/r/n");
     
    PreparedStatement pment = conn
      .prepareStatement("select * from " + tableName
        + " where 1 = 2");
    ResultSet rs = pment.executeQuery();
    ResultSetMetaData metaData = rs.getMetaData();
    int colum = metaData.getColumnCount();
    for (int j = 1; j <= colum; j++) {
     String typeStr = ""; //类型    
     //获取列名    
     String columName = metaData.getColumnName(j).toLowerCase();
     //获取每一列的数据类型    
     int type = metaData.getColumnType(j);
      

     //判断    
     if (Types.INTEGER == type || Types.NUMERIC == type) {
      typeStr = "Integer";
     } else if (Types.VARCHAR == type || Types.CHAR == type
       || Types.DATE == type) {
      typeStr = "String";
     } else if (Types.DECIMAL == type || Types.DOUBLE == type || Types.FLOAT == type) {
      typeStr = "Double";
     } else if (Types.BIGINT == type) {
      typeStr = "Long";
     }

     propFile.write("m_" + columName + "=" + columName + "/r/n");
     beanFile.write(" "+typeStr + " m_"+columName+";/r/n");
    }
    
    beanFile.write("/r/n");
    beanFile.write("/r/n");
    for (int m = 1; m <= colum; m++) {
     beanFile.write("/r/n");
     
     String typeStr = ""; //类型    
     //获取列名    
     String columName = metaData.getColumnName(m).toLowerCase();
     //获取每一列的数据类型    
     int type = metaData.getColumnType(m);
     //判断    
     if (Types.INTEGER == type || Types.NUMERIC == type) {
      typeStr = "Integer";
     } else if (Types.VARCHAR == type || Types.CHAR == type
       || Types.DATE == type) {
      typeStr = "String";
     } else if (Types.DECIMAL == type || Types.DOUBLE == type || Types.FLOAT == type) {
      typeStr = "Double";
     }

     String defaultValue = " /"/"";
     if(typeStr == "Integer")
     {
      defaultValue="0";
     } else if(typeStr == "Double")
     {
      defaultValue= "0.0";
     }
     beanFile.write(" public "+typeStr+" getM_"+columName+"() {/r/n");
     beanFile.write("  return m_"+ columName+"==null ? "+defaultValue+" : m_"+ columName+";/r/n");
     beanFile.write(" }/r/n");
     beanFile
       .write(" public void setM_"+columName+"("+typeStr+" m_"+ columName+") {/r/n");
     beanFile.write("  this.m_"+ columName+" = m_"+ columName+";/r/n");
     beanFile.write(" }/r/n");
    }
    
    beanFile.write("}/r/n");
    
    beanFile.close();
    propFile.close();
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

 
 private static String GetClassName(String tableName)
 {
  String className ="";
  String[] elems = tableName.split("_");
  for(int i=0;i<elems.length;i++)
  {
   className += elems[i].substring(0, 1).toUpperCase() + elems[i].substring(1);
  }
  return className;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值