继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

跟着课程写了一个很简单的学生信息管理系统

qq_MEGALOVANIA_0
关注TA
已关注
手记 15
粉丝 7
获赞 23
package model;

import java.util.Date;

public class Student {
	private String id;
	private String name;
	private Date birthday;
	private int age;
	private float score;
	public Student(){}
	public Student(String id, String name, Date birthday, int age, float score) {
		this.id = id;
		this.name = name;
		this.birthday = birthday;
		this.age = age;
		this.score = score;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public float getScore() {
		return score;
	}
	public void setScore(float score) {
		this.score = score;
	}
	@Override
	public String toString() {
		return "id:" + id + "  姓名:" + name + "  生日:" + birthday + "  年领:" + age + "  成绩:" + score;
	}
	
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package db;

import java.sql.*;

public class DBUtil {
	private static final String url = "jdbc:mysql://localhost:3306/student?useSSL=false";
	private static final String name = "root";
	private static final String password = "123";
	
	private static Connection conn =null;
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url,name,password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Connection getConnection(){
		return conn;
	}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.sql.PreparedStatement;


import db.DBUtil;
import model.Student;

public class StudentDao {
	//添加新学生
	public void addStudent(Student s) throws SQLException{
		Connection conn = DBUtil.getConnection();
		String sql = "insert into student"+
				     "(id,name,age,score,birthday)"+
				     "value(?,?,?,?,?)";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1,s.getId());
		ptmt.setString(2, s.getName());
		ptmt.setInt(3, s.getAge());
		ptmt.setFloat(4, s.getScore());
		ptmt.setDate(5, new Date(s.getBirthday().getTime()));
		ptmt.execute();
		ptmt.close();
		conn.close();
	}
	//根据学号删除学生
	public void delStudent(String id) throws SQLException{
		Connection conn = DBUtil.getConnection();
		String sql = "delete from student where id=?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1,id);
		ptmt.execute();
		ptmt.close();
		conn.close();
	}
	//更新学生信息
	public void updateStudent(String id,Student s) throws SQLException{
		Connection conn = DBUtil.getConnection();
		String sql = "update student set id=?,name=?,birthday=?,age=?,score=?"+
					 "where id=?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1, s.getId());
		ptmt.setString(2, s.getName());
		ptmt.setDate(3,new Date(s.getBirthday().getTime()));
		ptmt.setInt(4, s.getAge());
		ptmt.setFloat(5, s.getScore());
		ptmt.setString(6,id);
		ptmt.execute();
		ptmt.close();
		conn.close();
	}
	//根据学号查询学生信息
	public Student queryStudent(String id) throws SQLException{
		Connection conn = DBUtil.getConnection();
		String sql = "select * from student where id=?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1,id);
		ResultSet rs = ptmt.executeQuery();
		Student s = null;
		while(rs.next()){
			s = new Student();
			s.setId(rs.getString("id"));
			s.setName(rs.getString("name"));
			s.setAge(rs.getInt("age"));
			s.setBirthday(rs.getDate("birthday"));
			s.setScore(rs.getFloat("score"));
		}
		rs.close();
		ptmt.close();
		conn.close();
		return s;
	}
	//查找所有学生的信息
	public List<Student> queryAllStudents() throws SQLException{
		Connection conn = DBUtil.getConnection();
		Statement stmt = conn.createStatement();
		String sql = "select * from student";
		ResultSet rs = stmt.executeQuery(sql);
		List<Student> students =new ArrayList<Student>();
		Student stu = null;
		while(rs.next()){
			stu = new Student();
			stu.setId(rs.getString("id"));
			stu.setName(rs.getString("name"));
			stu.setBirthday(rs.getDate("birthday"));
			stu.setAge(rs.getInt("age"));
			stu.setScore(rs.getFloat("score"));
			students.add(stu);
		}
		rs.close();
		stmt.close();
		conn.close();
		return students;
	}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package Action;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import dao.StudentDao;
import model.Student;

public class StudentAction {
	StudentDao sd = new StudentDao();
	Scanner sc = new Scanner(System.in);
	//新增
	public void add() throws SQLException{
		String id = "";
		String name = "";
		Date birthday = null;
		int age = 0;
		float score = 0;
		System.out.print("输入学生id:");
		if(sc.hasNext()){
			id = sc.next();
		}
		System.out.print("输入学生姓名:");
		if(sc.hasNext()){
			name = sc.next();
		}
		System.out.print("输入学生生日:");
		if(sc.hasNext()){
			String dateString = sc.next();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				birthday = sdf.parse(dateString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.print("输入学生年龄:");
		if(sc.hasNextInt()){
			age = sc.nextInt();
		}
		System.out.print("输入学生成绩:");
		if(sc.hasNextFloat()){
			score = sc.nextFloat();
		}
		Student s = new Student(id,name,birthday,age,score);
		sd.addStudent(s);
		System.out.println("学生信息添加成功!");
	}
	//删除
	public void del() throws SQLException{
		String id = "";
		System.out.print("输入要删除的学生的id:");
		if(sc.hasNext()){
			id = sc.next();
		}
		sd.delStudent(id);
		System.out.println("删除成功!!!");
	}
	//修改某学号的学生
	public void update() throws SQLException{
		String id = "";
		String name = "";
		Date birthday = null;
		int age = 0;
		float score = 0;
		String birthdaystr = null;
		System.out.print("输入要修改的学生的id:");
		if(sc.hasNext()){
			id = sc.next();
		}
		System.out.print("输入修改的学生的姓名:");
		if(sc.hasNext()){
			name = sc.next();
		}
		System.out.print("输入要修改的学生的生日:");
		if(sc.hasNext()){
			birthdaystr = sc.next();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				birthday = sdf.parse(birthdaystr);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.print("输入要修改的学生的年龄:");
		if(sc.hasNext()){
			age = sc.nextInt();
		}
		System.out.print("输入要修改的学生的成绩:");
		if(sc.hasNextFloat()){
			score = sc.nextFloat();
		}
		Student s = new Student(id,name,birthday,age,score);
		sd.updateStudent(id, s);
		System.out.print("修改完成!!");
	}
	//查找指定学号的学生
	public void query() throws SQLException{
		String id = "";
		System.out.print("输入要查询的学生的id:");
		if(sc.hasNext()){
			id = sc.next();
		}
		Student s = sd.queryStudent(id);
		System.out.println("id:"+s.getId());
		System.out.println("姓名:"+s.getName());
		System.out.println("年龄:"+s.getAge());
		System.out.println("生日:"+s.getBirthday());
		System.out.println("成绩:"+s.getScore());
	}
	//查找所有学生
	public void queryAll() throws SQLException{
		List<Student> sl = new ArrayList<Student>();
		sl = sd.queryAllStudents();
		for(Student s:sl){
			System.out.println(s.toString());
		}
	}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package main;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import Action.StudentAction;
import dao.StudentDao;
import model.Student;

public class Test {

	public static void main(String[] args) throws SQLException {
		Scanner sc = new Scanner(System.in);
		StudentAction sa = new StudentAction();
			System.out.println(">>>>>>>>>>>>>>学生管理系统<<<<<<<<<<<<<<<");
			System.out.println("1.新增学生信息
2.删除学生信息
3.修改学生信息
4.查找学生信息
5.显示全部学生信息
"
					         + "6.输入EXIT/E退出");
			System.out.print("输入查询的指令编号:");
		do{
			String order = sc.next();
			if("EXIT".equals(order.toUpperCase())
					||"EXIT".substring(0, 1).equals(order.toUpperCase())){
				System.out.println(">>>>>>>>>>>>>>>>>系统已退出<<<<<<<<<<<<<<<<<<<");
				break;
			}
			switch(order){
			case "1":
				sa.add();
				break;
			case "2":
				sa.del();
				break;
			case "3":
				sa.update();
				break;
			case "4":
				sa.query();
				break;
			case "5":
				sa.queryAll();
				break;
			default:
				System.out.println("输入指令有误!");
				}
		}while(sc.hasNext());
	}
}

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP