日期:2014-05-16 浏览次数:20571 次
//生产类
?
package com.yangtb.creatclass;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ParameterMetaData;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
/**
?* 类:创建实体
?* 通过表名,创建一个实体
?* @author Yang 杨天兵
?*时间:20090409
?*/
public class CreatClass extends Sql2000 {
?
?//包名
?String strpackage ="";
?//?数据库表名
?String tableName="";
?
?StringBuffer sb = new StringBuffer();
?Connection con = null;
?
?ResultSet res = null;
?ResultSetMetaData rsmd =null;
?
?public String getTableName() {
??return tableName;
?}
?public void setTableName(String tableName) {
??this.tableName = tableName;
?}
?
?public String getStrpackage() {
??return strpackage;
?}
?public void setStrpackage(String strpackage) {
??this.strpackage = strpackage;
?}
?public void execute() throws?? IOException{
??con = getCoonnection();
??String strsql="select * from "+tableName;
??Statement st;
??try {
???
???st = con.createStatement();
???
??? res = st.executeQuery(strsql);
???
??? rsmd = res.getMetaData();
???String newstrpack = "";
???newstrpack = strpackage.replace(".", "/");
???System.out.println(newstrpack);
???//类名
???String className = tableName.substring(0, 1).toUpperCase()+tableName.substring(1);
???//创建这个类文件strpackage
???File file = new File("src"+"/"+newstrpack+"/"+className+".java");
???sb.append("package "+strpackage+";/n");
???sb.append("");
???sb.append("/*/n *");
???sb.append("这是一个类:/n *@author yangtianb/n *//n");
???sb.append("public class "+className+" {");
???
???sb.append("/n");
???for(int i=1;i<rsmd.getColumnCount();i++){
????String type = typename(rsmd.getColumnTypeName(i));
????//打印属性
????sb.append("/tprivate/t");
????sb.append(type+"/t");
????sb.append(rsmd.getColumnName(i)+";");
????sb.append("/n");
????//打印get方法
????sb.append("/t//get方法/n");
????sb.append("/tpublic/t"+type+"/tget"+rsmd.getColumnName(i)+"(){/n");
????sb.append("/t/treturn "+rsmd.getColumnName(i)+";/n/t/n");
????sb.append("}/n");
????//打印set方法
????sb.append("/t//set方法/n");
????//
????sb.append("/tpublic/t"+type+"/tset"+rsmd.getColumnName(i)+"("+type+"/t"+rsmd.getColumnName(i)+")"+"{/n");
????sb.append("/treturn/tthis."+rsmd.getColumnName(i)+"/t = "+rsmd.getColumnName(i)+";/n");
????sb.append("}/n");
???}
???sb.append("}");
???String strsb = sb.toString();
??? //创建一个FileWriter对象
???FileWriter fw = new FileWriter(file);
???//创建一个BufferedWriter对象
???BufferedWriter bw = new BufferedWriter(fw);
???bw.write(strsb);
???bw.newLine();
??//关闭文件流
???bw.flush();
???fw.close();
?
??} catch (SQLException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}finally{
???//关闭数据库连接
???closeResultSet(res);
???closeConneciton(con);
??}
??
??System.out.println(sb);
?}
?
?public String typename(String typename){
??
??String tystr=null;
??if(typename.equals("varchar")){
???tystr="String";
??}
??if(typename.equals("int")){
???tystr="int";
??}
??else{
???tystr="Object";
??}
??return tystr;
?}
?
?
}
?
在上面工程中的类,以后在其他工程中导入这个类,在写入xml传入参数就能得到相应数据库表名的实体
?
//build.xml
?
<?xml version="1.0"?>
<project name="project" default="default">
?
??? <target name="default" depends="depends" description="--> description">
??????? <taskdef name="test" classname="com.yangtb.creatclass.CreatClass"></taskdef>
??? ?<test tableName="Bloginfo" strpacka