execute executeQuery executeUpdate三种的分别封装使用

package com.zhao.practice727_1;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * 测试类
 * @author  Administrator
 *
 */
public class TestDBPreStat {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new TestDBPreStat().testExecute();
	}
	
	/**
	 * 测试通过execute来查询数据
	 */
	public void testExecute(){
		DBPreparedStatement db = new DBPreparedStatement();
		String sql = "select name ,score from test2 where id = ?";
		Object[] arrays = {10};
		Object object =  db.getDataByExecute(sql, arrays);
		
		
		if(object instanceof List){
			List list = (List)object;
			
			for (Object obj : list) {
				Map map = (Map)obj;
				Iterator it = map.keySet().iterator();
				while(it.hasNext()){
					String key =  (String) it.next();
					String value = (String) map.get(key);
					System.out.println(key +"    "+value);
				}
				
			}
		}
		
		db.close();
	}
	
	
	
//	/**
//	 * 测试通过executeQuery或executeUpdate来查询数据
//	 */
//	public void method1(){
//		DBPreparedStatement db = new DBPreparedStatement();
//		String sql = "select name ,score from test2 where id = ?";
//		Object[] arrays = {10};
//		List list = db.getDataBySQL(sql, arrays);
//		
//		
//		for (Object object : list) {
//			Map map = (Map)object;
//			Iterator it = map.keySet().iterator();
//			while(it.hasNext()){
//				String key =  (String) it.next();
//				String value = (String) map.get(key);
//				System.out.println(key +"    "+value);
//			}
//			
//		}
//		db.close();
//	}
	

}

package com.zhao.practice727_1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * PreparedStatement工具类
 * @author  Administrator
 *
 */
public class DBPreparedStatement {
	/**
	 * 驱动字符串
	 */
	private static final String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
	/**
	 * 连接数据库的URL
	 */
	private static final String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Zhao722"; // 连接服务器和数据库Zhao722
	/**
	 * 用户名字符串
	 */
	private static final String USER= "sa";
	/**
	 * 密码字符串
	 */
	private static final String PASSWORD = "zhao";
	/**
	 * 数据库连接
	 */
	Connection conn = null;
	/**
	 * PreparedStatement
	 */
	PreparedStatement pstat = null;
	/**
	 * 结果集ResultSet
	 */
	ResultSet rs = null;
	
	static {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取数据库连接
	 * @return 
	 */
	private Connection getConnection(){
		try {
			
			conn = DriverManager.getConnection(dbURL, USER, PASSWORD);
//			System.out.println(conn);
			return conn;
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获得PreparedStatement对象
	 * @param sql sql语句
	 * @return  PreparedStatement
	 */
	private PreparedStatement getPreparedStatement(String sql){
		try {
			pstat = getConnection().prepareStatement(sql);
			System.out.println(sql);
			return pstat;
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获得结果集
	 * @param arrays 传入的参数数组
	 * @param sql sql语句
	 * @return  ResultSet
	 */
	private ResultSet getResultSet(String sql , Object[] arrays){
		pstat = getPreparedStatement(sql);
		
		try {
			//设置参数
			for (int i = 0; i < arrays.length; i++) {
				pstat.setObject(i+1, arrays[i]);
			}
			//开始查询
			rs = pstat.executeQuery();
			return rs;
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	} 
	/**
	 * 增删改查的操作
	 * @param sql sql语句
	 * @param arrays 传入的参数数组
	 * @return  int
	 */
	private int getDataByUpdate(String sql , Object[] arrays){
		pstat = getPreparedStatement(sql);
		
		try {
			//设置参数
			for (int i = 0; i < arrays.length; i++) {
				pstat.setObject(i+1, arrays[i]);
			}
			//开始增删改查操作
			int i = pstat.executeUpdate();
			return i;
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 对结果集进行处理
	 * @param sql sql语句
	 * @param arrays 传入的参数数组
	 * @return  List
	 */
	private List getDataBySQL(String sql , Object[] arrays){
		List list = new ArrayList();
		try{
			
			rs = getResultSet(sql, arrays);
			//对结果集进行处理
			while(rs.next()){
				//定义Map来保存每行对应的每列的值
				Map map = new HashMap();
				
				//结果集元数据
				ResultSetMetaData rsmd = rs.getMetaData();
				for(int i= 1;i <= rsmd.getColumnCount();i++){
					map.put(rsmd.getColumnName(i), rs.getObject(i));
				}
				//添加map到集合中
				list.add(map);
			}
			return list;
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 *执行execute语句 
	 */
	public Object getDataByExecute(String sql , Object[] arrays){
		int line = 0;
		boolean hasResultSet = false;
		pstat = getPreparedStatement(sql);
		try {
			//设置参数
			for (int i = 0; i < arrays.length; i++) {
				pstat.setObject(i+1, arrays[i]);
			}
			//开始查询
			hasResultSet = pstat.execute();
			
			if(hasResultSet){
				return getDataBySQL(sql, arrays);
			}else{
					line = pstat.getUpdateCount();
					return line;
			}
			
			
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 关闭所有打开的数据库连接
	 */
	public void close(){
		try{
			if(rs != null){
				rs.close();
			}
			if(pstat != null){
				pstat.close();
			}
			if(conn != null){
				conn.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(方便以后的批处理)