Automatically generate Pojo (JavaBean) objects from database tables

Source: Internet
Author: User

Main class:


Package bqw.tool;


Import Java.util.ResourceBundle;
Import Java.sql.DriverManager;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.Statement;
Import Java.sql.Types;
Import Java.io.File;
Import Java.io.FileWriter;
Import Java.sql.DatabaseMetaData;
Import java.util.ArrayList;

public class Db2javabean {

Private String Driver = ""; Driven
Private String URL = ""; Database access string
Private String UserName = ""; Database user Name
Private String Password = ""; Database Password
Private String TableName = ""; To generate a table name for a Jopo object, use; split
Private String Tablematchpattern = ""; database table name matching pattern
Private String Matchpattern = ""; Whether the database table name matching mode feature is enabled, the TableName property is not used when enabled


Public Db2javabean () {}

Public Db2javabean (Boolean init) {
if (init) {
ResourceBundle RB = Resourcebundle.getbundle ("Bqw.tool.DB2JavaBean");
This.driver = rb.getstring ("Driver");
This.url = rb.getstring ("url");
This.username = rb.getstring ("UserName");
This.password = rb.getstring ("password");
This.tablename = rb.getstring ("TableName");
This.matchpattern = rb.getstring ("Matchpattern");
This.tablematchpattern = rb.getstring ("Tablematchpattern");
}

}

Public Db2javabean (String baseName) {
ResourceBundle RB = Resourcebundle.getbundle (BaseName);
This.driver = rb.getstring ("Driver");
This.url = rb.getstring ("url");
This.username = rb.getstring ("UserName");
This.password = rb.getstring ("password");
This.tablename = rb.getstring ("TableName");
This.tablematchpattern = rb.getstring ("Tablematchpattern");
This.matchpattern = rb.getstring ("Matchpattern");

}

Public Db2javabean (String driver,string url,string username,string password,string tablename,string TableMatchPattern , String Matchpattern) {
This.driver = driver;
This.password = password;
This.username = UserName;
This.url = URL;
This.tablename = TableName;
This.tablematchpattern = Tablematchpattern;
This.matchpattern = Matchpattern;
}

public void Setdriver (String driver) {
This.driver = driver;
}

public void SetUrl (String URL) {
This.url = URL;
}

public void Setusername (String userName) {
This.username = UserName;
}

public void SetPassword (String password) {
This.password = password;
}

public void Settablename (String tableName) {
This.tablename = TableName;
}

public void Settablematchpattern (String tablematchpattern) {
This.tablematchpattern=tablematchpattern;
}

public void Setmatchpattern (String matchpattern) {
This.matchpattern = Matchpattern;
}

Public String Getdriver () {
return this.driver;
}

Public String GetUrl () {
return this.url;
}

Public String GetUserName () {
return this.username;
}

Public String GetPassword () {
return This.password;
}

Public String Gettablename () {
return this.tablename;
}

Public String Gettablematchpattern () {
return this.tablematchpattern;
}

Public String Getmatchpattern () {
return this.matchpattern;
}
public void init (int objecttypeorcommonlytype) {
try{
Class.forName (This.driver). newinstance ();
Connection conn = drivermanager.getconnection (This.url,this.username, This.password);
String [] tables = new String[0];
ArrayList tableal = new ArrayList (20);
if ("true". Equals (This.matchpattern)) {
DatabaseMetaData Dbmd = Conn.getmetadata ();
ResultSet Dbmdrs = Dbmd.gettables (null, This.userName.toUpperCase (), This.tablematchpattern, new string[]{"TABLE"});
while (Dbmdrs.next ()) {
Tableal.add (Dbmdrs.getstring (3));
}
Dbmdrs.close ();
if (Tableal.size () ==0) {
Dbmdrs = Dbmd.gettables (null, This.userName.toLowerCase (), This.tablematchpattern, new string[]{"TABLE"});
while (Dbmdrs.next ()) {
Tableal.add (Dbmdrs.getstring (3));
}
Dbmdrs.close ();
}
if (Tableal.size () ==0) {
Dbmdrs = Dbmd.gettables (null, This.username, This.tablematchpattern, new string[]{"TABLE"});
while (Dbmdrs.next ()) {
Tableal.add (Dbmdrs.getstring (3));
}
Dbmdrs.close ();
}
tables = new String[tableal.size ()];
for (int ti = 0;ti
Tables[ti] = Tableal.get (TI);
}
}
else{
tables = This.tableName.split (";");
}
String strtype;
String StrName;
String ClassName;
String [] namesect;
StringBuilder TBN = new StringBuilder ();
StringBuilder tstr1 = new StringBuilder ();
StringBuilder tstr2 = new StringBuilder ();
File File = new file ("JavaBean");
if (!file.exists ()) File.mkdir ();
if (!file.isdirectory ()) File.mkdir ();
for (int i=0;i
Namesect = Tables[i].split ("_");
for (String Ns:namesect) {
Tbn.append (ns.substring (0,1). toUpperCase () +ns.substring (1). toLowerCase ());
}

ClassName = Tbn.tostring ();
Tbn.delete (0,tbn.length ());
Tstr1.append ("Import java.sql.*;");
Tstr1.append ("\ n");
Tstr1.append ("Import javax.sql.*;");
Tstr1.append ("\ n");
Tstr1.append ("Import java.io.*;");
Tstr1.append ("\ n");
Tstr1.append ("public class" +classname+ "Implements serializable{");
Tstr1.append ("\ n");
try{
System.out.println (Tables[i]);
Statement Statement = Conn.createstatement ();
ResultSet rs = statement.executequery ("SELECT * from" +tables[i]);
ResultSetMetaData RSD = Rs.getmetadata ();
int cc = Rsd.getcolumncount ();
for (int j=1;j<=cc;j++) {
if (Objecttypeorcommonlytype = = Staticvar.objecttype) {
Strtype = This.getobjecttype (Rsd.getcolumntype (j));
}
else{
Strtype = This.getcommonlytype (j);
}
if (strtype = = null) continue;
StrName = Rsd.getcolumnname (j);
Tstr1.append ("Private" +strtype+ "" +strname.tolowercase () + ";");
Tstr1.append ("\ n");
Tstr2.append ("public void Set" +strname.substring (0,1). toUpperCase () +strname.substring (1). toLowerCase () + "(" + Strtype+ "" +strname.tolowercase () + ") {");
Tstr2.append ("\ n");
Tstr2.append ("this.") +strname.tolowercase () + "=" +strname.tolowercase () + ";");
Tstr2.append ("\ n");
Tstr2.append ("}");
Tstr2.append ("\ n");
Tstr2.append ("Public" +strtype+ "get" +strname.substring (0,1). toUpperCase () +strname.substring (1). toLowerCase () + "( ){");
Tstr2.append ("\ n");
Tstr2.append ("return this.") +strname.tolowercase () + ";");
Tstr2.append ("\ n");
Tstr2.append ("}");
Tstr2.append ("\ n");


}
Rs.close ();
Statement.close ();

}
catch (Exception Tablee)
{
Tablee.printstacktrace ();
}
Tstr2.append ("}");
Tstr2.append ("\ n");
Tstr1.append (Tstr2.tostring ());
Tstr1.append ("\ n");
File = new file ("javabean/" +classname+ ". Java");
FileWriter FW = new FileWriter (file);
Fw.write (Tstr1.tostring ());
Fw.flush ();
Fw.close ();
Tstr1.delete (0, Tstr1.length ());
Tstr2.delete (0, Tstr2.length ());
}
Conn.close ();
}
catch (Exception drivere) {
Drivere.printstacktrace ();
}

}

