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

这个程序是一个有进步也有失败的程序

比如没有做好多表查询,也没有做好主类中的逻辑处理。总之又要去复习了。

改进:

1、使用第三工具dbutils对数据库操作部分进行了封装

2、把URL等变量改为属性文件的形式

代码:

MYSQL:

student表:

CREATE TABLE student(
stuId INT PRIMARY KEY AUTO_INCREMENT,				-- 学生编号
stuName VARCHAR(20) NOT NULL DEFAULT ‘‘,		-- 学生姓名
stuAge INT NOT NULL DEFAULT 0,							-- 学生年龄
stuSex VARCHAR(5) NOT NULL DEFAULT ‘‘				-- 学生性别
)engine myisam charset utf8;

studentscore表:

CREATE TABLE studentScore(
stuId INT NOT NULL,											-- 学生编号
stuChinese DOUBLE NOT NULL DEFAULT 0.0,	-- 学生中文成绩
stuMath DOUBLE NOT NULL DEFAULT 0.0,		-- 学生数学成绩
stuEnglish DOUBLE NOT NULL DEFAULT 0.0,	-- 学生英文成绩
CONSTRAINT fk_student FOREIGN KEY(stuId) REFERENCES student(stuId) -- 设置外键
)engine myisam charset utf8;

JAVA:

对象类:

Student(学生基本信息,com.laolang.domain.student):

package com.laolang.domain;

// TODO: Auto-generated Javadoc
/**
 * The Class Student.
 */
public class Student {

	/**
	 * Instantiates a new student.
	 */
	public Student() {
		super();
	}

	/**
	 * Instantiates a new student.
	 *
	 * @param stuId
	 *            the stu id
	 * @param stuName
	 *            the stu name
	 * @param stuAge
	 *            the stu age
	 * @param stuSex
	 *            the stu sex
	 */
	public Student(int stuId, String stuName, int stuAge, String stuSex) {
		super();
		this.stuId = stuId;
		this.stuName = stuName;
		this.stuAge = stuAge;
		this.stuSex = stuSex;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuAge="
				+ stuAge + ", stuSex=" + stuSex + "]";
	}

	/**
	 * Gets the stu id.
	 *
	 * @return the stu id
	 */
	public int getStuId() {
		return stuId;
	}

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

	/**
	 * Gets the stu name.
	 *
	 * @return the stu name
	 */
	public String getStuName() {
		return stuName;
	}

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

	/**
	 * Gets the stu age.
	 *
	 * @return the stu age
	 */
	public int getStuAge() {
		return stuAge;
	}

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

	/**
	 * Gets the stu sex.
	 *
	 * @return the stu sex
	 */
	public String getStuSex() {
		return stuSex;
	}

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

	/** The stu id. */
	private int stuId;

	/** The stu name. */
	private String stuName;

	/** The stu age. */
	private int stuAge;

	/** The stu sex. */
	private String stuSex;
}

StudentScore(学生成绩,com.laolang.domain.StudentScore):

package com.laolang.domain;

// TODO: Auto-generated Javadoc
/**
 * The Class studentScore.
 */
public class StudentScore {

	/**
	 * Instantiates a new student score.
	 */
	public StudentScore() {
		super();
	}

	/**
	 * Instantiates a new student score.
	 *
	 * @param stuId
	 *            the stu id
	 * @param stuChinese
	 *            the stu chinese
	 * @param stuMath
	 *            the stu math
	 * @param stuEnglish
	 *            the stu english
	 */
	public StudentScore(int stuId, double stuChinese, double stuMath,
			double stuEnglish) {
		super();
		this.stuId = stuId;
		this.stuChinese = stuChinese;
		this.stuMath = stuMath;
		this.stuEnglish = stuEnglish;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "studentScore [stuId=" + stuId + ", stuChinese=" + stuChinese
				+ ", stuMath=" + stuMath + ", stuEnglish=" + stuEnglish + "]";
	}

