Step 1: connect to the database
Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import javax. swing. JOptionPane; public class DBConnection {private static final String DBDRIVER = "com. mysql. jdbc. driver "; // Driver Class Name private static final String DBURL =" jdbc: mysql: // localhost: 3306/ibatis "; // connect URLprivate static final String DBUSER = "root"; // database username Private static final String DBPASSWORD = "admin"; // The Database Password is static {// place the driver to the static block. try {Class. forName (DBDRIVER); // load driver} catch (ClassNotFoundException e1) {// a load driver exception occurs in JOptionPane. showMessageDialog (null, "failed to load the driver !!! "," ☆★Prompt information ☆★", JOptionPane. INFORMATION_MESSAGE); // prompt driver loading Failed} public static Connection getConnection () {Connection conn = null; // create Connection interface reference try {conn = DriverManager. getConnection (DBURL, DBUSER, DBPASSWORD); // establish a connection} catch (SQLException e) {// connection exception JOptionPane. showMessageDialog (null, "connection to MySQL failed !!! "," ☆★Prompt information ☆★", JOptionPane. INFORMATION_MESSAGE); // a message indicating Connection to MySQL failed} return conn;} public static void close (Connection conn) {if (conn! = Null) {// determine whether the Connection object is null try {conn. close (); // close Connection to database resources} catch (SQLException e) {// determine whether an exception occurs when the Connection object is closed. System. out. println ("an exception occurred when closing the database connection") ;}} public static void close (Statement stmt) {if (stmt! = Null) {// determines whether the Statement object is null. try {stmt. close (); // close the database resource} catch (SQLException e) {// determine whether exceptions occur when the Statement object is closed. showMessageDialog (null, "an exception occurred when shutting down database operation resources !!! "," ☆★Prompt information ☆★", JOptionPane. INFORMATION_MESSAGE); // a message indicating an exception occurred when shutting down database operation resources} public static void close (ResultSet rs) {if (rs! = Null) {// determines whether the result set is null. try {rs. close (); // close the result set} catch (SQLException e) {// determine whether the result set has an exception JOptionPane. showMessageDialog (null, "an exception occurred when disabling the result set !!! "," ☆★Prompt information ☆★", JOptionPane. INFORMATION_MESSAGE); // An error occurred while disabling the result set }}}}
Step 2: Execute the conversion:
Import java. io. file; import java. io. printWriter; import java. SQL. connection; import java. SQL. databaseMetaData; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. resultSetMetaData; import java. SQL. types; import org. apache. commons. dbutils. dbUtils; public class BuildDTO {public void build (String tableName, String packageName, String className) {Connection conn = null; PreparedStatement pment = null; ResultSet rs = null; PrintWriter pw = null; string pack = packageName. replace (". ","/"); // package name System. out. println ("src/" + pack + "/" + className + ". java "); try {pw = new PrintWriter (new File (" src/"+ pack +"/"+ className + ". java "); pw. println ("package" + packageName + "; \ n"); pw. println (""); pw. println ("public class" + className + "\ n {\ t"); // create a connection conn = DBConnection. getConnection (); // build the pre-processor pment = conn. prepareStatement ("select * from" + tableName + "where 1 = 2;"); rs = pment.exe cuteQuery (); ResultSetMetaData metaData = rs. getMetaData (); // get the number of columns in the form int colum = metaData. getColumnCount (); for (int I = 1; I <= colum; I ++) {String pStr = ""; // setXxxxString typeStr = ""; // type // obtain the column name String columName = metaData. getColumnName (I); // obtain the data type of each column. int type = metaData. getColumnType (I); // System. out. println (I + "---" + type); // judge typeStr = type (type ); // assemble the private statement pStr + = "private" + typeStr + "" + columName + ";"; // output the private field pw. println ("\ t" + pStr + "");} String constructStr = ""; // construct // assemble an empty parameter to construct constructStr + = "public" + className + "() \ n \ t {\ n \ t }"; // construct pw with an empty output parameter. println ("\ n \ t" + constructStr + "\ n"); for (int I = 1; I <= colum; I ++) {String getStr = ""; string setStr = ""; String typeStr = ""; // obtain the column name String columName = metaData. getColumnName (I); // obtain the data type of each column. int type = metaData. getColumnType (I); // judge typeStr = type (type); // assemble the set statement setStr + = "public void set" + columName. substring (0, 1 ). toUpperCase () + "" + columName. substring (1) + "(" + typeStr + "" + columName + ") \ n \ t {\ n"; setStr + = "\ t \ tthis. "+ columName +" = "+ columName +"; \ n \ t} "; // assemble the get statement getStr + =" public "+ typeStr +" get "+ columName. substring (0, 1 ). toUpperCase () + "" + columName. substring (1) + "() \ n \ t {\ n \ t"; getStr + = "\ treturn this. "+ columName +"; \ n \ t} "; // output setpw. println ("\ t" + setStr); // output getpw. println ("\ t" + getStr);} pw. println ("}"); // buffer pw. flush (); pw. close ();} catch (Exception e) {e. printStackTrace ();} finally {// close the connection to DbUtils. closeQuietly (conn, pment, rs) ;}} public String type (int type) {String typeStr = null; if (Types. INTEGER = type) {typeStr = "Integer";} else if (Types. VARCHAR = type) {typeStr = "String";} else if (Types. CHAR = type) {typeStr = "CHAR";} else if (Types. TIMESTAMP = type) {typeStr = "Date";} else if (Types. INTEGER = type) {typeStr = "Integer";} else if (Types. LONGVARCHAR = type) {typeStr = "String";} return typeStr;} // obtain all the tables in the database, public void getTableNameByCon (Connection con) {try {DatabaseMetaData meta = con. getMetaData (); ResultSet rs = meta. getTables (null, new String [] {"TABLE"}); while (rs. next () {System. out. println ("table name:" + rs. getString (3); System. out. println ("Table Username:" + rs. getString (2); System. out. println ("------------------------------");} con. close ();} catch (Exception e) {try {con. close ();} catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace ();} // TODO Auto-generated catch blocke. printStackTrace () ;}} public static void main (String [] args) {BuildDTO dto = new BuildDTO (); dto. build ("User", "com. mzsx. sql2bean "," User "); dto. getTableNameByCon (DBConnection. getConnection ());}}
This article from the "Dream think Xi" blog, please be sure to keep this source http://qiangmzsx.blog.51cto.com/2052549/1305085