DBUtil实战

问题:设计一个简单的学生操作系统,实现功能要求如下

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

import com.jd.util.DBUtil;
import com.jd.util.IRowMapper;

public class Main {
	

	public static void main(String[] args) {
		System.out.println("*********************************");
		System.out.println("*\t\t\t\t*");
		System.out.println("*\t欢迎使用学生信息管理系统\t*");
		System.out.println("*\t\t\t\t*");
		System.out.println("*********************************");
		while (true) {
			menu();
		}
	}
	
	public static void menu() {
		System.out.println("1、添加学生信息");
		System.out.println("2、删除学生信息");
		System.out.println("3、修改学生信息");
		System.out.println("4、查询学生信息");
		System.out.println("请输入操作,以Enter键结束:");
		Scanner scanner = new Scanner(System.in);
		int option  = scanner.nextInt();
		class RowMapper implements IRowMapper {
			@Override
			public void rowMapper(ResultSet resultSet) {
				try {
					while (resultSet.next()) {
						String id = resultSet.getString("id");
						String name = resultSet.getString("name");
						System.out.println(id);
						System.out.println(name);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		switch (option) {
			case 1:{
				System.out.println("请输入需要添加的学生学号");
				String id=scanner.next();
				if(DBUtil.exist("select *from student where id=?",id)) {
					System.out.println("该学号已存在");
				}else {
				System.out.println("请输入学生的姓名");
				String name=scanner.next();
				DBUtil.update("insert into student (id,name) VALUES (?,?)",id,name);
				System.out.println("添加成功");
				}
				break;
				}
			case 2:{
				System.out.println("请输入需要删除的学生学号");
				String id=scanner.next();
				if(DBUtil.exist("select *from student where id=?",id)) {
				DBUtil.update("delete from student where id=?",id);
				System.out.println("删除成功");
				}else {
					System.out.println("该学号不存在");
				}
				break;
				}
			case 3:{
				System.out.println("请输入需要修改的学生学号");
				String id=scanner.next();
				if(DBUtil.exist("select *from student where id=?",id)) {
					System.out.println("请输入学生的姓名");
					String name=scanner.next();
					DBUtil.update("update student set name=? where id=?", name,id);
					System.out.println("修改成功");
				}else {
					System.out.println("该学号不存在");
				}
				break;
				}
			case 4:{
				System.out.println("请输入需要查询的学生学号");
				String id=scanner.next();
				if(DBUtil.exist("select *from student where id=?",id)) {
					RowMapper rowMapper = new RowMapper();
					DBUtil.select("select * from student where id=?",rowMapper,id);
				}else {
					System.out.println("该学号不存在");
				}
				break;
				}
			default:
				System.out.println("请输入正确的选项");
		}
	}
}	

在这里我们写的main方法其实只是一个界面而已,真正管理了学生信息使用的是之前的DBUtil工具类,而DBUtil工具类中的方法由menu方法(管理系统的菜单)来进行调用,通过给menu套上一个死循环来使得其可立即重复执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值