	/**
	 * Gets the stu id.
	 *
	 * @return the stu id
	 */
	public int getStuId() {
		return stuId;
	}

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

	/**
	 * Gets the stu chinese.
	 *
	 * @return the stu chinese
	 */
	public double getStuChinese() {
		return stuChinese;
	}

	/**
	 * Sets the stu chinese.
	 *
	 * @param stuChinese
	 *            the new stu chinese
	 */
	public void setStuChinese(double stuChinese) {
		this.stuChinese = stuChinese;
	}

	/**
	 * Gets the stu math.
	 *
	 * @return the stu math
	 */
	public double getStuMath() {
		return stuMath;
	}

	/**
	 * Sets the stu math.
	 *
	 * @param stuMath
	 *            the new stu math
	 */
	public void setStuMath(double stuMath) {
		this.stuMath = stuMath;
	}

	/**
	 * Gets the stu english.
	 *
	 * @return the stu english
	 */
	public double getStuEnglish() {
		return stuEnglish;
	}

	/**
	 * Sets the stu english.
	 *
	 * @param stuEnglish
	 *            the new stu english
	 */
	public void setStuEnglish(double stuEnglish) {
		this.stuEnglish = stuEnglish;
	}

	/** The stu id. */
	private int stuId;

	/** The stu chinese. */
	private double stuChinese;

	/** The stu math. */
	private double stuMath;

	/** The stu english. */
	private double stuEnglish;
}

连接封装类

laolangDB(封装驱动连接和关闭,com.laolang.db.laolangDB):

package com.laolang.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

// TODO: Auto-generated Javadoc
/**
 * The Class laolangDB.
 */
public class laolangDB {

	/** The url. */
	private static String URL;

	/** The username. */
	private static String USERNAME;

	/** The userpassword. */
	private static String USERPASSWORD;

	/** The driver. */
	private static String DRIVER;

	/** The rb. */
	private static ResourceBundle rb = ResourceBundle
			.getBundle("com.laolang.db.db-config");

	/**
	 * 使用静态代码块加载驱动
	 */
	static {
		URL = rb.getString("jdbc.url");
		USERNAME = rb.getString("jdbc.username");
		USERPASSWORD = rb.getString("jdbc.userpassword");
		DRIVER = rb.getString("jdbc.driver");

		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 获得链接
	 *
	 * @return the connection
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(URL, USERNAME, USERPASSWORD);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return conn;
	}

