通过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(); } } }

转载于:https://my.oschina.net/u/586106/blog/69514

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值