Java +servlet+mysql 学生管理系统
学生管理系统属于小型的Java web系统,由java web+mysql+servlet实现,采用mvc的设计模式,jdbc编程,具备增删改查以及模糊查询,用户登陆注册的功能
引言:学生管理系统属于小型的Java web系统,由java web+mysql+servlet实现,采用mvc的设计模式,jdbc编程,具备增删改查以及模糊查询,用户登陆注册的功能
1、效果图
2 因为代码较多,仅展示部分重要代码
package service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import dao.StuDao;
import model.Student;
import utils.DBUtils;
public class StuService implements StuDao {
Connection con=null;/
@Override/
public List<Student> selectAll() {
List<Student> list=new ArrayList<Student>();
try {
con=DBUtils.getConnection();
String sql="select * from student";
PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
while(rs.next()){//止
Student stu=new Student();
stu.setsNo(rs.getInt("sNo"));
stu.setsName(rs.getString("sName"));
stu.setsAge(rs.getInt("sAge"));
stu.setsAddress(rs.getString("sAddress"));
stu.setSchool(rs.getString("school"));
list.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
DBUtils.closeConnection(con);
}
return list;
}
@Override
public int delete(int sNo) {
try {
con=DBUtils.getConnection();
String sql="delete from student where sNo=?";
PreparedStatement pst=con.prepareStatement(sql);
pst.setInt(1,sNo);
pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally{
DBUtils.closeConnection(con);
}
return 1;
}
@Override
public boolean insert(Student stu) {
String sql="insert into student(sNo,sName,sAge,sAddress,school) values(?,?,?,?,?)";
try {
con=DBUtils.getConnection();
PreparedStatement pst=con.prepareStatement(sql);
pst=con.prepareStatement(sql);
pst.setInt(1, stu.getsNo());
pst.setString(2, stu.getsName());
pst.setInt(3,stu.getsAge());
pst.setString(4, stu.getsAddress());
pst.setString(5, stu.getSchool());
pst.execute();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally{
DBUtils.closeConnection(con);
}
}
@Override
public boolean update(Student stu, int sNo) {
//TODO Auto-generated method stub
try{
con=DBUtils.getConnection();
String sql="update student set sName=?,sAge=?,sAddress=?,school=? where sNo=?";
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,stu.getsName());
pst.setInt(2,stu.getsAge());
pst.setString(3,stu.getsAddress());
pst.setString(4, stu.getSchool());
pst.setInt(5,sNo);
pst.execute();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
DBUtils.closeConnection(con);
}
}
@Override
public List<Student> findBysName(String sName) {
Connection con=null;
List<Student> list =new ArrayList<Student>();
try{
con=DBUtils.getConnection();
String sql="select * from student where sName like ?";
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, "%"+sName+"%");
ResultSet rs=pst.executeQuery();
while(rs.next()) {
Student stu =new Student();
stu.setsNo(rs.getInt("sNo"));
stu.setsName(rs.getString("sName"));
stu.setsAge(rs.getInt("sAge"));
stu.setsAddress(rs.getString("sAddress"));
stu.setSchool(rs.getString("school"));
list.add(stu);
}
return list;
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
DBUtils.closeConnection(con);
}
}
@Override
public Student findBysNo(int sNo) {
try{
con=DBUtils.getConnection();
String sql="select * from student where sNo=?";
PreparedStatement pst=con.prepareStatement(sql);
pst.setInt(1,sNo);
ResultSet rs=pst.executeQuery();
if(rs.next()){
Student stu=new Student();
stu.setsNo(rs.getInt("sNo"));
stu.setsName(rs.getString("sName"));
stu.setsAge(rs.getInt("sAge"));
stu.setSchool(rs.getString("school"));
stu.setsAddress(rs.getString("sAddress"));
return stu;
}else{
return null;
}
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
DBUtils.closeConnection(con);
}
}
public static void main(String[] args) {
StuService s=new StuService();
/*List<Student> list=s.selectAll();
for(Student st:list) {
System.out.println(st.getsAddress());
}*/
Student stu=new Student();
//stu.setsNo(sNo);
/*stu.setsName("");
stu.setsAge(21);
stu.setsAddress("");
stu.setSchool("xxx");
boolean b=s.update(stu, 1211);*/
//System.out.println(b);
List<Student> list=s.findBysName("xxx");
for(Student st:list) {
System.out.println(st.getsAddress());
}
}
@Override
public Student ByAddress(String address) {
// TODO Auto-generated method stub
Connection c=null;
try {
c=DBUtils.getConnection();
String sql="select * from student where address like ?";
}catch(Exception e) {
}
return null;
}
}
更多推荐
所有评论(0)