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;
}
}