經過jdbc來對數據庫中進行各類操做,包含控制檯的輸入選擇機制

package com.zhao.practice727_2;
/**
 * 動做接口
 * @author  Administrator
 *
 */
public interface ActionInterface {
	Object doAction(String sql , Object[] arrays);
	void showSurface();
	void operator();
}

package com.zhao.practice727_2;

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;
/**
 * DBPreparedStatement工具類
 * @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=Test"; // 鏈接服務器和數據庫Test
	/**
	 * 用戶名字符串
	 */
	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();
		}finally{
			close();
		}
		
		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();
		}finally{
			close();
		}
		
		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;
	}
	
	/**
	 * 關閉全部打開的數據庫鏈接
	 */
	private void close(){
		try{
			if(rs != null){
				rs.close();
			}
			if(pstat != null){
				pstat.close();
			}
			if(conn != null){
				conn.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}

package com.zhao.practice727_2;

import java.util.Scanner;

/**
 * 退出類
 * 
 * @author  Administrator
 * 
 */
public class ExitAction implements ActionInterface {

//	Object object;
	
	@Override
	public Object doAction(String sql, Object[] arrays) {
		// 顯示查詢功能
		// this.showSurface();

		DBPreparedStatement db = new DBPreparedStatement();
		Object object = db.getDataByExecute(sql, arrays);

		return object;
	}

	/**
	 * 顯示查詢功能
	 */
	public void showSurface() {
		System.out.println("1:退出");
	}

	/**
	 * 操做方法
	 */
	@Override
	public void operator() {
		Scanner scann = new Scanner(System.in);
		System.out.println("請輸入:");
		int selectId = scann.nextInt();
		switch (selectId) {
		case 1:
			System.out.println("你已退出!!!");
			System.exit(0);
			
			break;
		default:
			break;
		}

	}
}

package com.zhao.practice727_2;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 插入類
 * @author  Administrator
 *
 */
public class InsertAction implements ActionInterface {
	String sql = null;

	Object object;
	
	@Override
	public Object doAction(String sql , Object[] arrays) {
		//顯示查詢功能
//		this.showSurface();
		
		DBPreparedStatement db = new DBPreparedStatement();
		Object object = db.getDataByExecute(sql, arrays);
		
		return object;

	}
	/**
	 * 顯示查詢功能
	 */
	public void showSurface(){
		System.out.println("1:插入全部值");
		
	}
	/**
	 * 操做方法
	 */
	@Override
	public void operator() {
		Scanner scann = new Scanner(System.in);
		System.out.println("請輸入:");
		int selectId = scann.nextInt();
		switch(selectId){
		case 1:
			sql = "insert into employee(employeeId ,employeeName , email ,employeeSalary ,departmentId) " +
					"values(?,?,?,?,?)";
			System.out.println("請輸入要插入的employeeId的值:");
			int employeeId = scann.nextInt();
			
			System.out.println("請輸入要插入的employeeName:");
			String employeeName = scann.nextLine();
			
			System.out.println("請輸入要插入的email:");
			String email = scann.nextLine();
			
			System.out.println("請輸入要插入的employeeSalary:");
			String employeeSalary = scann.nextLine();
			
			System.out.println("請輸入要插入的departmentId:");
			String departmentId = scann.nextLine();
			
			Object[] array = {employeeId ,employeeName , email ,employeeSalary ,departmentId};
			object = doAction(sql, array);
			printResult(object);
			break;
		default :
			break;
		}
	}
	
	/**
	 * 輸出查詢到的結果
	 * @param object
	 */
	private void printResult(Object object){
		List list = (List)object;
		object = list.get(0);
		Map map = (Map)object;
		Iterator it = map.keySet().iterator();
		/**
		 * 輸出字段值
		 */
		while(it.hasNext()){
			String key =  (String) it.next();
			System.out.print(key +"\t\t" );
		}
		System.out.println();
		/**
		 * 輸出value值
		 */
		int listSize = list.size();
		for (int i = 1; i < listSize; i++) {
			object = list.get(i);
			map = (Map)object;
			it = map.keySet().iterator();
			/**
			 * 獲取第一行字符串
			 */
			while(it.hasNext()){
				String key =  (String) it.next();
				String value = map.get(key)+"";
				System.out.print(value+"      " +"\t\t" );
			}
			System.out.println();
			
		} 
	}
}

package com.zhao.practice727_2;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 查詢類
 * @author  Administrator
 *
 */
public class QueryAction implements ActionInterface{
	/**
	 * sql語句
	 */
	String sql= null;
	/**
	 * 對象數組
	 */
	Object[] arrays = null;
	
	
	Object object;
	@Override
	public Object doAction(String sql , Object[] arrays) {
		
		DBPreparedStatement db = new DBPreparedStatement();
		Object object = db.getDataByExecute(sql, arrays);
		
		return object;
	}
	/**
	 * 顯示查詢功能
	 */
	public void showSurface(){
		System.out.println("1:查詢所有");
		System.out.println("2:根據employeeId查詢");
	}
	/**
	 * 操做方法
	 */
	public void operator(){
		Scanner scann = new Scanner(System.in);
		System.out.println("請輸入:");
		int selectId = scann.nextInt();
		switch(selectId){
		case 1:
			int i = 1;
			sql = "select employeeId ,employeeName , email ,employeeSalary ,departmentId from employee where 1 = ?";
			Object[] arrays = {i};
			object = doAction(sql, arrays);
			printResult(object);
			break;
		case 2:
			sql = "select employeeId ,employeeName , email ,employeeSalary ,departmentId  from employee where employeeId = ?";
			System.out.println("請輸入employeeId的值:");
			int employeeId = scann.nextInt();
			Object[] array = {employeeId};
			object = doAction(sql, array);
			printResult(object);
			break;
		default :
			break;
		}
		
	}
	/**
	 * 輸出查詢到的結果
	 * @param object
	 */
	private void printResult(Object object){
		List list = (List)object;
		object = list.get(0);
		Map map = (Map)object;
		Iterator it = map.keySet().iterator();
		/**
		 * 輸出字段值
		 */
		while(it.hasNext()){
			String key =  (String) it.next();
			System.out.print(key +"\t\t" );
		}
		System.out.println();
		/**
		 * 輸出value值
		 */
		int listSize = list.size();
		for (int i = 1; i < listSize; i++) {
			object = list.get(i);
			map = (Map)object;
			it = map.keySet().iterator();
			/**
			 * 獲取第一行字符串
			 */
			while(it.hasNext()){
				String key =  (String) it.next();
				String value = map.get(key)+"";
				System.out.print(value+"      " +"\t\t" );
			}
			System.out.println();
			
		} 
		
	}
}

package com.zhao.practice727_2;
/**
 * 歡迎界面類
 * @author  Administrator
 *
 */
public class WelcomeSurface {
	/**
	 * 選擇號碼
	 */
	private int selectId ;
	
	public int getSelectId() {
		return selectId;
	}

	public void setSelectId(int selectId) {
		this.selectId = selectId;
	}

	/**
	 * 構造方法
	 */
	public WelcomeSurface(){
		System.out.println("歡迎您");
		System.out.println("1:查詢");
		System.out.println("2:更新");
		System.out.println("3:插入");
		System.out.println("4:刪除");
		System.out.println("5:退出");
	}
	
	/**
	 * 判斷具體是哪一種選擇功能	
	 */
	public void selectFunction(){
		ActionInterface action = null;
		
		switch(selectId){
		case 1:
			action = new QueryAction();
			show(action);
			break;
		case 2:
			action = new UpdateAction();
			show(action);
			break;
		case 3:
			action = new InsertAction();
			show(action);
			break;
		case 4:
			action = new DeleteAction();
			show(action);
			break;
		case 5:
			action = new ExitAction();
			show(action);
			break;
		}
		
	}
	
	
	/**
	 * 顯示具體功能界面
	 */
	private void show(ActionInterface action){
		action.showSurface();
		action.operator();
	}
	
}

package com.zhao.practice727_2;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 更新類
 * @author  Administrator
 *
 */
public class UpdateAction implements ActionInterface {
	String sql = null;
	Object[] arrays = null;
	
	Object object;
	
	@Override
	public Object doAction(String sql , Object[] arrays) {
		//顯示查詢功能
//		this.showSurface();
		
		DBPreparedStatement db = new DBPreparedStatement();
		Object object = db.getDataByExecute(sql, arrays);
		
		return object;
	}
	/**
	 * 顯示查詢功能
	 */
	public void showSurface(){
		System.out.println("1:更新employeeName根據employeeId");
	}

	/**
	 * 操做方法
	 */
	@Override
	public void operator() {
		Scanner scann = new Scanner(System.in);
		System.out.println("請輸入:");
		int selectId = scann.nextInt();
		switch(selectId){
		case 1:
			sql = "update employee set employeeName = ? where employeeId =?";
			System.out.println("請輸入要更新的employeeId的值:");
			int employeeId = scann.nextInt();
			
			System.out.println("請輸入要更新的employeeName:");
			String employeeName = scann.nextLine();
			Object[] array = {employeeName , employeeId};
			object = doAction(sql, array);
			printResult(object);
			break;
		default :
			break;
		}

	}
	
	/**
	 * 輸出查詢到的結果
	 * @param object
	 */
	private void printResult(Object object){
		List list = (List)object;
		object = list.get(0);
		Map map = (Map)object;
		Iterator it = map.keySet().iterator();
		/**
		 * 輸出字段值
		 */
		while(it.hasNext()){
			String key =  (String) it.next();
			System.out.print(key +"\t\t" );
		}
		System.out.println();
		/**
		 * 輸出value值
		 */
		int listSize = list.size();
		for (int i = 1; i < listSize; i++) {
			object = list.get(i);
			map = (Map)object;
			it = map.keySet().iterator();
			/**
			 * 獲取第一行字符串
			 */
			while(it.hasNext()){
				String key =  (String) it.next();
				String value = map.get(key)+"";
				System.out.print(value+"      " +"\t\t" );
			}
			System.out.println();
			
		} 
	}
}

package com.zhao.practice727_2;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 更新類
 * @author  Administrator
 *
 */
public class UpdateAction implements ActionInterface {
	String sql = null;
	Object[] arrays = null;
	
	Object object;
	
	@Override
	public Object doAction(String sql , Object[] arrays) {
		//顯示查詢功能
//		this.showSurface();
		
		DBPreparedStatement db = new DBPreparedStatement();
		Object object = db.getDataByExecute(sql, arrays);
		
		return object;
	}
	/**
	 * 顯示查詢功能
	 */
	public void showSurface(){
		System.out.println("1:更新employeeName根據employeeId");
	}

	/**
	 * 操做方法
	 */
	@Override
	public void operator() {
		Scanner scann = new Scanner(System.in);
		System.out.println("請輸入:");
		int selectId = scann.nextInt();
		switch(selectId){
		case 1:
			sql = "update employee set employeeName = ? where employeeId =?";
			System.out.println("請輸入要更新的employeeId的值:");
			int employeeId = scann.nextInt();
			
			System.out.println("請輸入要更新的employeeName:");
			String employeeName = scann.nextLine();
			Object[] array = {employeeName , employeeId};
			object = doAction(sql, array);
			printResult(object);
			break;
		default :
			break;
		}

	}
	
	/**
	 * 輸出查詢到的結果
	 * @param object
	 */
	private void printResult(Object object){
		List list = (List)object;
		object = list.get(0);
		Map map = (Map)object;
		Iterator it = map.keySet().iterator();
		/**
		 * 輸出字段值
		 */
		while(it.hasNext()){
			String key =  (String) it.next();
			System.out.print(key +"\t\t" );
		}
		System.out.println();
		/**
		 * 輸出value值
		 */
		int listSize = list.size();
		for (int i = 1; i < listSize; i++) {
			object = list.get(i);
			map = (Map)object;
			it = map.keySet().iterator();
			/**
			 * 獲取第一行字符串
			 */
			while(it.hasNext()){
				String key =  (String) it.next();
				String value = map.get(key)+"";
				System.out.print(value+"      " +"\t\t" );
			}
			System.out.println();
			
		} 
	}
}

package com.zhao.practice727_2;

import java.util.Scanner;
/**
 * 測試類
 * @author  Administrator
 *
 */
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		WelcomeSurface surface = new WelcomeSurface();
		Scanner scann = new Scanner(System.in);
		System.out.println("請選擇:");
		int selectId = scann.nextInt();
		//設置選擇
		surface.setSelectId(selectId);
		
		//調用WelcomeSurface的selectFunction()方法
		surface.selectFunction();
		
	
	}

}

