标签:元数据
声明:下面的案例在数据库为:
create database day12;
use day12;
create table account(
id int primary key auto_increment,
name varchar(30),
money double
);
使用的c3p0数据库
配置文件:c3p0-config.xml文件
<?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.介绍Connection的getMetaData()方法
案例:
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(); DatabaseMetaData metaDate = con.getMetaData();//换取元数据,主要是获取数据库配置信息 String driverName = metaDate.getDriverName();//驱动名 String url = metaDate.getURL();//url地址 String userName = metaDate.getUserName();//用户名 System.out.println(driverName); System.out.println(url); System.out.println(userName); } catch (SQLException e) { e.printStackTrace(); }finally{ DbUtils.closeQuietly(con, ps, rs); } } }运行结果:
MySQL-AB JDBC Driver
jdbc:mysql://localhost:3306/day12?generateSimpleParameterMetadata=true
root@localhost
2.介绍ParepareStatement的getParameterMetaData()方法
案例:
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(); ps = conn.prepareStatement("select * from account where name=? and money=?"); //--获取参数元数据 ParameterMetaData metaData = ps.getParameterMetaData(); //----参数的个数 int count = metaData.getParameterCount(); System.out.println(count); //----获取参数的类型 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); } } }其中最重要的是获取参数的个数,,,主要是给框架师使用的,在我的文章的后面自己定义自己的QueryRunner中的update()方法中使用到了。
运行结果:
2 :说明有两个问号
VARCHAR :目前不管什么类型都是varchar,mysql没有这个功能.?generateSimpleParameterMetadata=true见c3p0-config.xml的配置文件中的配置
VARCHAR
3.介绍ResultSet中的getMetaData
案例:
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(); ps=con.prepareStatement("select * from account "); rs=ps.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); int count = metaData.getColumnCount(); System.out.println(count); //列的名字比如id name money String columnName = metaData.getColumnName(1); System.out.println(columnName); //获取列的名字的类型如id的类型是int name 的类型是varchar money的类型是double String columnTypeName = metaData.getColumnTypeName(2); System.out.println(columnTypeName); } catch (SQLException e) { e.printStackTrace(); }finally{ DbUtils.closeQuietly(con, ps, rs); } } }int count = metaData.getColumnCount(); 这个表一共几列
运行结果:
3
id
VARCHAR
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:元数据
原文地址:http://blog.csdn.net/u014010769/article/details/46727219