学生信息管理系统JAVASE版--第0阶段之CMD版--0.1.2 版

改进:

1、把statement改为preparedstatement
2、主类student变量不再重复
3、student的性别属性由 char 类型改为 String 类型
4、添加输入提示


代码:

主类Manager:


package com.laolang.manager;

import java.util.Scanner;

import com.laolang.sqlManager.SqlManager;
import com.laolang.student.Student;

/**
 * 功能:CMD版学生信息管理系统 可以对一个班级内的学生的信息进行简单的增、删、改、查 版本:0.1.2
 * 改进:
 * 1、将Statement改为PreparedStatement,改进查询效率
 * 2、主类中Student不再重复
 * 3、输入时添加了提示
 * 
 * @author Administrator
 * 
 */

public class Manager {

	public static void main(String[] args) {

		int or = 0;//用于保存输入的操作选项
		SqlManager sm = new SqlManager();//数据库处理实例
		
		/**
		 * 接收输入的学生信息
		 */
		Student stu = null;
		int id = 0;
		String name = null;
		int age = 0;
		String sex = null;
		double score = 0.00;
		
		Scanner input = new Scanner( System.in );//用于接收
		
		while (true) {
			or = menuPrint();

			switch (or) {
			// 退出
			case 0: {
				System.out.println("正在退出...");
				System.out.println("------谢谢使用!------");
				System.exit(0);
				
				break;
			}
			// 插入
			case 1:{
				System.out.println("将执行插入操作");
				
				System.out.println("输入学生的完整信息");
				
				/**
				 * 接收学生的信息
				 */
				System.out.print("请输入学生的 编号,由四个数字组成:");
				id = input.nextInt();
				
				System.out.print("请输入学生的姓名,中间不可有空格:");
				name = input.next();
				
				System.out.print("请输入学生的年龄:");
				age = input.nextInt();
				
				System.out.print("请输入学生的性别,F 代表男生, M 代表女生:");
				sex = input.next();
				
				
				System.out.print("请输入学生的成绩,形如 00.00:");
				score = input.nextDouble();
				
				stu = new Student( id, name, age, sex, score );//创建 Student 对象
				
				System.out.println("--------正在执行插入操作...--------");
				sm.insertStudent(stu);//写入到数据库
				
				
				break;
			}
			// 删除
			case 2:{
				System.out.println("将执行删除操作");
				
				System.out.print("输入要查询的学生的编号:");//输入提示
				id = input.nextInt();
				
				System.out.println("-------正在执行删除操作...-------");
				sm.deleteStudentById(id);
				
				break;
				
				
			}
			// 更新
			case 3:{
				
				System.out.println("将要执行更新操作");
				
				System.out.print("请输入要更新信息的学生的 编号,由四个数字组成:");
				id = input.nextInt();
				
				/**
				 * 接收学生的信息
				 */
				
				System.out.print("请输入学生的姓名,中间不可有空格:");
				name = input.next();
				
				System.out.print("请输入学生的年龄:");
				age = input.nextInt();
				
				System.out.print("请输入学生的性别,F 代表男生, M 代表女生:");
				sex = input.next();
				
				
				System.out.print("请输入学生的成绩,形如 00.00:");
				score = input.nextDouble();
				
				stu = new Student( id, name, age, sex, score );//创建 Student 对象
				
				System.out.println("--------正在更新操作...--------");
				sm.updateStudent(stu);
								
				break;
			}
			// 查询
			case 4: {
				System.out.println("将执行查询操作");
				
				
				
				System.out.print("输入要查询的学生的编号:");//输入提示
				
				id = input.nextInt();//接收输入的学生编号
				
				System.out.println("正在执行查询操作...");
				stu = sm.selectStudentById(id);
				System.out.println(stu.toString());
				
				break;
			}
			default: {
				System.out.println("输入正确的命令!1,2,3,4");
				break;
			}
			}
		}

	}

	private static int menuPrint() {

		int or = 0;

		System.out.println();
		System.out.println("1-插入记录" + "\t\t" + "2-删除记录");
		System.out.println("3-更新记录" + "\t\t" + "4-查询记录");
		System.out.println("0-退出");
		System.out.print("输入选项:");
		Scanner input = new Scanner(System.in);
		or = input.nextInt();
		System.out.println("-------------------------------------" + "\n");

		return or;
	}

}


数据库部分SqlManager:

package com.laolang.sqlManager;

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

import com.laolang.student.Student;

// TODO: Auto-generated Javadoc
/**
 * 功能:CMD版0.1.2 实现数据库部分的管理功能 将Statement改为PreparedStatement The Class SqlManager.
 */
public class SqlManager {

