The role of this code, connected to the MySQL database, and then a MySQL table reflected as a JavaBean object, save part of the time.
Insufficient, has not been able to import the package. For example, the date class also needs to be imported manually.
Package Com.coffee.common;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileWriter;
Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.SQLException;
Import Java.util.Vector;
public class Reflectbean {private Connection Connection;
Private PreparedStatement userquery; /*mysql URL's connection string */private static string url = "jdbc:mysql://127.0.0.1:3306/test1?useunicode=true&
Characterencoding=utf-8&useoldaliasmetadatabehavior=true ";
Account private static String user = "root";
Password private static String password = "123";
Private vector<string> Vector = new vector<string> ();
MySQL JDBC Java package driver string private string driverclassname = "Com.mysql.jdbc.Driver";
The table name in the database String table = "E_task_result"; The column name of the database private string[] colnames;
Column array group//column name Type array private string[] coltypes; Public reflectBean () {try {//Drive registration Class.forName (DRIVERCLASSNAME); if (connection = null | | connection.isclosed ())//Get link connection = drivermanager.getconnection (URL, user, passwo
RD);
catch (ClassNotFoundException ex) {ex.printstacktrace ();
System.out.println ("Oh,not");
catch (SQLException e) {e.printstacktrace ();
System.out.println ("Oh,not");
} public Connection getconnection () {return Connection;
public void SetConnection (Connection Connection) {this.connection = Connection;
public void Doaction () {String sql = ' select * from ' +table;
try {preparedstatement statement = connection.preparestatement (SQL);
Gets the metadata of the database ResultSetMetaData metadata = Statement.getmetadata ();
Number of fields in the database int len = Metadata.getcolumncount ();
Field name colnames = new String[len+1];
Field type---> has been converted to the class name in Java Coltypes = new string[len+1]; for (int i= 1;i<=len;i++) {//system.out.println (Metadata.getcolumnname (i) + ":" +metAdata.getcolumntypename (i) + ":" +sqltype2javatype (Metadata.getcolumntypename (i). toLowerCase ()) + ":" +
Metadata.getcolumndisplaysize (i));
Metadata.getcolumndisplaysize (i); Colnames[i] = Metadata.getcolumnname (i); Gets the field name Coltypes[i] = Sqltype2javatype (Metadata.getcolumntypename (i));
Gets the field type} catch (SQLException e) {e.printstacktrace (); }/* * MySQL's field type translates to Java type * * private string Sqltype2javatype (string sqltype) {if sqltype.equal
Signorecase ("bit") {return "boolean");
}else if (sqltype.equalsignorecase ("tinyint")) {return "byte";
}else if (sqltype.equalsignorecase ("smallint")) {return ' short ';
}else if (sqltype.equalsignorecase ("int")) {return "int";
}else if (sqltype.equalsignorecase ("bigint")) {return "long";
}else if (sqltype.equalsignorecase ("float")) {return "float"; }else if (sqltype.equalsignOrecase ("decimal") | | Sqltype.equalsignorecase ("numeric") | | Sqltype.equalsignorecase ("real") | | Sqltype.equalsignorecase ("Money") | |
Sqltype.equalsignorecase ("SmallMoney")) {return "double"; }else if (sqltype.equalsignorecase ("varchar") | | | sqltype.equalsignorecase ("char") | | sqltype.equalsignor Ecase ("nvarchar") | | Sqltype.equalsignorecase ("nchar") | |
Sqltype.equalsignorecase ("text")) {return "String"; }else if (sqltype.equalsignorecase ("datetime") | |
Sqltype.equalsignorecase ("date")) {return "date";
}else if (sqltype.equalsignorecase ("image")) {return "Blod";
return null; /* * Gets the string of the entire class and outputs it to the Java file * */Public StringBuffer getclassstr () {//Output class string StringBuffer str = new StringBuffer (""
);
Gets the field name of the table type and table name This.doaction (); Checksum if (null = = Colnames && NULL = = Coltypes) retuRN null;
Stitching Str.append ("public class" +table+ "{\ r \ n"); Stitching properties for (int index=1; index < colnames.length; index++) {Str.append getattrbutestring (colnames[index],coltypes[
Index])); ///Stitching Get,set method for (int index=1; index < colnames.length; index++) {Str.append (getgetmethodstring
Dex],coltypes[index]));
Str.append (getsetmethodstring (Colnames[index],coltypes[index));
} str.append ("}\r\n");
The output to the file is filename = new file (table+ ". Java");
BufferedWriter write = null;
try {write = new BufferedWriter (new FileWriter (file));
Write.write (Str.tostring ());
Write.close ();
catch (IOException e) {e.printstacktrace ();
if (write!= null) try {write.close ();
catch (IOException E1) {e1.printstacktrace ();
} return str; /* * Get field String */Public StringBuffer getattrbutestring (string name, String type) {if (!check (Name,type)) {System
. OUT.PRINTLN ("a property or type is NULL in the class");
return null;
}; String format = String.Format ("Private%s%s;\n\r", New String[]{type,name});
return new StringBuffer (format); /* * Verify that name and type are legitimate/public boolean check (string name, String type) {if (""). Equals (name) | | Name = = NULL | |
Name.trim (). Length () ==0) {return false; if ("". Equals (type) | | Type = = NULL | |
Type.trim (). Length () ==0) {return false;
return true; * * * Gets the Get method string */Private StringBuffer getgetmethodstring (string name, String type) {if (!check (Name,type)) {S
Ystem.out.println ("A property or type is NULL in the class");
return null;
};
String methodname = "Get" +gettuofeng (name);
String format = String.Format ("Public%s%s () {\n\r", new Object[]{type,methodname});
Format + = String.Format ("Return this.%s;\r\n", New Object[]{name});
Format + + "}\r\n";
return new StringBuffer (format);
///capitalize the first character of the name to private string Gettuofeng (string name) {name = Name.trim (); if (Name.length () > 1) {name = name.substring (0, 1). toUpperCase () +name.subString (1);
}else {name = Name.touppercase ();
} return name; * * * Gets the field's Get method String */Private Object getsetmethodstring (string name, String type) {if (!check (Name,type)) {Syst
Em.out.println ("A property or type is NULL in the class");
return null;
};
String methodname = "Set" +gettuofeng (name);
String format = String.Format ("public void%s (%s) {\n\r", new Object[]{methodname,type,name});
Format + = String.Format ("this.%s =%s;\r\n", New Object[]{name,name});
Format + + "}\r\n";
return new StringBuffer (format);
public static void Main (string[] args) {Reflectbean bean = new Reflectbean ();
System.err.println (Bean.getclassstr ());
}
}