Disclaimer: The following case is in the database:
Create Database Day12;
Use DAY12;
CREATE TABLE Account (
ID int primary KEY auto_increment,
Name varchar (30),
Money double
);
The C3P0 database used
Configuration files: C3p0-config.xml file
<?xml version= "1.0" encoding= "UTF-8"?><c3p0-config> <default-config> <property name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "Jdbcurl" >jdbc:mysql:// localhost:3306/day12?generatesimpleparametermetadata=true</property> <property name= "user" >root </property> <property name= "password" >169500</property> </default-config></ C3p0-config>
1. Introduction of the GetMetaData () method of connection
Case:
Package Com.itheima.matadata;import Java.sql.connection;import Java.sql.databasemetadata;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import Org.apache.commons.dbutils.dbutils;import Com.mchange.v2.c3p0.combopooleddatasource;public Class connectionmatadata {public static void main (string[] args) {Connection con=null; PreparedStatement Ps=null; ResultSet Rs=null; Combopooleddatasource source=new Combopooleddatasource (); try {con=source.getconnection ();D atabasemetadata metaDate = Con.getmetadata ();//exchange for metadata, mainly to get database configuration information String drivername = Metadate.getdrivername ();//driver String url = Metadate.geturl ();//url address String userName = Metadate.getusername ();//user name System.out.println (drivername); System.out.println (URL); System.out.println (userName);} catch (SQLException e) {e.printstacktrace ();} Finally{dbutils.closequietly (Con, PS, RS);}}}
Operation Result:
Mysql-ab JDBC Driver
Jdbc:mysql://localhost:3306/day12?generatesimpleparametermetadata=true
[Email protected]
2. Introduction of the Getparametermetadata () method of Pareparestatement
Case:
Package Com.itheima.matadata;import Java.sql.connection;import Java.sql.parametermetadata;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import Org.apache.commons.dbutils.dbutils;import Com.mchange.v2.c3p0.combopooleddatasource;public Class preparestatementmedadata {public static void main (string[] args) {Connection conn = null; PreparedStatement PS = null; ResultSet rs = null; Combopooleddatasource Source = new Combopooleddatasource (); try{conn = Source.getconnection ();p s = Conn.preparestatement ("SELECT * from account where name=?") and money=? "); /--get parameter metadata parametermetadata MetaData = Ps.getparametermetadata ();//----number of arguments int count = Metadata.getparametercount () ; System.out.println (count);//----Gets the type of the argument string type = Metadata.getparametertypename (1); System.out.println (type); String type2 = Metadata.getparametertypename (2); System.out.println (type2);} catch (Exception e) {e.printstacktrace ();} finally{dbutils.closequietly (Conn, PS, RS);}}}
One of the most important is to get the number of parameters,,, primarily for the framework, which is used in the update () method in the definition of my own queryrunner in the back of my article.
Operation Result:
2: Description has two question marks
VARCHAR: At present no matter what type is varchar,mysql does not have this function.? generatesimpleparametermetadata=true See configuration in C3p0-config.xml configuration file
VARCHAR
3. Introduction of GetMetaData in resultset
Case:
Package Com.itheima.matadata;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.resultsetmetadata;import Java.sql.sqlexception;import Org.apache.commons.dbutils.dbutils;import Com.mchange.v2.c3p0.combopooleddatasource;public class ResultMetaData { public static void Main (string[] args) {Connection con=null; PreparedStatement Ps=null; ResultSet Rs=null; Combopooleddatasource source=new Combopooleddatasource (); try {con=source.getconnection ();p s=con.preparestatement ( "SELECT * from"); Rs=ps.executequery (); ResultSetMetaData metaData = Rs.getmetadata (); int count = Metadata.getcolumncount (); System.out.println (count);//column name such as ID name moneystring columnName = metadata.getcolumnname (1); System.out.println (columnName);//Gets the type of the name of the column, such as the type of ID is the type of int name is the type of varchar money is doublestring columntypename = Metadata.getcolumntypename (2); System.out.println (columntypename);} catch (SQLException e) {e.printstacktrace ();} Finally{dbutils.closequietly (Con, PS, Rs);}}}
int count = Metadata.getcolumncount (); This table has a few columns.
String columnName = metadata.getcolumnname (1); Gets the name of the first column, such as ID name money
String columntypename = Metadata.getcolumntypename (2); Gets the type of the name of the first column of type ID is int, etc.
Operation Result:
3
Id
VARCHAR
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The meta-data of black Horse DAY12 database