	/**
	 * 通过学生编号查询学生信息.
	 * 
	 * @param id
	 * @return the student
	 */
	public Student selectStudentById(int id) {
		Student stu = null;

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			Class.forName(DRIVER);

			conn = DriverManager.getConnection(URL, USERNAME, USEPASSWORD);

			String sqlSelect = "select stuId, stuName, stuAge, stuSex, stuScore from student where stuId = ?";

			ps = conn.prepareStatement(sqlSelect);
			ps.setInt(1, id);// 设置占位符对应的值

			rs = ps.executeQuery();

			while (rs.next()) {

				// 返回值
				String name = rs.getString("stuName");
				int age = rs.getInt("stuAge");
				String sex = rs.getString("stuSex");
				double score = rs.getDouble("stuScore");

				char Sex[] = sex.toCharArray();

				// 创建Student对象
				stu = new Student(id, name, age, sex, score);

			}

			System.out.println("查询成功!");

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return stu;
	}

	/**
	 * 通过学生编号删除学生信息.
	 * 
	 * @param id
	 *            the id
	 */
	public void deleteStudentById(int id) {
		Connection conn = null;
		PreparedStatement ps = null;

		try {
			Class.forName(DRIVER);

			conn = DriverManager.getConnection(URL, USERNAME, USEPASSWORD);

			String sqlSelect = "delete from student where stuId = ?";

			ps = conn.prepareStatement(sqlSelect);
			ps.setInt(1, id);// 设置占位符对应的值

			ps.executeUpdate();// 执行SQL语句

			System.out.println("删除成功!");

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 插入学生信息
	 * 
	 * @param stu
	 *            the stu
	 */
	public void insertStudent(Student stu) {

		Connection conn = null;
		PreparedStatement ps = null;

		try {
			Class.forName(DRIVER);

			conn = DriverManager.getConnection(URL, USERNAME, USEPASSWORD);

			String sqlSelect = "insert into student ( stuId, stuName, stuAge, stuSex, stuScore ) values (?,?,?,?,?)";

			ps = conn.prepareStatement(sqlSelect);

			// 设置占位符对应的值
			ps.setInt(1, stu.getId());
			ps.setString(2, stu.getName());
			ps.setInt(3, stu.getAge());
			ps.setString(4, stu.getSex());
			ps.setDouble(5, stu.getScore());

			ps.executeUpdate();// 执行SQL语句

			System.out.println("插入成功!");

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void updateStudent( Student stu ){
		Connection conn = null;
		PreparedStatement ps = null;

		try {
			Class.forName(DRIVER);

			conn = DriverManager.getConnection(URL, USERNAME, USEPASSWORD);

			String sqlSelect = "update student set stuName = ? , stuAge = ?, stuSex = ?, stuScore = ? where stuId = ?";

			ps = conn.prepareStatement(sqlSelect);

			// 设置占位符对应的值
			ps.setString(1, stu.getName());
			ps.setInt(2, stu.getAge());
			ps.setString(3, stu.getSex());
			ps.setDouble(4, stu.getScore());
			ps.setInt(5, stu.getId());
			
			ps.executeUpdate();// 执行SQL语句

			System.out.println("更新成功!");

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/** The Constant URL. */
	private final static String URL = "jdbc:mysql://localhost:3306/studentmanager0.1.2";// 数据库连接地址

	/** The Constant USERNAME. */
	private final static String USERNAME = "root";// 用户名

	/** The Constant USEPASSWORD. */
	private final static String USEPASSWORD = "123456";// 密码

	/** The Constant DRIVER. */
	public final static String DRIVER = "com.mysql.jdbc.Driver";
}

学生对象Student:

package com.laolang.student;

// TODO: Auto-generated Javadoc
/**
 * 功能:学生对象.
 *将性别属性由 char 类型改为 String 类型
 * @author Administrator
 */

public class Student {

	/**
	 * 默认构造方法
	 * 初始化学生信息
	 * Instantiates a new student.
	 */
	public Student() {
		super();
		this.id = 1000;
		this.name = "学生";
		this.age = 0;
		this.sex = "F";
		this.score = 0.00;
	}

	/**
	 * Instantiates a new student.
	 * 
	 * 自定义构造方法用于生成实例
	 *
	 * @param id the id
	 * @param name the name
	 * @param age the age
	 * @param sex the sex
	 * @param score the score
	 */
	public Student(int id, String name, int age, String sex, double score) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.score = score;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "student [id=" + id + ", name=" + name + ", age=" + age
				+ ", sex=" + sex + ", score=" + score + "]";
	}

	
	
	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * Sets the id.
	 *
	 * @param id the new id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Gets the name.
	 *
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 *
	 * @param name the new name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the age.
	 *
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * Sets the age.
	 *
	 * @param age the new age
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * Gets the sex.
	 *
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}

	/**
	 * Sets the sex.
	 *
	 * @param sex the new sex
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}

	/**
	 * Gets the score.
	 *
	 * @return the score
	 */
	public double getScore() {
		return score;
	}

	/**
	 * Sets the score.
	 *
	 * @param score the new score
	 */
	public void setScore(double score) {
		this.score = score;
	}

	
	
	/** 学生编号  */
	private int id;
	
	/** 学生姓名 */
	private String name;
	
	/** 学生年龄 */
	private int age;
	
	/** 学生性别 */
	private String sex;
	
	/** 学生成绩 */
	private double score;
}





















  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值