Java web 学生信息管理系统 之学生信息查询系统的具体实现(附源码)

学生管理系统包括:学生信息登录、注册、删除、添加、查询等功能。最关键的也就是查询系统的实现。至于界面的设计前端事情我们不管。

完整版已经更新啦 点击获取:java web 学生信息管理系统源码+教学视频+完整课设文档Seeの资源站 (chencpp.top)

下面是效果图。

《Java web 学生信息管理系统 之学生信息查询系统的具体实现(附源码)》

界面效果实在一般,不会前端设计,也不想copy,将就看吧。主要是核心实现方法。 代码如下,

代码步骤即设计步骤。

《Java web 学生信息管理系统 之学生信息查询系统的具体实现(附源码)》

java代码严格按照了DAO设计模式进行:

VO类

package cn.web.vo;

public class Student {

   private int id;
   private String name;
   private String adress;
   private float height;
   private float weight;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getAdress() {
	return adress;
}
public void setAdress(String adress) {
	this.adress = adress;
}
public float getHeight() {
	return height;
}
public void setHeight(float height) {
	this.height = height;
}
public float getWeight() {
	return weight;
}
public void setWeight(float weight) {
	this.weight = weight;
}
   
}

DatabseConnection

package cn.web.dbc;

import java.sql.Connection;
import java.sql.DriverManager;

public class DatabaseConnection {
	private static final String DBDRIVER="com.mysql.jdbc.Driver";
	private static final String DBURL="jdbc:mysql://localhost:3306/demo";
	private static final String DBUSER="root";
	private static final String DBPASSWORD="root";
	private static Connection conn=null;
	
	public DatabaseConnection() throws Exception{
		//在构造方法中获得数据库的连接
		Class.forName(DBDRIVER);
		
	this.conn=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
		
	}

	public  Connection getConn() {
		return DatabaseConnection.conn;
	}

	public  void close(Connection conn) throws Exception {
		if(DatabaseConnection.conn!=null) {
			DatabaseConnection.conn.close();
		}
	}
	

}

DAO接口

package cn.web.dao;

import java.util.List;

import cn.web.vo.Student;

public interface IStudentDAO {
	
public List<Student> findAll() throws Exception;

public List<Student> findAll(String keyWord) throws Exception;//模糊查询方法

public Student findById(int id) throws Exception;

public int findCount() throws Exception;

public boolean doCreate(Student s) throws Exception;
public List<Student> findByPage(int currentPage,int pageSize) throws Exception;

}

impl类

package cn.web.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.web.dao.IStudentDAO;

import cn.web.vo.Student;

public class StudentDAOImpl implements IStudentDAO {

	private Connection conn=null;
	private PreparedStatement pstmt=null;
	
	public StudentDAOImpl(Connection databaseConnection) {
		//通过构造方法获得数据库的连接
		this.conn=databaseConnection;
		
	}

	@Override
	public List<Student> findAll() throws Exception {
		
	     List<Student> all=new ArrayList<Student>();
	     ResultSet rs=null;
		 String sql="select * from student";
		 pstmt=conn.prepareStatement(sql);
		 rs =pstmt.executeQuery();
		 while(rs.next()) {
			 
		 Student s=new Student();
			 s.setId(rs.getInt(1));
			 s.setName(rs.getString(2));
			 s.setAdress(rs.getString(3));
			 s.setHeight(rs.getFloat(4));
			 s.setWeight(rs.getFloat(5));
		  all.add(s);
		 }
		 pstmt.close();
		 return all;
		
	}

public List<Student> findAll(String keyWord) throws Exception {
		List<Student> all=new ArrayList<Student>();
	     ResultSet rs=null;
		 String sql="select * from student where name like ? or adress like ?";//通过地址或姓名进行模糊查找
		 pstmt=conn.prepareStatement(sql);
		 pstmt.setString(1, "%"+keyWord+"%");
		 pstmt.setString(2, "%"+keyWord+"%");
		 rs =pstmt.executeQuery();
		 while(rs.next()) {
			  Student s=new Student();
			 s.setId(rs.getInt(1));
			 s.setName(rs.getString(2));
			 s.setAdress(rs.getString(3));
			 s.setHeight(rs.getFloat(4));
			 s.setWeight(rs.getFloat(5));
		  all.add(s);
		 }
		 pstmt.close();
		 return all;
		
	}



