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

此案例是基于JavaEE、mysql技术的学生信息管理系统。
为了先对案例有个清楚的认识,这里先贴上运行结果截图:
1、索引页面,进入系统。
JAVAEE案例: 基于javaweb、mysql的 学生信息管理系统_第1张图片
2、主页面:
JAVAEE案例: 基于javaweb、mysql的 学生信息管理系统_第2张图片
3、点击添加:
JAVAEE案例: 基于javaweb、mysql的 学生信息管理系统_第3张图片
4、删除李琦琦一栏:
JAVAEE案例: 基于javaweb、mysql的 学生信息管理系统_第4张图片
5、修改马衍硕:
JAVAEE案例: 基于javaweb、mysql的 学生信息管理系统_第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  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 getDepts() {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List  depts=new ArrayList();
		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  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 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);
	}

	       

}

贴个代码分布图
JAVAEE案例: 基于javaweb、mysql的 学生信息管理系统_第6张图片

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

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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


学生信息管理系统


(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+"/";

%>




Insert title here


<%List depts=(List)request.getAttribute("depts"); if(depts==null) { %> <%}else{ for(Dept dept:depts) { %> <%}} %>
学生信息表
学生学号学生姓名家庭地址操作
没有符合条件的数据
<%=dept.getDeptno() %> <%=dept.getDname() %> <%=dept.getLoc() %> 修改 删除

(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+"/";

%>




Insert title here


修改信息

<% Dept dept=(Dept)request.getAttribute("dept"); %> 学生学号:
学生姓名:
家庭地址:

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

你可能感兴趣的:(Java)