一、思维导图
二、步骤
先在SQLServer数据库建立相对应的3个表
再去eclipse里做项目
做项目时我们要把相对应的jar包都导好
下面是本次项目的目录
倒包里面的所有方法
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.entity.Class;
import com.entity.Student;
import com.entity.Teacher;
import com.util.DBHelper;
public class StudentDao {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
/**
* 查询所有学生
* @return
*/
public List<Student> getAll(){
List<Student> list=new ArrayList<Student>();
try {
con=DBHelper.getCon();
String sql="select * from tb_student";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
int sid=rs.getInt(1);
String sname=rs.getString(2);
String tname=rs.getString(3);
String sclass=rs.getString(4);
String sah=rs.getString(5);
Student s=new Student(sid, sname, tname, sclass, sah);
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 查询老师
* @return
*/
public List<Teacher> getTeacher(){
List<Teacher> list=new ArrayList<Teacher>();
try {
con=DBHelper.getCon();
String sql="select * from tb_teacher";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
int tid=rs.getInt(1);
String tname=rs.getString(2);
Teacher s=new Teacher(tid, tname);
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 查询所有班级
* @return
*/
public List<Class> getclass(){
List<Class> list=new ArrayList<Class>();
try {
con=DBHelper.getCon();
String sql="select * from tb_class";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
int cid=rs.getInt(1);
String sclass=rs.getString(2);
Class s=new Class(cid, sclass);
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 增加学生方法
* @param s
* @return
*/
public int add(Student s) {
try {
con=DBHelper.getCon();
String sql="insert into tb_student values(?,?,?,?)";
ps=con.prepareStatement(sql);
ps.setString(1, s.getSname());
ps.setString(2, s.getTname());
ps.setString(3, s.getSclass());
ps.setString(4, s.getsah());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTra