>데이터 베이스 >MySQL 튜토리얼 >Java获取数据库各种查询结果_MySQL

Java获取数据库各种查询结果_MySQL

WBOY
WBOY원래의
2016-06-01 13:07:46919검색

在查询时候有时候要一条数据,有时候要的是一个结果集,然而有时候返回就是一个统计值,通过对ResultSet和ResultSetMetaData的变换得到各类所需的查询结果,因为没有利用连接池数据链接管理比较麻烦,所以谢了一个工具类,

package com.sky.connect;import java.lang.reflect.InvocationTargetException;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.commons.beanutils.BeanUtils;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;import com.mysql.jdbc.ResultSetMetaData;/** * DAO设计模式 *  * @author 潘琢文 *  */public class DAO {	/**	 * 更新数据库操作	 * 	 * @param sql	 * @param args	 */	public void update(String sql, Object... args) {		Connection connection = null;		PreparedStatement preparedStatement = null;		try {			connection = JDBCTools.getConnection();			preparedStatement = (PreparedStatement) connection					.prepareStatement(sql);			for (int i = 0; i  T get(Class<t> clazz, String sql, Object... args) {		T entity = null;		Connection connection = null;		PreparedStatement preparedStatement = null;		ResultSet result = null;		try {			connection = JDBCTools.getConnection();			preparedStatement = (PreparedStatement) connection					.prepareStatement(sql);			for (int i = 0; i  map = new HashMap<string object>();			ResultSetMetaData rsmd = (ResultSetMetaData) result.getMetaData();			if (result.next()) {				for (int i = 0; i  0) {				entity = clazz.newInstance();				for (Map.Entry<string object> entry : map.entrySet()) {					String filedName = entry.getKey();					Object filedObject = entry.getValue();					BeanUtils.setProperty(entity, filedName, filedObject);				}			}		} catch (Exception e) {			e.printStackTrace();		} finally {			JDBCTools.release(result, preparedStatement, connection);		}		return entity;	}	/**	 * 通用查询方法,返回一个结果集	 * 	 * @param clazz	 * @param sql	 * @param args	 * @return	 */	public <t> List<t> getForList(Class<t> clazz, String sql, Object... args) {		List<t> list = new ArrayList<t>();		Connection connection = null;		PreparedStatement preparedStatement = null;		ResultSet result = null;		try {			connection = JDBCTools.getConnection();			preparedStatement = (PreparedStatement) connection					.prepareStatement(sql);			for (int i = 0; i > values = handleResultSetToMapList(result);			list = transfterMapListToBeanList(clazz, values);		} catch (Exception e) {			e.printStackTrace();		} finally {			JDBCTools.release(result, preparedStatement, connection);		}		return list;	}	/**	 * 	 * @param clazz	 * @param values	 * @return	 * @throws InstantiationException	 * @throws IllegalAccessException	 * @throws InvocationTargetException	 */	public <t> List<t> transfterMapListToBeanList(Class<t> clazz,			List<map object>> values) throws InstantiationException,			IllegalAccessException, InvocationTargetException {		List<t> result = new ArrayList<t>();		T bean = null;		if (values.size() > 0) {			for (Map<string object> m : values) {				bean = clazz.newInstance();				for (Map.Entry<string object> entry : m.entrySet()) {					String propertyName = entry.getKey();					Object value = entry.getValue();					BeanUtils.setProperty(bean, propertyName, value);				}				// 13. 把 Object 对象放入到 list 中.				result.add(bean);			}		}		return result;	}	/**	 * 	 * @param resultSet	 * @return	 * @throws SQLException	 */	public List<map object>> handleResultSetToMapList(			ResultSet resultSet) throws SQLException {		List<map object>> values = new ArrayList<map object>>();		List<string> columnLabels = getColumnLabels(resultSet);		Map<string object> map = null;		while (resultSet.next()) {			map = new HashMap<string object>();			for (String columnLabel : columnLabels) {				Object value = resultSet.getObject(columnLabel);				map.put(columnLabel, value);			}			values.add(map);		}		return values;	}	/**	 * 	 * @param resultSet	 * @return	 * @throws SQLException	 */	private List<string> getColumnLabels(ResultSet resultSet)			throws SQLException {		List<string> labels = new ArrayList<string>();		ResultSetMetaData rsmd = (ResultSetMetaData) resultSet.getMetaData();		for (int i = 0; i  E getForValue(String sql, Object... args) {		Connection connection = null;		PreparedStatement preparedStatement = null;		ResultSet resultSet = null;		try {			connection = JDBCTools.getConnection();			preparedStatement = (PreparedStatement) connection					.prepareStatement(sql);			for (int i = 0; i <br><pre class="brush:php;toolbar:false">package com.sky.connect;import java.io.IOException;import java.io.InputStream;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Properties;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Driver;import com.mysql.jdbc.PreparedStatement;import com.mysql.jdbc.Statement;/** * JDBC操作的工具类 版本 1.0 *  * @author 潘琢文 *  */public class JDBCTools {	/**	 * 使用preparedStatement进行数据更新	 * 	 * @param sql	 * @param args	 */	public static void update(String sql, Object ... args) {		Connection connection = null;		PreparedStatement preparedStatement = null;		try {			connection = JDBCTools.getConnection();			preparedStatement = (PreparedStatement) connection					.prepareStatement(sql);			for (int i = 0; i <br>
    
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.