Java +servlet+mysql 學生管理系統

引言:學生管理系統屬於小型的Java web系統,由java web+mysql+servlet實現,採用mvc的設計模式,jdbc編程,具備增刪改查以及模糊查詢,用戶登陸註冊的功能,版本爲eclipse版,如果需要別的版本如idea版,系統架構採用ssm或者springboot的類似Java 或Java web 小型軟件系統,或者定製的,需要本系統源碼的,幫忙遠程部署安裝講解可以加我qq1728608455。


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;
    }

}


希望能幫到大家,需要源碼可以加我QQ1728608455
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章