Public String getobjecttype (int type) {
Switch (type) {
Case Types.ARRAY:return NULL;
Case Types.BIGINT:return "Long";
Case Types.BINARY:return NULL;
Case Types.BIT:return "Byte";
Case Types.BLOB:return "BLOB";
Case Types.BOOLEAN:return "BOOLEAN";
Case Types.CHAR:return "String";
Case Types.CLOB:return "CLOB";
Case Types.DATALINK:return NULL;
Case Types.DATE:return "DATE";
Case Types.DECIMAL:return "Double";
Case Types.DISTINCT:return NULL;
Case Types.DOUBLE:return "DOUBLE";
Case Types.FLOAT:return "FLOAT";
Case Types.INTEGER:return "INTEGER";
Case Types.NUMERIC:return "Integer";
Case Types.JAVA_OBJECT:return NULL;
Case Types.LONGVARBINARY:return NULL;
Case Types.LONGVARCHAR:return NULL;
Case Types.NULL:return NULL;
Case Types.OTHER:return NULL;
Case Types.REAL:return NULL;
Case Types.REF:return NULL;
Case Types.SMALLINT:return "short";
Case Types.STRUCT:return NULL;
Case Types.TIME:return "Time";
Case Types.TIMESTAMP:return "TIMESTAMP";
Case Types.TINYINT:return "short";
Case Types.VARBINARY:return NULL;
Case Types.VARCHAR:return "String";
Default:return null;
}
}

