JAVAEE案例: 基于javaweb、mysql的 学生信息管理系统

此案例是基于JavaEE、mysql技术的学生信息管理系统。
为了先对案例有个清楚的认识,这里先贴上运行结果截图:
1、索引页面,进入系统。
索引页面
2、主页面:
主页面
3、点击添加:
这里写图片描述
4、删除李琦琦一栏:
这里写图片描述
5、修改马衍硕:
这里写图片描述

一、对数据库的封装:

在数据库库部分,通过Java代码对数据库操作进行封装,其中涉及到很多技术和技巧,请持续关注我的博客,后续会有详细介绍。

对数据库的封装代码:
(1)这里是定义数据库操作元素的结构,操作元素有三个属性,即deptno、dname、loc,分别对应学生信息的学号、姓名、家庭住址。

Dept.java

package com.neusoft.dao;

//实体类
public class Dept {
private int deptno;//
private String dname;//
private String loc;//
private String value1;//
private String value2;//
public int getDeptno() {
	return deptno;
}
public void setDeptno(int deptno) {
	this.deptno = deptno;
}
public String getDname() {
	return dname;
}
public void setDname(String dname) {
	this.dname = dname;
}
public String getLoc() {
	return loc;
}
public void setLoc(String loc) {
	this.loc = loc;
}
public String getValue1() {
	return value1;
}
public void setValue1(String value1) {
	this.value1 = value1;
}
public String getValue2() {
	return value2;
}
public void setValue2(String value2) {
	this.value2 = value2;
}



}

(2)连接数据库:

DeUtils.java