package com.zhao.practice727_2; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; /** * 刪除類 * * @author Administrator * */ public class DeleteAction implements ActionInterface { String sql = null; Object object; @Override public Object doAction(String sql, Object[] arrays) { // 顯示查詢功能 // this.showSurface(); DBPreparedStatement db = new DBPreparedStatement(); Object object = db.getDataByExecute(sql, arrays); return object; } /** * 顯示查詢功能 */ public void showSurface() { System.out.println("1:刪除employeeId對應的信息"); } /** * 操做方法 */ @Override public void operator() { Scanner scann = new Scanner(System.in); System.out.println("請輸入:"); int selectId = scann.nextInt(); switch (selectId) { case 1: sql = "delete employee where employeeId = ? "; System.out.println("請輸入要刪除的employeeId的值:"); int employeeId = scann.nextInt(); Object[] array = {employeeId }; object = doAction(sql, array); printResult(object); break; default : break; } } /** * 輸出查詢到的結果 * @param object */ private void printResult(Object object){ List list = (List)object; object = list.get(0); Map map = (Map)object; Iterator it = map.keySet().iterator(); /** * 輸出字段值 */ while(it.hasNext()){ String key = (String) it.next(); System.out.print(key +"\t\t" ); } System.out.println(); /** * 輸出value值 */ int listSize = list.size(); for (int i = 1; i < listSize; i++) { object = list.get(i); map = (Map)object; it = map.keySet().iterator(); /** * 獲取第一行字符串 */ while(it.hasNext()){ String key = (String) it.next(); String value = map.get(key)+""; System.out.print(value+" " +"\t\t" ); } System.out.println(); } } }
相關文章
相關標籤/搜索