Public String getcommonlytype (int type) {
Switch (type) {
Case Types.ARRAY:return NULL;
Case Types.BIGINT:return "Long";
Case Types.BINARY:return NULL;
Case Types.BIT:return "byte";
Case Types.BLOB:return "String";
Case Types.BOOLEAN:return "BOOLEAN";
Case Types.CHAR:return "String";
Case Types.CLOB:return "String";
Case Types.DATALINK:return NULL;
Case Types.DATE:return "DATE";
Case Types.DECIMAL:return "Double";
Case Types.DISTINCT:return NULL;
Case Types.DOUBLE:return "DOUBLE";
Case Types.FLOAT:return "FLOAT";
Case Types.INTEGER:return "int";
Case Types.NUMERIC:return "int";
Case Types.JAVA_OBJECT:return NULL;
Case Types.LONGVARBINARY:return NULL;
Case Types.LONGVARCHAR:return NULL;
Case Types.NULL:return NULL;
Case Types.OTHER:return NULL;
Case Types.REAL:return NULL;
Case Types.REF:return NULL;
Case Types.SMALLINT:return "short";
Case Types.STRUCT:return NULL;
Case Types.TIME:return "Time";
Case Types.TIMESTAMP:return "TIMESTAMP";
Case Types.TINYINT:return "short";
Case Types.VARBINARY:return NULL;
Case Types.VARCHAR:return "String";
Default:return null;
}
}

public static void Main (string[] args) {
TODO auto-generated Method Stub
Db2javabean d2j = new Db2javabean (true);
System.out.println (D2j.getdriver ());
System.out.println (D2j.geturl ());
System.out.println (D2j.getusername ());
System.out.println (D2j.getpassword ());
System.out.println (D2j.gettablename ());
System.out.println (D2j.gettablematchpattern ());
System.out.println (D2j.getmatchpattern ());
D2j.init (Staticvar.objecttype);
D2j.init (Staticvar.commonlytype);
System.out.println ("OK");
}

}

Static class:

Package bqw.tool;

public class Staticvar {
public static final int OBJECTTYPE = 0;
public static final int commonlytype = 1;
}

Configuration file:

#驱动
Driver= Org.apache.derby.jdbc.ClientDriver
#数据库访问串
url=jdbc:derby://localhost:1527/myeclipse
#用户名
UserName =classiccars
#密码
Password=classiccars
#需要产生pojo文件的表名, use; number delimited
Tablename=customer;employee;office; Payment
#如果matchPattern为true, Tablematchpattern works, TableName does not work
Matchpattern=true
#表名匹配模式, using the like statement's matching pattern, Automatically selects tables that belong to this user and the table name matches Tablematchpattern
tablematchpattern=%


#驱动
#driver =oracle.jdbc.driver.oracledriver
#数据库访问串
#url =JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL
#用户名
#userName =test
#密码
#password =test
To #需要产生pojo文件的表名, use, or divide a number
#tableName =t_files;t_sys_wordtemplate;t_sys_wordmodel;t_sys_user;t_sys_user_dept;
#如果matchPattern为true, then Tablematchpattern works, TableName doesn't work.
#matchPattern =true
#表名匹配模式, the table that belongs to this user and the table name matches Tablematchpattern is automatically selected using the match pattern of the like statement, which is case-sensitive
#tableMatchPattern =t_%

Transferred from: HTTP://HI.BAIDU.COM/BIANQIWEI/ITEM/E37C4EAA913ACF14A8CFB7DD

Automatically generate Pojo (JavaBean) objects from database tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.