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();
		}
	}
}
相關文章
相關標籤/搜索