Java code Generator (only completes the entity class generation and Formbean class to the entity class conversion code, because I just want this part of the code)
1. Main function, the entity class is generated by database;
2. Editing environment: Jdk1.5,jbuilder2006,sqlserver (MySQL temporarily did not do)
The main interface is shown below:
The first step: fill in the server, username, password, database type, and then click "Connection Database", the database connection;
The second step: After the database connection, select the database, retrieve the table from the library;
The third step: Select the table, you can preview the code to be generated, if you want to add code to the package, the "Custom namespace" check box, and then fill in the name of the package you want to define, click Preview;
The fourth step: click "Generate Button", will map your selected table into an entity class; If you click "Generate All", all the tables in your selected library will be mapped into entity classes;
Step Fifth: (This is what I wrote for my struts) if necessary, you can generate conversion code actionform to the entity class, provided that the code for the property in Actionform is generated by the code generator.
The core of the code is the mapping of database fields and the generation of files, and the mapping of the database mainly uses ResultSetMetaData classes.
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 ("No table selected");
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;
}
}
Note: Because of the urgent need to write, so there is a lot of duplication in the code, IO anomaly is much, my code just do a popular deal.
There is a need to complete the source code can go to:http://kubbye.ys168.com--> tools-->coder.rar download