JDBC reads database metadata and generates Java entity classes

Source: Internet
Author: User

JDBC reads database metadata and generates a Java entity class <br/> package COM. nffish. util; </P> <p> Import Java. io. filewriter; <br/> Import Java. io. ioexception; <br/> Import Java. io. printwriter; <br/> Import Java. SQL. connection; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. resultsetmetadata; <br/> Import Java. SQL. sqlexception; </P> <p> Import COM. nffish. common. dbsession; </P> <p> public class genentitytool {<br/> private string tablename = "petdiary"; </P> <p> private string [] colnames; // array of column names </P> <p> private string [] coltypes; // array of column name types </P> <p> private int [] colsizes; // array of column name size </P> <p> private Boolean f_util = false; // whether to import the Java package. util. * </P> <p> private Boolean f_ SQL = false; // whether to import the Java package. SQL. * </P> <p> Public genentitytool () {<br/> connection conn = dbsession. getconnection (); // obtain the database connection <br/> string strsql = "select * from" + tablename; <br/> try {<br/> preparedstatement pstmt = Conn. preparestatement (strsql); <br/> resultsetmetadata rsmd = pstmt. getmetadata (); <br/> int size = rsmd. getcolumncount (); // total number of columns <br/> colnames = new string [size]; <br/> coltypes = new string [size]; <br/> colsizes = new int [size]; <br/> for (INT I = 0; I <rsmd. getcolumncount (); I ++) {<br/> colnames [I] = rsmd. getcolumnname (I + 1); <br/> coltypes [I] = rsmd. getcolumntypename (I + 1); <br/> If (coltypes [I]. equalsignorecase ("datetime") {<br/> f_util = true; <br/>}< br/> If (coltypes [I]. equalsignorecase ("image") <br/> | coltypes [I]. repeated signorecase ("text") {<br/> f_ SQL = true; <br/>}< br/> colsizes [I] = rsmd. getcolumndisplaysize (I + 1); <br/>}< br/> string content = parse (colnames, coltypes, colsizes ); <br/> try {<br/> filewriter fw = new filewriter (initcap (tablename) + ". java "); <br/> printwriter PW = new printwriter (FW); <br/> PW. println (content); <br/> PW. flush (); <br/> PW. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> dbsession. closeconnection (conn); <br/>}</P> <p>/** <br/> * parse (generate entity-class subject code) <br/> */<br/> private string parse (string [] colnames, string [] coltypes, int [] colsizes) {<br/> stringbuffer sb = new stringbuffer (); <br/> If (f_util) {<br/> Sb. append ("Import Java. util. date;/R/N "); <br/>}< br/> If (f_ SQL) {<br/> Sb. append ("Import Java. SQL. *;/R/n/R/N "); <br/>}< br/> Sb. append ("public class" + initcap (tablename) + "{/R/N"); <br/> processallattrs (SB); <br/> processallmethod (SB ); <br/> Sb. append ("}/R/N"); <br/> system. out. println (sb. tostring (); <br/> return sb. tostring (); </P> <p >}</P> <p>/** <br/> * generate all methods <br/> * @ Param sb <br/> */<br/> private void processallmethod (stringbuffer SB) {<br/> for (INT I = 0; I <colnames. length; I ++) {<br/> Sb. append ("/tpublic void set" + initcap (colnames [I]) + "(" <br/> + sqltype2javatype (coltypes [I]) + "" + colnames [I] <br/> + ") {/R/N"); <br/> Sb. append ("/T/tThis. "+ colnames [I] +" = "+ colnames [I] +";/R/N "); <br/> Sb. append ("/t}/R/N"); </P> <p> Sb. append ("/tpublic" + sqltype2javatype (coltypes [I]) + "get" <br/> + initcap (colnames [I]) + "() {/R/N "); <br/> Sb. append ("/T/treturn" + colnames [I] + ";/R/N"); <br/> Sb. append ("/t}/R/N "); <br/>}</P> <p>/** <br/> * parse output attributes <br/> * @ return <br/> */<br/> private void processallattrs (stringbuffer SB) {<br/> for (INT I = 0; I <colnames. length; I ++) {<br/> Sb. append ("/tprivate" + sqltype2javatype (coltypes [I]) + "<br/> + colnames [I] +";/R/N "); </P> <p >}< br/>}</P> <p>/** <br/> * change the first letter of the input string to uppercase. <br/> * <br/> * @ Param STR <br/> * @ return <br/> */<br/> private string initcap (string Str) {<br/> char [] CH = Str. tochararray (); <br/> If (CH [0]> = 'A' & Ch [0] <= 'Z ') {<br/> CH [0] = (char) (CH [0]-32); <br/>}< br/> return new string (CH ); <br/>}</P> <p> private string sqltype2javatype (string sqltype) {<br/> If (sqltype. equalsignorecase ("bit") {<br/> return "bool"; <br/>}else if (sqltype. equalsignorecase ("tinyint") {<br/> return "Byte"; <br/>} else if (sqltype. inclusignorecase ("smallint") {<br/> return "short"; <br/>} else if (sqltype. equalsignorecase ("int") {<br/> return "int"; <br/>}else if (sqltype. equalsignorecase ("bigint") {<br/> return "long"; <br/>} else if (sqltype. equalsignorecase ("float") {<br/> return "float"; <br/>} else if (sqltype. equalsignorecase ("decimal") <br/> | sqltype. equalsignorecase ("numeric") <br/> | sqltype. repeated signorecase ("real") {<br/> return "double"; <br/>}else if (sqltype. equalsignorecase ("money") <br/> | sqltype. repeated signorecase ("smallmoney") {<br/> return "double"; <br/>} else if (sqltype. equalsignorecase ("varchar") <br/> | sqltype. equalsignorecase ("char") <br/> | sqltype. equalsignorecase ("nvarchar") <br/> | sqltype. equalsignorecase ("nchar") {<br/> return "string"; <br/>} else if (sqltype. equalsignorecase ("datetime") {<br/> return "date"; <br/>}</P> <p> else if (sqltype. repeated signorecase ("image") {<br/> return "blob"; <br/>}else if (sqltype. repeated signorecase ("text") {<br/> return "clob"; <br/>}< br/> return NULL; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> New genentitytool (); <br/>}< br/>

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.