学生信息管理系统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();
		}
		
		
	} 
	
	
}


测试效果:






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、 创建类实现基本对象和他们关系的管理,包括学生、教学班、课程、成绩、教师等。使用集合框架存储所有对象。学生至少包含学号、姓名、性别等信息。教学班至少包含教师、课程名字、总人数、教学班号、开课学期等信息。课程至少包含课程编号、课程名字等信息。教师至少包含教师编号、姓名等信息。 2、随机生成学生,数量不少于100。一个教学班有一个教师上一门课程,教学班的学生数量不少于20。课程数量不少于3门。教师数量不少于6个。一门课至少有两个老师上课。每个学生选择至少选择3门课程。一个学生在一个教学班上一门课,考试后取得一个成绩。一门课的成绩构成有4部分构成,包括平时成绩、期中考试、实验成绩和期末考试成绩,然后计算出总成绩。成绩随机生成,均为整数。 3、分阶段模拟教学过程。例如执行一个命令,可以生成一个教学班的所有学生的平时成绩。第一步,生成初始化数据,包括教师,学生、课程,教学班等;第二步,学生选课,随机进行,为每门课程的教学班安排学生。第三步,获得平时成绩,获得期中成绩,获得实验成绩,获得期末成绩,最后计算总成绩。 4、能够显示一个教学班级的学生,可以根据学号排序,可以根据成绩排序。可以通过名字查询成绩,可以按照各科成绩和总成绩进行排名显示,可以统计各课程学生成绩的分数段分布。 5、可以实现自己的扩展功能。注意类和类之间的关系。充分利用继承,多态等特性,使用上抽象类,接口,泛型,内部类等设计元素

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值