	public Student findById(int id) throws Exception {
		Student ss=new Student();
		 ResultSet rs=null;
		 String sql="select * from student where id=?";
		 pstmt=conn.prepareStatement(sql);
		 pstmt.setInt(1, id);
	      rs =pstmt.executeQuery();
	      if(rs.next()) {
		
			 ss.setId(rs.getInt(1));
			 ss.setName(rs.getString(2));
			 ss.setAdress(rs.getString(3));
			 ss.setHeight(rs.getFloat(4));
			 ss.setWeight(rs.getFloat(5));
	}
	      pstmt.close();
	       return ss;
	
	}

	@Override
	public boolean doCreate(Student s) throws Exception {
		boolean flag=false;
		String sql="insert into student (id,name,adress,height,weight) values (?,?,?,?,?)";
		 pstmt=conn.prepareStatement(sql);
		 pstmt.setInt(1,s.getId());
		 pstmt.setString(2,s.getName());
		 pstmt.setString(3,s.getAdress());
		 pstmt.setFloat(4,s.getHeight());
		 pstmt.setFloat(5,s.getWeight());
		 if(this.pstmt.executeUpdate()>0) {
			 flag=true;
		 } 
		 pstmt.close();
		return flag;
	}

	
	@Override
	public List<Student> findByPage(int currentPage, int pageSize) throws Exception{
		 List<Student> all=new ArrayList<Student>();
		 ResultSet rs=null;
		 String sql="select * from student limit ?,?";
		 pstmt=conn.prepareStatement(sql);
		 pstmt.setInt(1, currentPage);
		 pstmt.setInt(2, pageSize);
		 
		 rs=pstmt.executeQuery();
		 while(rs.next()) {
			  Student s=new Student();
			 s.setId(rs.getInt(1));
			 s.setName(rs.getString(2));
			 s.setAdress(rs.getString(3));
			 s.setHeight(rs.getFloat(4));
			 s.setWeight(rs.getFloat(5));
		   all.add(s);
		 }
		 this.pstmt.close();
		 return all;
	}

	@Override
	public int findCount() throws Exception{
		int count =0;
		 String sql="select count(1) from student ";
		 pstmt=conn.prepareStatement(sql);
		 ResultSet rs=null;
		 rs=pstmt.executeQuery();
		 if(rs.next()) {
			count=rs.getInt(1);
		 }
		pstmt.close();
		return count;
	}

}

proxy

package cn.web.proxy;

import java.util.List;

import cn.web.dao.IStudentDAO;
import cn.web.dbc.DatabaseConnection;
import cn.web.impl.StudentDAOImpl;
import cn.web.vo.Student;

public class StudentDAOProxy implements IStudentDAO {
                //代理类 调用前面的前面的类  ps:什么吊事也不用做,调用就完了
	IStudentDAO dao=null;
	DatabaseConnection dbc=null;
	
	public StudentDAOProxy() throws Exception{
		this.dbc=new DatabaseConnection();
		this.dao=new StudentDAOImpl(dbc.getConn());
		
		
	}
	
	public List<Student> findAll() throws Exception {
		List<Student> ss=null;
		ss=this.dao.findAll();
		return ss;
	}

	
	public List<Student> findAll(String keyWord) throws Exception {
		List<Student> ss=null;
		ss=this.dao.findAll(keyWord);
		return ss;
	}

	@Override
	public Student findById(int id) throws Exception {
		Student ss=null;
		ss=this.dao.findById(id);
		return ss;
	}

	@Override
	public boolean doCreate(Student s) throws Exception {
		boolean flag=false;
		if(this.dao.findById(s.getId())!=null) {
		flag=this.dao.doCreate(s);
	}
	 return flag;

}

	@Override
	public List<Student> findByPage(int currentPage, int pageSize) throws Exception {
	   List<Student> all;
		all=dao.findByPage(currentPage, pageSize);
		return all;
	}

