Java Tools class – Automatically generate database tables JavaBean

Source: Internet
Author: User
Tags stmt

There's a lot of work done with the database tables recently, because it's a live interface.

In this process found to convert the table to the corresponding JavaBean type, the field of the table is small, if you accidentally hit dozens of fields of the fucking writing up a bit of trouble, in case you encounter hundreds of, it is not to collapse.

So want to write a tool class, automatically generate JavaBean.

First, the idea:

1. Read the database table and get the field name inside.

Prepare the drive package to connect to the database, just for the Oracle database and MySQL database

2. Build a StringBuffer string to generate the classes we need.

3. Writing files

Requires the appropriate file stream knowledge.

Well, that's all for the job, and here's the code,

Package com.tool;
/*
* Give the database jar package, database link path, database table space name, database name, database password, table name
* You can extract the JavaBean file that creates the table properties and provide a standard Get,set method.
* This program extracts all fields and data to be defined as string types, and if there is any other need to extract the type of fields in the table and other table information, automatically generate
* Java file
* \ t denotes a space
* \ r indicates that line break is equivalent to \ n
* ResultSetMetaData Key
* */
Import Java.io.BufferedWriter;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.SQLException;
Import java.sql.Statement;

public class Sqltobean {
PRIVATE Connection conn = null; Save Link Path
Private Statement stmt = null; Establish a connection
private ResultSetMetaData meta = null; Save Table Property Information
Private ResultSet rs = null; Query result set
Private OutputStreamWriter OSW = null;
Private BufferedWriter bw = NULL;
Private FileOutputStream FOS = null;
private static StringBuffer coding = new StringBuffer (); string buffers
Private String driver = null; Database package Name
Private String URL = null; Path name
Private String table = null; Table Space Name
Private String password = null; Password
Private String tableName = null; Table name

Public Sqltobean (string driver, string url, string table, string password, string tableName) {
This.driver = driver;
This.url = URL;
this.table = table;
This.password = password;
This.tablename = TableName;
}

Private String getcoding (StringBuffer code) {
return code.tostring ();
}

Private StringBuffer Creategenerate (String property) {
String prop = Property.tolowercase ();
Coding.append ("\ r \ t \ Private String" + prop + ";");
return coding;
}

Private StringBuffer Createmethod (string[] str) {
for (int i=0;i<str.length;i++) {
Str[i].charat (0)-32) turn into uppercase ideas
Str[i] = Str[i].tolowercase ();
Coding.append ("\ r \ n \ public void Set" + (char) (Str[i].charat (0)-+) + str[i].substring (1) + "(String" + str[i] + ") {");
Coding.append ("\ r \t\t this.") +str[i] + "=" + Str[i] + ";");
Coding.append ("\ r \ t}");
Coding.append ("\ r \ t public String get" + (char) (Str[i].charat (0)-+) + str[i].substring (1) + "() {");
Coding.append ("\ r \t\t return this.") +str[i] + ";");
Coding.append ("\ r \}\n");
}
return coding;
}
/*
* Close all links to the database
* */
private void Destroy () {
try {
IF (conn! = null) {
Conn.close ();
conn = null;
}
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
if (rs! = null) {
Rs.close ();
rs = null;
}

if (BW! = NULL) {
Bw.close ();
BW = NULL;
}
if (fos! = null) {
Fos.close ();
FOS = null;
}
if (OSW! = null) {
Osw.close ();
OSW = null;
}

} catch (SQLException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
/*
* Link is closed when database connection is abnormal
* */
private void Connect () {
try {
Class.forName (driver);
conn = Drivermanager.getconnection (Url,table,password);
stmt = Conn.createstatement ();

rs = Stmt.executequery ("SELECT * from" + tableName); Query to determine the result set is that table
Meta = Rs.getmetadata (); Methods to invoke the record table information for the result set
} catch (ClassNotFoundException e) {
E.printstacktrace ();
try {
IF (conn! = null) {
Conn.close ();
conn = null;
}
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
if (rs! = null) {
Rs.close ();
rs = null;
}
} catch (SQLException E1) {
E.printstacktrace ();
}
} catch (SQLException e) {
E.printstacktrace ();
try {
IF (conn! = null) {
Conn.close ();
conn = null;
}
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
if (rs! = null) {
Rs.close ();
rs = null;
}
} catch (SQLException E1) {
E.printstacktrace ();
}
}

}

Private string[] Getcolumenname () {
/* Get all column names of the table returned as an array of strings
* */
int count;
string[] str = NULL;
try {
Count = Meta.getcolumncount ();
string[] Strcolumenname = new String[count];
for (int i = 1;i <= count; i++) {
Strcolumenname[i-1] = Meta.getcolumnname (i);
}
str = strcolumenname;
} catch (SQLException e) {
E.printstacktrace ();
}
return str;
}
/**
* Written in the specified file
* @param message
*/
private void WriteData (String message,string className) {
String file = "c:\\" +classname+ ". Java";
try {
FOS = new FileOutputStream (file);
OSW = new OutputStreamWriter (FOS);
BW = new BufferedWriter (OSW);
Bw.write (message);
Bw.flush ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}

}

Public StringBuffer createclassname (String className) {
Coding.append ("public class" + ClassName + "{\ n");
return coding;
}

public static void Main (string[] args) {
String className = "Hellow";
Sqltobean Sqltobean = new Sqltobean ("Oracle.jdbc.driver.OracleDriver", "JDBC:ORACLE:THIN:@192.168.3.11:1521:ORCL" , "Mamibon", "Mamibon", "my_standard_data2");
Sqltobean Sqltobean = new Sqltobean ("Org.gjt.mm.mysql.Driver", "jdbc:mysql://117.79.84.144:3306/wordpress", "Wangjun "," wangjun123 "," wp_users ");
Connecting to a database
Sqltobean.connect ();
Sqltobean.createclassname (ClassName);
Get the fields for a table
String[] STR;
str = Sqltobean.getcolumenname ();
for (int i = 0;i<str.length; i++) {
Sqltobean.creategenerate (Str[i]);
}
Coding.append ("\ n");
Sqltobean.createmethod (str);
Coding.append ("\ n}");
Write file
Sqltobean.writedata (sqltobean.getcoding (coding), className);
Sqltobean.destroy ();

System.out.println ("If you think this tool class is good, please follow our website: http://www.itbuluoge.com, look forward to your stay, the programmer's Club, to provide you with more help!");
System.out.println ("If you think this tool class is good, please follow our website: http://www.itbuluoge.com, look forward to your stay, the programmer's Club, to provide you with more help!");
}

}

Java Tools class – Automatically generate database tables JavaBean

Related Article

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.