目录
一、 需求分析
1)建表
需要建立四张表,分别为班级表、学生表、教师表、爱好表
班级表字段:班级编号、班级名称
学生表字段:学生id、学生姓名、班级编号、教员编号、爱好id
教师表字段:教员id、教员名称、所教班级
爱好表字段:爱好id、爱好名称
2)查询
可以多条件查询:根据班级、教师、爱好进行查询
3)新增
4)修改
修改要注意值得回显
5)分页
显示第几页,共几页,总记录多少条数据,可以选定页面跳转
6)删除
二、数据库
1)新建数据库以及表
2)添加值,以便测试
三、实体类
1)根据数据库的表建立所需实体类
2) 给实体类定义变量以及所需构造方法
例如:
package com.mgy.entity;
import java.io.Serializable;
import java.util.List;
/**
* 实体类:学生类
* @author Administrator
*
*/
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private int sid;//学生编号
private String sname;//学生名称
private Cla cl;//班级对象
private Theaher th;//教员对象
private List<Hobby> hy;//爱好集合
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Cla getCl() {
return cl;
}
public void setCl(Cla cl) {
this.cl = cl;
}
public Theaher getTh() {
return th;
}
public void setTh(Theaher th) {
this.th = th;
}
public List<Hobby> getHy() {
return hy;
}
public void setHy(List<Hobby> hy) {
this.hy = hy;
}
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int sid, String sname, Cla cl, Theaher th, List<Hobby> hy) {
this.sid = sid;
this.sname = sname;
this.cl = cl;
this.th = th;
this.hy = hy;
}
public Student(String sname, Cla cl, Theaher th, List<Hobby> hy) {
this.sname = sname;
this.cl = cl;
this.th = th;
this.hy = hy;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", cl=" + cl + ", th=" + th + ", hy=" + hy + "]";
}
}
四、mvc模式
1)定义数据库辅助类
package com.mgy.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 数据库辅助类
* 提供了一组获得或关闭数据库对象的方法
*
*/
public class DBHelper {
private static String driver;
private static String url;
private static String user;
private static String password;
static {// 静态块执行一次,加载 驱动一次
try {
InputStream is = DBHelper.class
.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url")