码迷,mamicode.com
首页 > 数据库 > 详细

JDBC学习笔记(14):数据库的元数据信息与参数的元数据信息

时间:2015-04-03 19:13:31      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

数据库的元数据信息:

 1 package com.xxyh.jdbc;
 2 import java.sql.DatabaseMetaData;
 3 import java.sql.SQLException;
 4 public class DBMD {
 5     
 6     public static void main(String[] args) throws SQLException {
 7         java.sql.Connection conn = JdbcUtils.getConnection();
 8         DatabaseMetaData dbmd = conn.getMetaData();
 9 //        dbmd.getConnection();
10         System.out.println("数据库连接:" + dbmd.getConnection());
11         System.out.println("驱动名:" + dbmd.getDriverName());
12         System.out.println("数据库名:" + dbmd.getDatabaseProductName());
13         System.out.println("数据库版本号:" + dbmd.getDatabaseProductVersion());
14         System.out.println("是否支持事务:" + dbmd.supportsTransactions());
15     }
16 }
【运行结果】:
数据库连接:com.mysql.jdbc.JDBC4Connection@42ab91e8
驱动名:MySQL Connector Java
数据库名:MySQL
数据库版本号:5.6.21-log
是否支持事务:true  

参数的元数据信息
 1 package com.xxyh.jdbc;
 2 import java.sql.Connection;
 3 import java.sql.ParameterMetaData;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 public class ParameterMetaTest {
 8     
 9     public static void main(String[] args) throws SQLException {
10         read("select * from user where name=? and birthday<? and money>?", null);
11     }
12     
13     static void read(String sql, Object[] params) throws SQLException {
14         Connection conn = null;
15         PreparedStatement ps = null;
16         ResultSet rs = null;
17         
18         try {
19             conn = JdbcUtils.getConnection();
20             ps = conn.prepareStatement(sql);
21             ParameterMetaData pmd = ps.getParameterMetaData();
22             int count = pmd.getParameterCount();// 获取参数个数
23             for (int i = 1; i <= count; i++) {
24                 System.out.print(pmd.getParameterClassName(i)+"\t");
25                 System.out.print(pmd.getParameterType(i)+"\t");
26                 System.out.println(pmd.getParameterTypeName(i));
27             }
28         } finally {
29             JdbcUtils.close(rs, ps, conn);
30         }
31     }
32 }
【运行结果】:
Exception in thread "main" java.sql.SQLException: Parameter metadata not available for the given statement 
原因:驱动设置默认不支持使用这个对象
解决方案:在数据库连接URL加上一个参数generateSimpleParameterMetadata=true"
url="jdbc:mysql://localhost:3306/jdbc?generateSimpleParameterMetadata=true"
?【运行结果】:
java.lang.String    12    VARCHAR
java.lang.String    12    VARCHAR
java.lang.String    12    VARCHAR  
可以发现并没有查询到正确的类型,因为并没有查询数据库,直接返回varchar类型。
 
通用的数据更新操作,不论数据的参数个数:
 1 package com.xxyh.jdbc;
 2 import java.sql.Connection;
 3 import java.sql.Date;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 public class ParameterMetaTest {
 8     
 9     public static void main(String[] args) throws SQLException {
10         Object[] params = new Object[]{"lisi", new Date(System.currentTimeMillis()), 100f};
11         read("select * from user where name=? and birthday < ? and money > ?", params);
12     }
13     
14     static void read(String sql, Object[] params) throws SQLException {
15         Connection conn = null;
16         PreparedStatement ps = null;
17         ResultSet rs = null;
18         
19         try {
20             conn = JdbcUtils.getConnection();
21             ps = conn.prepareStatement(sql);
22 //            ParameterMetaData pmd = ps.getParameterMetaData();
23 //            int count = pmd.getParameterCount();// 获取参数个数
24             for (int i = 1; i <= params.length; i++) {
25                 ps.setObject(i, params[i-1]);
26             }
27             
28             rs = ps.executeQuery();
29             
30             while (rs.next()) {
31                 System.out.println(rs.getInt("id") + "\t" 
32                         + rs.getString("name") + "\t"
33                         + rs.getDate("birthday") + "\t"
34                         + rs.getFloat("money"));
35             }
36         } finally {
37             JdbcUtils.close(rs, ps, conn);
38         }
39     }
40 }
【运行结果】:
2    lisi    1986-01-01    900.0  

JDBC学习笔记(14):数据库的元数据信息与参数的元数据信息

标签:

原文地址:http://www.cnblogs.com/xiaoxiaoyihan/p/4390779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!