	@Override
	public int findCount() throws Exception {
		
		return dao.findCount();
	}
}

DAO工厂

package cn.web.factory;

import cn.web.dao.IStudentDAO;
import cn.web.proxy.StudentDAOProxy;

public class StudentDAOFactory {
	public static IStudentDAO getIStudentDAOInstance() throws Exception {
		return new StudentDAOProxy();
	}

}

 下面是JSP代码

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@page import="java.util.*" %>
<%@page import="cn.web.vo.Student" %>

<html>
<head>
<meta charset="gbk">
<title>学生信息查询</title>
</head>
<body>
<%request.setCharacterEncoding("GBK"); %>
<jsp:useBean id="stud"  scope="request" class="cn.web.factory.StudentDAOFactory"/>
<%
List<Student> all=null;
Student ss=null;

%>
<%int currentPage = 1 ;	// 为当前所在的页,默认在第1页
int lineSize = 8 ;		// 每次显示的记录数
int pageSize = 1 ;		// 表示全部的页数(尾页)
%>
<%    String s=request.getParameter("realPage");
      String keyWord=request.getParameter("query");
if(s!=null){
	  currentPage = Integer.parseInt(s);
}
if("".equals(keyWord)||keyWord==null||"全部".equals(keyWord)||"null".equals(keyWord)){
	all=stud.getIStudentDAOInstance().findByPage((currentPage-1)*8, 8);
}else{
	all=stud.getIStudentDAOInstance().findAll(keyWord);
}


Iterator<Student>it=all.iterator();
pageSize=stud.getIStudentDAOInstance().findCount()%lineSize==0?
		stud.getIStudentDAOInstance().findCount()/lineSize:stud.getIStudentDAOInstance().findCount()/lineSize+1;
  
%>

<script language="javaScript">
 function go(num){
	 document.getElementById("realPage").value = num;
	 document.myform.submit();
}
 
</script>

<center>
<form name="myform" action="display.jsp" method="post">
输入姓名或地址进行查询<input type="text" name="query" width="5" value="<%=keyWord%>">
<input type="button"   value="查询" onclick="go(1)">
<table border="1" width="60%">
<tr>
<td>学号</td>
<td>姓名</td>
<td>地址</td>
<td>身高</td>
<td>体重</td>
</tr>

<%while(it.hasNext()){
	ss=it.next();%>
<tr>
<td><%=ss.getId() %></td>
<td><%=ss.getName() %></td>
<td><%=ss.getAdress() %></td>
<td><%=ss.getHeight() %></td>
<td><%=ss.getWeight() %></td>
</tr>

<%} %>
<tr>
<td><input type="button" value="首页"  onclick="go(<%=1%>)" 
<%=currentPage==1?"DISABLED":""%>></td>
<td><input type="button" value="上一页" onclick="go(<%=currentPage-1%>)"
<%=currentPage==1?"DISABLED":""%>></td>
<td><input type="button" value="下一页" onclick="go(<%=currentPage+1%>)"
<%=currentPage==pageSize?"DISABLED":""%>></td>
<td>跳转到<select name="jump" onchange="go(this.value)">
		<%
			for(int i=1;i<=pageSize;i++){
		%>
			<option value="<%=i%>" <%=i==currentPage?"SELECTED":""%>><%=i%></option>
		<%
			}
		%>
		</select>页</td>



<td><input type="button" value="尾页" onclick="go(<%=pageSize%>)"
<%=currentPage==pageSize?"DISABLED":""%>></td>

</tr>
</table>
<input type="hidden" name="realPage" value="1">
</form>
</center>

</body>
</html>

 代码就这么多。按顺序看下来慢慢实现吧,不难理解,有疑问  添加QQ:  3492625357

本来准备今晚写完整个管理系统,但是太困了,先设计完成了查询系统。由于DAO设计模式和代码耦合度低,便于日后完善设计,往后我会把完善后完整学生管理系统发出来。

    原文作者:陈子青 :See
    原文地址: https://blog.csdn.net/weixin_40582034/article/details/91215967
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章