	/**
	 * 关闭链接
	 *
	 * @param rs
	 *            the rs
	 * @param ps
	 *            the ps
	 * @param conn
	 *            the conn
	 */
	public static void closeConnection(ResultSet rs, Statement ps,
			Connection conn) {
		try {
			if (null != rs)
				rs.close();
			if (null != ps)
				ps.close();
			if (null != conn)
				conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

属性文件

db-config.properties(连接属性文件,com.laolang.db.db-config.properties):

jdbc.url=jdbc:mysql://localhost:3306/studentmanager_0.1.3

jdbc.username=root

jdbc.userpassword=123456

jdbc.driver=com.mysql.jdbc.Driver

DAO接口:

StudentDao(学生基本信息数据处理,com.laolang.dao.StudentDao):

package com.laolang.dao;

import java.sql.SQLException;
import java.util.List;

import com.laolang.domain.Student;

// TODO: Auto-generated Javadoc
/**
 * The Interface StudentDao.
 */
public interface StudentDao {

	/**
	 * 插入学生基本信息
	 *
	 * @param stu
	 *            the stu
	 * @throws SQLException
	 *             the SQL exception
	 */
	public void insertStudent(Student stu) throws SQLException;

	/**
	 * 删除学生基本信息
	 *
	 * @param stuId
	 *            the stu id
	 * @throws SQLException
	 *             the SQL exception
	 */
	public void deleteStudent(int stuId) throws SQLException;

	/**
	 * 更新学生基本信息
	 *
	 * @param stu
	 *            the stu
	 * @throws SQLException
	 *             the SQL exception
	 */
	public void updateStudent(Student stu) throws SQLException;

	/**
	 * S通过编号查询学生基本信息
	 *
	 * @param stuId
	 *            the stu id
	 * @return the student
	 * @throws SQLException
	 *             the SQL exception
	 */
	public Student selectStudentById(int stuId) throws SQLException;

	/**
	 * 查询所有学生基本信息
	 *
	 * @return the list
	 * @throws SQLException
	 *             the SQL exception
	 */
	public List<Student> selectStudentAll() throws SQLException;
}

StudentScoreDao(学生成绩处理接口,com.laolang.dao.StudentScoreDao):

package com.laolang.dao;

import java.sql.SQLException;

public interface StudentScoreDao {

	public String maxScore(StudentDao stuDao) throws SQLException;

	public String stuScore( StudentDao stuDao, int stuId ) throws SQLException;
}

DAO实现:

StudentDaoImpl(学生基本信息处理实现,com.laolang.dao.impl.StudentDaoImpl):

package com.laolang.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.laolang.dao.StudentDao;
import com.laolang.db.laolangDB;
import com.laolang.domain.Student;

// TODO: Auto-generated Javadoc
/**
 * The Class StudentDaoImpl.
 */
public class StudentDaoImpl implements StudentDao {

	/** The runner. */
	private QueryRunner runner;

	/**
	 * Instantiates a new student dao impl.
	 */
	public StudentDaoImpl() {
		runner = new QueryRunner();
	}

	/*
	 * 插入学生基本信息
	 *
	 * @see com.laolang.dao.StudentDao#insertStudent(com.laolang.domain.Student)
	 */
	@Override
	public void insertStudent(Student stu) throws SQLException {
		String insertStudent = "insert into student( stuId, stuName, stuAge, stuSex ) values(?,?,?,?)";
		runner.update(laolangDB.getConnection(), insertStudent, stu.getStuId(),
				stu.getStuName(), stu.getStuAge(), stu.getStuSex());
	}

	/*
	 * 删除学生基本信息
	 *
	 * @see com.laolang.dao.StudentDao#deleteStudent(int)
	 */
	@Override
	public void deleteStudent(int stuId) throws SQLException {
		String deleteStudentById = "delete from student where stuId = ?";
		runner.update(laolangDB.getConnection(), deleteStudentById, stuId);
	}

	/*
	 * 更新学生基本信息
	 *
	 * @see com.laolang.dao.StudentDao#updateStudent(com.laolang.domain.Student)
	 */
	@Override
	public void updateStudent(Student stu) throws SQLException {
		String updateStudent = "update student set stuName = ?, stuAge = ?, stuSex = ? where stuId = ?";
		runner.update(laolangDB.getConnection(), updateStudent,
				stu.getStuName(), stu.getStuAge(), stu.getStuSex(),
				stu.getStuId());
	}

	/*
	 * 通过编号查询学生基本信息
	 *
	 * @see com.laolang.dao.StudentDao#selectStudentById(int)
	 */
	@Override
	public Student selectStudentById(int stuId) throws SQLException {
		Student stu = null;
		String selectStudentById = "select stuName, stuAge, stuSex from student where stuId = ?";
		stu = runner.query(laolangDB.getConnection(), selectStudentById,
				new BeanHandler<Student>(Student.class), stuId);
		stu.setStuId(stuId);

		return stu;
	}

	/*
	 * 查询所有学生基本信息
	 *
	 * @see com.laolang.dao.StudentDao#selectStudentAll()
	 */
	@Override
	public List<Student> selectStudentAll() throws SQLException {
		String selectStudentAll = "select stuId, stuName, stuAge, stuSex from student";
		List<Student> studentList = runner.query(laolangDB.getConnection(),
				selectStudentAll, new BeanListHandler<Student>(Student.class));

		return studentList;
	}

}

StudentScoreDaoImpl(学生成绩处理实现【未写成】,com.laolang.dao.impl.StudentScoreDaoImpl):

package com.laolang.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.laolang.dao.StudentDao;
import com.laolang.dao.StudentScoreDao;
import com.laolang.db.laolangDB;
import com.laolang.domain.Student;
import com.laolang.domain.StudentScore;

// TODO: Auto-generated Javadoc
/**
 * The Class StudentScoreDaoImpl.
 */
public class StudentScoreDaoImpl implements StudentScoreDao {

	/** The runner. */
	private QueryRunner runner;

	/**
	 * Instantiates a new student score dao impl.
	 */
	public StudentScoreDaoImpl(){
		runner = new QueryRunner();
	}

	/* (non-Javadoc)
	 * @see com.laolang.dao.StudentScoreDao#maxScore(com.laolang.dao.StudentDao)
	 */
	@Override
	public String maxScore(StudentDao stuDao) throws SQLException {

		return null;
	}

	/* 根据学生编号查询学生的编号,姓名,和成绩
	 * @see com.laolang.dao.StudentScoreDao#stuScore(com.laolang.dao.StudentDao, int)
	 */
	@Override
	public String stuScore(StudentDao stuDao, int stuId) throws SQLException {
		Student stu = stuDao.selectStudentById(stuId);//根据编号查询学生基本信息
		String selectStuSore = "select stuChinese, stuMath, stuEnglish from studentscore where stuId = ?";
		StudentScore stuSc = runner.query(laolangDB.getConnection(), selectStuSore,
				new BeanHandler<StudentScore>(StudentScore.class), stuId);
		StringBuffer stuScore = new StringBuffer();
		stuScore.append( stu.getStuId() );
		stuScore.append( "   ");
		stuScore.append( stu.getStuName() );
		stuScore.append( "   ");
		stuScore.append( stuSc.getStuChinese() );
		stuScore.append( "   ");
		stuScore.append( stuSc.getStuMath() );
		stuScore.append( "   ");
		stuScore.append( stuSc.getStuEnglish() );

		return stuScore.toString();
	}

}

启动类:

manager(只使用了一个输出所有学生基本信息,不过其它的,只要我写的,都是可以用的,com.laolang.test.manager)

package com.laolang.manager;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import com.laolang.dao.StudentDao;
import com.laolang.dao.StudentScoreDao;
import com.laolang.dao.impl.StudentDaoImpl;
import com.laolang.dao.impl.StudentScoreDaoImpl;
import com.laolang.db.laolangDB;
import com.laolang.domain.Student;

public class manager {

	public static void main(String[] args) {

		StudentDao sDao = new StudentDaoImpl();
		try {
			List<Student> studentList= sDao.selectStudentAll();

			for( Student stu : studentList ){
				System.out.println(stu.toString());
			}

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

	} 

}

测试效果:

时间: 2024-07-30 15:32:40

学生信息管理系统JAVASE版--第0阶段之CMD版--0.1.3 版的相关文章

学生信息管理系统JAVASE版--1.1.1

现在终于可以写出实用一点的程序了.虽然这个程序的功能非常之简陋,而且还有BUG.不过最起码已经可以使用了. 功能预览和下一步的目标 程序主界面 查询功能: 目前只做了一个表的增.删.改.查.下一步应该就是把功能完善,比如加入错误处理,比如加入成绩部分.完成一个班级内的学生信息管理的功能,应该具有学生的基本信息查询,成绩管理这两个功能 不过有一个问题就是,在表格更新这一部分,每更新一次,就要创建一个tabliModel对象,感觉可以改进.再有就是MVC模式,其实也就接触设计模式.还有就是整成那种可

学生信息管理系统 1.0

学生信息管理系统V1.0 1.实现用户的注册与登录功能. 1.1:注册时检测用户名,如果有重复则需要重新输入,用户信息存入数据库. 1.2:登录时,有验证码验证. 2.实现用户查询学生表的基本信息. 3.用户可以更改.删除学生表信息. 4.系统可以正确显示系统时间. 5.系统正确显示用户权限(功能暂未实现). 因为很多东西还没有学到,只能用最基础的代码实现功能. 1)欢迎页面 1 <%@ page language="java" contentType="text/ht

2018.4.2 学生信息管理系统(集合版) 完整篇

学生信息管理系统(集合版) Student.java package com.lanqiao.demo; /** * 学生类 * * @author qichunlin * */ public class Student { private String name;// 姓名 private double[] score = new double[3];// 三门课程成绩 // get set public String getName() { return name; } public voi

学术休假-学生信息管理系统v1.0

这是第一个学术休假的项目,主体部分在回家时的火车上完成的,隔了几天继续加工,今天终于完成,这是学生管理系统的beta版本,后续还将继续完善. 学生信息包括:学号,姓名,年龄,性别,出生年月,地址,电话,E-mail等.试设计一学生信息管理系统,使之能提供以下功能: (1)系统以菜单方式工作 (2)学生信息录入功能(学生信息用文件保存)---输入 (3)学生信息浏览功能---输出 (4)查询.排序功能 按学号查询 按姓名查询 (5)学生信息的删除与修改(可选项) /* *Copyright (c)

学生信息管理系统v1.0

昨天一个教师朋友找到我,告诉我现在学期末他工作比较忙.需要统计处理很多学生信息,想让我帮他做一个管理系统.实现的功能就是把WPS表格转化成Word文档,将每一个学生的信息都能够分开,并且要根据名字找到对应的文档.(笔者注:原话如此) 和他又扯了好久,分析整理了一下思路,推测他大概就是想这样: 学校发出的成绩表格统计是这样的: -----------------------------程序运行后-------------------------------------> 每个文件的内容是: 大概就

Java_学生信息管理系统——数组版——初次编写

Java练习学生信息管理系统,第一次写,对于类要怎么弄,函数要干嘛,main方法静态不能用非静态的等等很是烦恼了好大一阵. 幸好ArrayList做参数时,是引用传递...C++用&就可以引用传递,Java呢? 1 package Student; 2 3 public class Student 4 { 5 private String stunumber; 6 private String name; 7 private int age; 8 private boolean sex;//ma

学生信息管理系统修改

北京工业大学耿丹学院 c语言设计课程报告   课程设计名称:高级语言程序设计 专业班级:计算机科学与技术1 姓名:吴双 学号:150809201   2016年5月10日 一 对c语言指针链表的体会 ------------------------ 二 修改学生信息管理系统 ------------------------ 三 体会 ------------------------ 一 对c语言指针链表的体会 1.指针 简单来说,指针是一个存储计算机内存地址的变量. 用 int *ptr 这种形

用基本数据结构修改后的学生信息管理系统(增删改查)

package com.xt.student.system; //创建学生类存放信息 public class Student {//声明变量private String stuNo; private String stuName; private String gender; private int age; private int score; //对变量进行封装 public String getStuNo() {return stuNo;} public void setStuNo(St

基于数据库MySQL的简易学生信息管理系统

通过这几天学习Mysql数据库,对其也有了基本的了解,为了加深印象,于是就写了一个最简易的学生信息管理系统. 一:基本要求 1.通过已知用户名和密码进行登录: 2.可以显示菜单: 3.可以随时插入学生信息: 4.可以删除学生信息: 5.可以通过学生姓名或学号显示学生所有信息: 还可以修改学生信息,添加学生表格属性等等,,,这些实现都基本类似上述的(这些不想写了,最简易的学生信息管理系统): 二:步骤 1.写一个sql脚本,包括创建数据库,使用数据库,创建学生信息表格,插入大部分学生信息. stu