JDBC의 ParameterMetaData

3664 단어 JDBC
1、ParameterMetaData pmd = preparedStatement.getParameterMetaData();
2. 파라메터메타데이터를 통해 파라미터 정보를 얻을 수 있다.
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

좋은 웹페이지 즐겨찾기