Because the work is developed using eclipse and the hibernate plug-in is not installed, the automatic generation of entity classes cannot be used. To facilitate searching for Automatic Generation of entity classes written by someone else on the Internet, with a slight modification, you can directly run the command to generate the object class of the corresponding table.
Note that you need to change the database address, user name, and password in the method, and enter the description of the object class you want to generate. Private string tablename = "ad_campaign ";
The method is as follows:
Package COM. test; import Java. io. filewriter; import Java. io. ioexception; import Java. io. printwriter; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. preparedstatement; import Java. SQL. resultsetmetadata; import Java. SQL. sqlexception; public class createbean {private string tablename = "ad_campaign"; private string [] colnames; // array of column names private string [] coltypes; // array of column name types private int [] colsizes; // array of column name size private Boolean f_util = false; // whether to import the package Java. util. * Private Boolean f_ SQL = false; // whether to import the Java package. SQL. * Private string url = "JDBC: mysql: // localhost: 3306/haierdb"; private string user = "permitdbuser"; private string Pwd = "permitdbpwd"; public connection getconnection () {connection conn = NULL; try {class. forname ("com. mySQL. JDBC. driver "); Conn = drivermanager. getconnection (URL, user, PWD);} catch (classnotfoundexception e) {e. printstacktrace ();} catch (sqlexception e) {e. printstacktrace () ;}return conn;} public void createbeanmethod () {connection conn = getconnection (); // obtain the database connection // mydb is the database name string strsql = "select * from" + tablename; preparedstatement pstmt = NULL; resultsetmetadata rsmd = NULL; try {pstmt = Conn. preparestatement (strsql); rsmd = pstmt. getmetadata (); int size = rsmd. getcolumncount (); // total number of columns colnames = new string [size]; coltypes = new string [size]; colsizes = new int [size]; for (INT I = 0; I <rsmd. getcolumncount (); I ++) {colnames [I] = rsmd. getcolumnname (I + 1); coltypes [I] = rsmd. getcolumntypename (I + 1); If (coltypes [I]. equalsignorecase ("datetime") {f_util = true;} If (coltypes [I]. equalsignorecase ("image") | coltypes [I]. equalsignorecase ("text") {f_ SQL = true;} colsizes [I] = rsmd. getcolumndisplaysize (I + 1);} string content = parse (colnames, coltypes, colsizes); try {filewriter fw = new filewriter (initcap (tablename) + ". java "); printwriter PW = new printwriter (FW); PW. println (content); PW. flush (); PW. close ();} catch (ioexception e) {e. printstacktrace () ;}} catch (sqlexception e) {e. printstacktrace ();} finally {try {pstmt. close (); Conn. close ();} catch (sqlexception e) {e. printstacktrace () ;}}/ *** parse (generate entity class subject Code ) */Private string parse (string [] colnames, string [] coltypes, int [] colsizes) {stringbuffer sb = new stringbuffer (); If (f_util) {sb. append ("Import Java. util. date; \ r \ n ");} If (f_ SQL) {sb. append ("Import Java. SQL. *; \ r \ n ");} sb. append ("public class" + initcap (tablename) + "{\ r \ n"); processallattrs (SB); processallmethod (SB); sb. append ("} \ r \ n"); system. out. println (sb. tostring (); return sb. tostring ();}/*** generate all methods ** @ Param Sb */private void processallmethod (stringbuffer SB) {for (INT I = 0; I <colnames. length; I ++) {sb. append ("\ tpublic void set" + initcap (colnames [I]) + "(" + sqltype2javatype (coltypes [I]) + "" + colnames [I] + ") {\ r \ n "); sb. append ("\ t \ tThis. "+ colnames [I] +" = "+ colnames [I] +"; \ r \ n "); sb. append ("\ t} \ r \ n"); sb. append ("\ tpublic" + sqltype2javatype (coltypes [I]) + "get" + initcap (colnames [I]) + "() {\ r \ n"); sb. append ("\ t \ treturn" + colnames [I] + "; \ r \ n"); sb. append ("\ t} \ r \ n") ;}/ *** parse the output attribute ** @ return */private void processallattrs (stringbuffer SB) {for (INT I = 0; I <colnames. length; I ++) {sb. append ("\ tprivate" + sqltype2javatype (coltypes [I]) + "" + colnames [I] + "; \ r \ n ");}} /*** change the first letter of the input string to uppercase *** @ Param Str * @ return */private string initcap (string Str) {char [] CH = Str. tochararray (); If (CH [0]> = 'A' & Ch [0] <= 'Z') {ch [0] = (char) (CH [0]-32);} return new string (CH);} private string sqltype2javatype (string sqltype) {If (sqltype. equalsignorecase ("bit") {return "bool";} 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") {return "double";} else if (sqltype. equalsignorecase ("money") | sqltype. inclusignorecase ("smallmoney") {return "double";} else if (sqltype. equalsignorecase ("varchar") | sqltype. equalsignorecase ("char") | sqltype. equalsignorecase ("nvarchar") | sqltype. equalsignorecase ("nchar") {return "string";} else if (sqltype. equalsignorecase ("datetime") {return "date";} else if (sqltype. equalsignorecase ("image") {return "blob";} else if (sqltype. equalsignorecase ("text") {return "clob";} return NULL;} public static void main (string [] ARGs) {createbean = new createbean (); createbean. createbeanmethod ();}}