java的代碼產生器(只完成了實體類的產生和formbean類向實體類的轉碼,因為我只要這部分代碼)
1.主要功能,由資料庫產生實體類;
2.編輯環境:jdk1.5,jbuilder2006,sqlserver 2000(mysql的暫時沒做)
主介面如下圖:
第一步:填寫伺服器,使用者名稱,密碼,資料庫類型,然後點擊"串連資料庫",進行資料庫連接;
第二步:資料庫連接上以後,選擇資料庫,從庫中檢索出表來;
第三步:選擇表,可以預覽將要產生的程式碼,如果要將代碼加入包中,將"自訂命名空間"複選框選中,同時將自己要定義的包名填寫,點擊預覽;
第四步:點擊"產生按鈕",將把你選中的表映射成個實體類;如果點擊"產生所有",將把你選中的庫中的所有表都映射成實體類;
第五步:(這個是我專為我的struts寫的)如果有需要,可以產生ActionForm到實體類的轉碼,前提是actionForm中屬性的代碼都是由代碼產生器產生的.
代碼的核心是資料庫欄位的映射和檔案的產生,資料庫的映射主要用ResultSetMetaData類.
package com.xaccp.code;
import java.sql.*;
import java.io.*;
public class FileService {
private CodeImplement code;
public FileService(CodeImplement c)
{
this.code=c;
}
public String service()
{
String driver=null ;
String url=null;
String user=null;
String pwd=null;
if(code.optMssql.isSelected())
{
try {
driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
url = "jdbc:microsoft:sqlserver://" + code.server.getText() +
":1433;databaseName=" +
code.database.getSelectedItem().toString();
user = code.user.getText();
pwd = code.password.getText();
} catch (Exception ex) {
return ex.getMessage();
}
}
if(code.optMysql.isSelected())
{}
DataBaseService dbs=new DataBaseService(driver,url,user,pwd);
ResultSetMetaData rm=dbs.getMeta(code.table.getSelectedItem().toString());
String filePre="";
if(code.pack.isSelected() && code.namespace.getText()!=null && code.namespace.getText()!="" )
{
filePre +="package "+code.namespace.getText()+";";
filePre +="/n/r";
}
filePre +="import java.sql.*;/n";
filePre +="import java.math.*;";
filePre +="/n"+"public class "+this.toUpper(code.table.getSelectedItem().toString());
filePre +="/n"+"{";
try {
for (int i = 1; i <= rm.getColumnCount(); i++) {
String cName=this.formatColumnName(rm.getColumnName(i));
String cType=getType(rm.getColumnClassName(i));
filePre +=Type.oneTable+"private "+cType +" "+cName+";";
filePre +=Type.oneTable+"public "+cType +" get"+this.toUpper(cName)+"(){";
filePre +=Type.twoTable+"return "+cName+";";
filePre +=Type.oneTable+"}";
filePre +=Type.oneTable+"public void "+" set"+this.toUpper(cName)+"("+cType+" "+cName+"){";
filePre +=Type.twoTable+"this."+cName+"="+cName+";";
filePre +=Type.oneTable+"}";
// System.out.println(ss+"|||||||"+type);
}
} catch (SQLException ex) {
return ex.getMessage();
}
filePre +="/n"+"}";
return filePre;
}
public String serviceForm()
{
String driver=null ;
String url = null;
String user = null;
String pwd = null;
if (code.optMssql.isSelected()) {
try {
driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
url = "jdbc:microsoft:sqlserver://" + code.server.getText() +
":1433;databaseName=" +
code.database.getSelectedItem().toString();
user = code.user.getText();
pwd = code.password.getText();
} catch (Exception ex) {
return ex.getMessage();
}
}
if (code.optMysql.isSelected()) {}
DataBaseService dbs = new DataBaseService(driver, url, user, pwd);
ResultSetMetaData rm = dbs.getMeta(code.table.getSelectedItem().toString());
String filePre = "";
String className=this.toUpper(code.table.getSelectedItem().toString());
filePre +=Type.oneTable+"public "+className+" get"+className+"(){";
filePre +=Type.twoTable+className +" "+Type.className+"=new "+className+"();";
try {
for (int i = 1; i <= rm.getColumnCount(); i++) {
String cName = this.formatColumnName(rm.getColumnName(i));
String cType = getType(rm.getColumnClassName(i));
filePre +=Type.twoTable+Type.className+".set"+this.toUpper(cName)+"("+cName+");";
}
} catch (SQLException ex) {
return ex.getMessage();
}
filePre +=Type.twoTable+"return temp;";
filePre += Type.oneTable + "}";
return filePre;
}
public boolean saveFormToFile(String pathMain)
{
return false;
}
public boolean saveToFile(String pathMain)
{
String path = null;
try {
path = pathMain + "/" +
this.toUpper(code.table.getSelectedItem().toString()) +
".java";
} catch (Exception ex) {
System.out.println("沒有選擇表");
return false;
}
try {
FileOutputStream fos=new FileOutputStream(new File(path));
OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
osw.write(this.service());
osw.flush();
fos.flush();
osw.close();
fos.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
return false;
}
return true;
}
public boolean saveAll(String path)
{
for(int i=0;i<code.table.getItemCount();i++)
{
code.table.setSelectedIndex(i);
this.saveToFile(path);
}
return true;
}
private String getType(String type)
{
if(type.equals("java.lang.Double") || type.equals("java.lang.Float"))
{
type= "double";
}
else if(type.equals("java.lang.Integer"))
{
type= "int";
}
else if(type.equals("java.lang.Boolean"))
{
type= "boolean";
}
else if(type.equals("java.sql.Timestamp"))
{
type= "java.sql.Date";
}
return type;
}
private String toUpper(String str)
{
String substr=str.substring(0,1);
String laststr=str.substring(1);
return substr.toUpperCase()+laststr;
}
private String formatColumnName(String name)
{
name=name.replaceAll("-","");
return name;
}
}
注:因為急需用才寫的,所以代碼中有重複的地方很多,io異常本來就多,My Code中只是做了福士化的處理.
有需要完整源碼的可以去:http://kubbye.ys168.com-->工具-->coder.rar下載