JDBC之ParameterMetaData

1、ParameterMetaData pmd = preparedStatement.getParameterMetaData();

2、通过 ParameterMetaData可以获得参数信息。

JdbcUtils中的代码:

package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Jdbc工具类
 */
public final class JdbcUtils {
	
	//省略localhost:3306
	private static String url = "jdbc:mysql:///jdbc";
	private static String url2 
		= "jdbc:mysql:///jdbc?user=root&password=root&"
		+ "useUnicode=true&generateSimpleParameterMetadata=true";
	private static String username = "root";
	private static String password = "root";
	
	/**
	 * 构造器私用,防止直接创建对象,
	 * 当然通过反射可以创建
	 */
	private JdbcUtils(){
		
	}
	
	//保证只是注册一次驱动
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	/**
	 * 获取连接
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url2);
		//return DriverManager.getConnection(url, username, password);
	}
	
	/**
	 * 释放资源
	 */
	public static void free(ResultSet rs, Statement st, Connection conn) {
		//规范的关系连接的方式
		try{
			if(rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try{
				if(st != null) {
					st.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if(conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
测试类中的代码:

package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

/**
 * 参数的原信息测试
 */
public class ParameterMetaTest {

	/**
	 * 测试从数据库中读取数据
	 * @throws SQLException
	 */
	@Test
	public void testRead() throws SQLException {
		//第二个参数传java.sql.Date或java.util.Date都可以测试成功
		Object[] params = new Object[] { "lisi",
				new java.util.Date(System.currentTimeMillis()), 100f };
		read("select * from user where name = ? and birthday < ? and money > ?", params);
	}
	
	/**
	 * 从数据库中读取数据
	 * @param sql
	 * @param params
	 * @throws SQLException
	 */
	static void read(String sql, Object[] params) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql);
			//获取参数原信息
			ParameterMetaData pmd = ps.getParameterMetaData();
			//通过获取参数个数来提高代码灵活性
			int count = pmd.getParameterCount();
			System.out.println("参数个数:" + count);
			
			//打印数据库参数信息,MySQL的输出并不准确
			for(int i = 1; i <= count; i ++) {
				System.out.print(pmd.getParameterClassName(i) + "\t");
				System.out.print(pmd.getParameterType(i) + "\t");
				System.out.println(pmd.getParameterTypeName(i));
				ps.setObject(i, params[i - 1]);
			}
			
			//执行查询操作
			rs = ps.executeQuery();
			
			while(rs.next()) {
				System.out.println(rs.getInt("id") + "\t"
						+ rs.getString("name") + "\t" + rs.getDate("birthday") 
						+ "\t" + rs.getFloat("money"));
			}
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
		
	}
}
测试结果:

参数个数:3
java.lang.String 12 VARCHAR
java.lang.String 12 VARCHAR
java.lang.String 12 VARCHAR
2 lisi 2017-06-06 310.0


你可能感兴趣的:(JDBC)