Metadata meta data

Source: Internet
Author: User

First, the basic

Definition: A description of the database, table, and column-related information.
When to use: use when you want to get information about a database.
One "database meta-data-through DatabaseMetaData
DatabaseMetaData Dbmd = Conn.getdatabasemetadata ();
Code

Connection conn =c3p0utils.getmysqlconnection (); //get database meta-data ———— DatabaseMetaDataDatabaseMetaData Dbmd =Conn.getmetadata (); String Driver=Dbmd.getdrivername (); System.out.println ("DriverName:" + driver);//mysql-ab JDBC DriverString URL =Dbmd.geturl (); System.out.println ("URL:" + URL);//Jdbc:mysql://127.0.0.1:3306/jdbc            intLevel = Dbmd.getdefaulttransactionisolation ();//To get the transaction level, the level of the value can have the field value of the Connectin class to viewSystem.out.println ("Level:" + level);//2String ProductName =Dbmd.getdatabaseproductname (); System.out.println ("ProductName:" + productName);//MySQL


     Two "parameter metadata-via Parametermetadata
             Parametermetadata PMD = pstmt.getparametermetadata;
        code:

           Connection conn = c3p0utils.getmysqlconnection ();             = Conn.preparestatement (Sqlmapping.query_inf); // SELECT * from jdbc.test_batch WHERE id =?             // get parameter metadata            Parametermetadata PMD = pstmt.getparametermetadata ();             int cnt = pmd.getparametercount ();            System.out.println ("Total" + cnt + "parameters"); // The public parameter, which is the SQL statement?  The number, here is 1


Three "result set data elements--by ResultSetMetaData
ResultSetMetaData RSMD = Rs.getmetadata ();
Code

String SQL = "SELECT * FROM Jdbc.test_batch"; Connection Conn=c3p0utils.getmysqlconnection (); PreparedStatement pstmt=conn.preparestatement (SQL); ResultSet RS=Pstmt.executequery (); ResultSetMetaData RSMD=Rs.getmetadata (); intCNT = Rsmd.getcolumncount ();//number of result sets             for(inti = 1; I <= CNT; i++) {String colname= Rsmd.getcolumnname (i);//get the name of each columnSystem.out.println ("colname =" + colname);//colname = id colname = Name colname = Age}


Summarize:
The core rules are as follows:
ResultSetMetaData Resultset.getmetadata ();
Parametermetadata Psmt.getmetadata ();
DatabaseMetaData Conn.getmetadata ();

Second, using metadata meta-data optimization cud operation and R operation (packaged into tools for easy use)
One "cud operation
Code

        //cud Operation         Public Static intUpdate (String sql, object[] params)throwsSQLException {Connection conn=c3p0utils.getmysqlconnection (); PreparedStatement pstmt= conn.preparestatement (SQL);//INSERT into Meta.user (username,salary) VALUES (?,?) //Get parameter MetadataParametermetadata PMD =Pstmt.getparametermetadata (); //number of parameters obtained            intSize =Pmd.getparametercount (); //the value of the loop-bound object             for(inti = 0; i < size; i++) {Pstmt.setobject (i+ 1, Params[i]); }            introws =pstmt.executeupdate ();            C3p0utils.close (conn); returnrows; }    


Two "R operation
Code

        //R operation (requires: the name of the JavaBean must be the same as the name of the database field)         Public Static<T> list<t> query (String sql, object[] params, Class clazz)throwsSQLException, Instantiationexception, Illegalaccessexception, invocationtargetexception {Connection conn =c3p0utils.getmysqlconnection (); List<T> list =NewArraylist<t>(); PreparedStatement pstmt= conn.preparestatement (SQL);//SELECT * from meta.user WHERE id =?Parametermetadata PMD =Pstmt.getparametermetadata (); intCNT =Pmd.getparametercount ();  for(inti = 0; I < CNT; i++) {Pstmt.setobject (i+1, Params[i]); } ResultSet RS=Pstmt.executequery ();  while(Rs.next ()) {ResultSetMetaData Rsmd=Rs.getmetadata (); intColumn =Rsmd.getcolumncount (); T T=(T) clazz.newinstance ();  for(inti = 0; I < column; i++) {String colname= Rsmd.getcolumnname (i+1);                Beanutils.setproperty (t, ColName, Rs.getobject (colname));            } list.add (t); }

C3p0utils.closeall (conn, pstmt, RS);

return list;
}



Metadata meta data

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.