package com.neusoft.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbUtils {
private DbUtils(){}
private static final String url="jdbc:mysql://localhost:3306/smartstyle";;
private static final  String user="root";
private static  final String password="857289";
static
{
	//1.创建驱动类对象
	//new oracle.jdbc.driver.OracleDriver();
	try {
		//Class.forName("oracle.jdbc.driver.OracleDriver");
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
public static  Connection getConnection()
{
	Connection conn=null;	
	try {
		conn=DriverManager.getConnection(url, user, password);
		System.out.println("Ok");
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return conn;
	}
public static void closeConnection(Connection conn)
{
	try {
		if(conn!=null)
		{
		conn.close();
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
public static void closePreparedStatement(PreparedStatement pstmt)
{
	try {
		if(pstmt!=null)
		{
		pstmt.close();
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
public static void closeResultSet(ResultSet rs)
{
	//4.关闭数据库
			try {
				if(rs!=null)
				{
				rs.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}


public static void beginTransaction(Connection conn) {
	try {
		conn.setAutoCommit(false);
	} catch (SQLException ex) {
		ex.printStackTrace();
	}
}

public static void commit(Connection conn) {
	try {
		conn.commit();
		conn.setAutoCommit(true);
	} catch (SQLException ex) {
		ex.printStackTrace();
	}
}

public static void rollback(Connection conn) {
	try {
		conn.rollback();
		conn.setAutoCommit(true);
	} catch (SQLException ex) {
		ex.printStackTrace();
	}
}
public static void main(String[] ars)
{
	 getConnection();
	}
}

(3)对数据库操作的封装:
这里的接口定义了对数据库操作的方法:
DeptDao.java

package com.neusoft.dao;

import java.util.List;


public interface DeptDao {
void insertDept(Dept dept);
void updateDept(Dept dept);
void deleteDept(int deptno);
List<Dept>  getDepts();
Dept getDeptByDeptno(int deptno);

}

这里实现了对数据库操作的封装:
在建立数据库连接的基础上,通过向数据库发送要执行的SQL语句,达到操作数据库的目的。

DeptDaoImpl.java

package com.neusoft.dao;

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


public class DeptDaoImpl implements DeptDao {

	@Override
	public void insertDept(Dept dept) {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstmt=null;
				try {
					conn=DbUtils.getConnection();
					//3.操作数据库					
					String sql="insert into dept(deptno,dname,loc) values(?,?,?)";					
					pstmt=conn.prepareStatement(sql);
					pstmt.setInt(1, dept.getDeptno());
					pstmt.setString(2, dept.getDname());
					pstmt.setString(3,dept.getLoc());
					
					pstmt.executeUpdate();
					
				}  catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally
				{
			
				//4.关闭数据库		
					DbUtils.closePreparedStatement(pstmt);
					DbUtils.closeConnection(conn);
				}
			
				
	}

	@Override
	public void updateDept(Dept dept) {
		// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement pstmt=null;


try {
	conn=DbUtils.getConnection();
	String sql="update dept set dname=?,loc=? where deptno=?";
	pstmt=conn.prepareStatement(sql);
	pstmt.setString(1, dept.getDname());
	pstmt.setString(2, dept.getLoc());
	pstmt.setInt(3, dept.getDeptno());
	
	pstmt.executeUpdate();
	
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}finally
{
	//4.关闭数据库		
	DbUtils.closePreparedStatement(pstmt);
	DbUtils.closeConnection(conn);
	}

	}

	@Override
	public void deleteDept(int deptno) {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstmt=null;
		
		try {
			conn=DbUtils.getConnection();
			String sql="delete from dept where deptno=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, deptno);
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			//4.关闭数据库		
			DbUtils.closePreparedStatement(pstmt);
			DbUtils.closeConnection(conn);
		}
		
	}

	@Override
	public List<Dept> getDepts() {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<Dept>  depts=new ArrayList<Dept>();
		try {
			conn=DbUtils.getConnection();
			//3.操作数据库			
			String sql="select * from dept";			
			pstmt=conn.prepareStatement(sql);
			
			rs=pstmt.executeQuery();
			
			while(rs.next())
			{
				Dept dept=new Dept();
				dept.setDeptno(rs.getInt("DEPTNO"));//
				dept.setDname(rs.getString("DNAME"));
				dept.setLoc(rs.getString("LOC"));
				depts.add(dept);
				
			}
			
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
	    DbUtils.closeResultSet(rs);
		DbUtils.closePreparedStatement(pstmt);
		DbUtils.closeConnection(conn);
		
		}
		return depts;
	}

	@Override
	public Dept getDeptByDeptno(int deptno) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		Dept  dept=new Dept();
		try {
			conn=DbUtils.getConnection();
			//3.操作数据库			
			String sql="select * from dept where deptno=?";			
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, deptno);
			rs=pstmt.executeQuery();
			
			if(rs.next())
			{
				dept.setDeptno(rs.getInt("DEPTNO"));
				dept.setDname(rs.getString("DNAME"));
				dept.setLoc(rs.getString("LOC"));
			}
			
			
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
	    DbUtils.closeResultSet(rs);
		DbUtils.closePreparedStatement(pstmt);
		DbUtils.closeConnection(conn);
		
		}
		return dept;
	}

}

(4)为了代码的简洁,再封装:
DeptService.java

package com.neusoft.service;

import java.util.List;

import com.neusoft.dao.Dept;

public interface DeptService {
	void insertDept(Dept dept);
	void updateDept(Dept dept);
	void deleteDept(int deptno);
	List<Dept>  getDepts();
	Dept getDeptByDeptno(int deptno);
}

DeptServiceImpl.java

package com.neusoft.service;

import java.util.List;

import com.neusoft.dao.Dept;
import com.neusoft.dao.DeptDao;
import com.neusoft.dao.DeptDaoImpl;

public class DeptServiceImpl implements DeptService {

	@Override
	public void insertDept(Dept dept) {
		// TODO Auto-generated method stub
		DeptDao deptDao=new DeptDaoImpl();
		deptDao.insertDept(dept);
	}

	@Override
	public void updateDept(Dept dept) {
		// TODO Auto-generated method stub
		DeptDao deptDao=new DeptDaoImpl();
	deptDao.updateDept(dept);
	}

	@Override
	public void deleteDept(int deptno) {
		// TODO Auto-generated method stub
		DeptDao deptDao=new DeptDaoImpl();
		deptDao.deleteDept(deptno);
	}

	@Override
	public List<Dept> getDepts() {
		DeptDao deptDao=new DeptDaoImpl();
		
		return deptDao.getDepts();
	}

	@Override
	public Dept getDeptByDeptno(int deptno) {
		// TODO Auto-generated method stub
		DeptDao deptDao=new DeptDaoImpl();
		return deptDao.getDeptByDeptno(deptno);
	}

	       

}

贴个代码分布图
这里写图片描述

这样,mysql的连接封装基本完成,接下来是web部分。

二、Web部分:
1、各个页面的JSP代码:
(1)index页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="DeptServlet?service=all">学生信息管理系统</a>
</body>
</html>

(2)主页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.List,com.neusoft.dao.Dept"%>
       <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=basePath %>dept/insert.jsp" method="post">

<table border="1" width="80%" align="center" cellspacing="0">
<caption>学生信息表</caption>
<tr><th>学生学号</th><th>学生姓名</th><th>家庭地址</th><th>操作</th><tr>
<%List<Dept>  depts=(List<Dept>)request.getAttribute("depts");
if(depts==null)
{
%>
<tr><td colspan="3">没有符合条件的数据</td></tr>
<%}else{ 
	for(Dept dept:depts)
	{
 
%>
<tr><td><%=dept.getDeptno() %></td>
<td><%=dept.getDname() %></td>
<td><%=dept.getLoc()  %></td>
<td><a href="DeptServlet?service=modify&deptno=<%=dept.getDeptno()%>">修改</a>
<a href="DeptServlet?service=delete&deptno=<%=dept.getDeptno() %>" >删除</a></td>
<tr>

<%}} %>
</table>
<input type="submit" value="添加" >
</form>
</body>
</html>

(3)修改页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.neusoft.dao.Dept"%>
       <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=basePath %>DeptServlet?service=update" method="post">

<center>
<h1>修改信息</h1>
<% Dept dept=(Dept)request.getAttribute("dept"); %>
学生学号:<input type="text" name="deptno" value="<%=dept.getDeptno()   %>"><br>
学生姓名:<input type="text" name="dname" value="<%=dept.getDname() %>"><br>
家庭地址:<input type="text" name="loc" value="<%=dept.getLoc()%>  "><br>
<input type="submit" value="修改信息">

</body>
</html>

2、服务器端:
对于客户端不同的HTTP请求对数据库进行不同的操作,并跳转页面。

package com.neusoft.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.dao.Dept;
import com.neusoft.service.DeptService;
import com.neusoft.service.DeptServiceImpl;

/**
 * Servlet implementation class DeptServlet
 */
@WebServlet("/DeptServlet")
public class DeptServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public DeptServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String service=request.getParameter("service");
		if("all".equals(service))
		{
			DeptService deptService=new DeptServiceImpl();
			List<Dept> depts=deptService.getDepts();
			
			request.setAttribute("depts", depts);
			
			request.getRequestDispatcher("dept/main.jsp").forward(request, response);
		}else if("add".equals(service))
		{
			//处理请求
			int deptno=Integer.parseInt(request.getParameter("deptno"));
			String dname=request.getParameter("dname");
			String loc=request.getParameter("loc");
			Dept dept=new Dept();
			dept.setDeptno(deptno);
			dept.setDname(dname);
			dept.setLoc(loc);
			
			//调用相应的业务逻辑
			DeptService deptService=new DeptServiceImpl();
			deptService.insertDept(dept);
			
			//找到某个视图响应回去
			request.getRequestDispatcher("DeptServlet?service=all").forward(request, response);
		}else if("update".equals(service))
		{
			//处理请求
			int deptno=Integer.parseInt(request.getParameter("deptno"));
			String dname=request.getParameter("dname");
			String loc=request.getParameter("loc");
			Dept dept=new Dept();
			dept.setDeptno(deptno);
			dept.setDname(dname);
			dept.setLoc(loc);
			
			//调用相应的业务逻辑
			DeptService deptService=new DeptServiceImpl();
			deptService.updateDept(dept);
			
			//找到某个视图响应回去
		
			request.getRequestDispatcher("DeptServlet?service=all").forward(request, response);
			
		}
		else if("delete".equals(service))
		{
			
			int deptno=Integer.parseInt(request.getParameter("deptno"));
			//调用相应的业务逻辑
			DeptService deptService=new DeptServiceImpl();
			deptService.deleteDept(deptno);
			
			//找到某个视图响应回去
			request.getRequestDispatcher("DeptServlet?service=all").forward(request, response);
			
		}else if("modify".equals(service))
		{
			int deptno=Integer.parseInt(request.getParameter("deptno"));
			//调用相应的业务逻辑
			DeptService deptService=new DeptServiceImpl();
		Dept dept=	deptService.getDeptByDeptno(deptno);
		
		request.setAttribute("dept", dept);
			//找到某个视图响应回去
			request.getRequestDispatcher("dept/update.jsp").forward(request, response);
			
		}
	}

}

完整项目我已上传至我的资源

https://download.csdn.net/download/mmayanshuo/10804768

没有更多推荐了,返回首页