首页 > 代码库 > 学生信息管理系统JAVASE版--第0阶段之CMD版--0.1.3 版

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


测试效果:






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