Java Web实现信息管理
前言:
大二上学期刚学完Oracle,老师就要求用Java语言操作Oracle,当时真的是懵了。回顾自己前两年的生活,大一上学期学习C语言,下学期学习Java,当时教课老师是学硬件的。课上讲一些理论性的问题,剩余时间让我们比这课本敲代码,感觉自己学的Java太片面了,而且那个时候也没有感到Java很重要,直到现在真的后悔之前没有好好学习Java…现在想想,所谓的连接Oracle和MySQL区别也不是很大,JDBC连接数据库的步骤都是固定的,SQL语句也差不多,之间的区别就在,数据库所属的公司不同,驱动jar包不同,具体的驱动类不同,或许,想到更细致一点,数据库的用户名和密码不同。现在,JSP,servlet,三层架构,MVC框架,前端技术( HTML CSS JS …)涉及的知识范围越广,理解起来其实也更加的容易。
Oracle数据库:
- 驱动jar包(从官网进行下载):ojdbc-x.jar
- 具体驱动类:oracle.jdbc.OracleDriver
MySQL数据库
- 驱动jar包:mysql-connection-java-x.jar
- 具体驱动类:com.mysql.jdbc.Driver
下面是具体的项目管理:
主页面:显示全部学生信息index.jsp
查询操作:
更新修改操作:
增加操作:
点击删除(超链接)后,数据库中对应的数据成功删除(这里不再展示)
实现代码:
StudentDao.java
package org.student.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;import org.student.entity.Student;/*** @author 11441*数据访问层:原子性,具体的 增删改查*/
public class StudentDao {private final String URL = "jdbc:mysql://localhost:3306/test1";private final String USERNAME = "root";private final String PASSWOED = "root";//判断学生存在public boolean isExist(int sno) {return queryStudentBySno(sno)==null ? false : true;}//增加public boolean addStudent(Student student) {//封装到数据类Connection connection = null;PreparedStatement pstmt = null;try {Class.forName("com.mysql.jdbc.Driver");connection = DriverManager.getConnection(URL, USERNAME, PASSWOED);String sql = "insert into student values (?,?,?,?)";pstmt = connection.prepareStatement(sql);pstmt.setInt(1, student.getSno());pstmt.setString(2, student.getSname());pstmt.setInt(3, student.getSage());pstmt.setString(4, student.getSaddress());int count = pstmt.executeUpdate();if(count > 0) {return true;}else {return false;}} catch (ClassNotFoundException e) {e.printStackTrace();return false;} catch (SQLException e) {e.printStackTrace();return false;} catch (Exception e) {e.printStackTrace();return false;} finally {try {if(pstmt != null) pstmt.close();if(connection != null) connection.close();} catch (SQLException e) {e.printStackTrace();}}}//根据学号修改学生:根据sno找到要修改的人public boolean updateStudentBySno(int sno,Student student) {Connection connection = null;PreparedStatement pstmt = null;try {Class.forName("com.mysql.jdbc.Driver");connection = DriverManager.getConnection(URL, USERNAME, PASSWOED);String sql = "update student set sname = ?,sage=?,saddress=? where sno=?";pstmt = connection.prepareStatement(sql);pstmt.setString(1, student.getSname());pstmt.setInt(2, student.getSage());pstmt.setString(3, student.getSaddress());pstmt.setInt(4, sno);int count = pstmt.executeUpdate();if(count > 0) {return true;}else {return false;}} catch (ClassNotFoundException e) {e.printStackTrace();return false;} catch (SQLException e) {e.printStackTrace();return false;} catch (Exception e) {e.printStackTrace();return false;} finally {try {if(pstmt != null) pstmt.close();if(connection != null) connection.close();} catch (SQLException e) {e.printStackTrace();}}}//根据学号删除学生public boolean deleteStudentBySno(int sno) {//封装到数据类Connection connection = null;PreparedStatement pstmt = null;try {Class.forName("com.mysql.jdbc.Driver");connection = DriverManager.getConnection(URL, USERNAME, PASSWOED);String sql = "delete from student where sno = ?";pstmt = connection.prepareStatement(sql);pstmt.setInt(1, sno);int count = pstmt.executeUpdate();if(count > 0) {return true;}else {return false;}} catch (ClassNotFoundException e) {e.printStackTrace();return false;} catch (SQLException e) {e.printStackTrace();return false;} catch (Exception e) {e.printStackTrace();return false;} finally {try {if(pstmt != null) pstmt.close();if(connection != null) connection.close();} catch (SQLException e) {e.printStackTrace();}}}//根据学号查询学生信息public Student queryStudentBySno(int sno) {Student student = null;Connection connection = null;PreparedStatement pstmt = null;ResultSet rs = null;try {Class.forName("com.mysql.jdbc.Driver");connection = DriverManager.getConnection(URL, USERNAME, PASSWOED);String sql = "select * from student where sno = ?";pstmt = connection.prepareStatement(sql);pstmt.setInt(1, sno);rs = pstmt.executeQuery();if(rs.next()) {int no = rs.getInt("sno");String name = rs.getString("sname");int age = rs.getInt("sage");String address = rs.getString("saddress");student = new Student(no,name,age,address);}return student;} catch (ClassNotFoundException e) {e.printStackTrace();return null;} catch (SQLException e) {e.printStackTrace();return null;} catch (Exception e) {e.printStackTrace();return null;} finally {try {if(rs != null) rs.close();if(pstmt != null) pstmt.close();if(connection != null) connection.close();} catch (SQLException e) {e.printStackTrace();}}}//查询全部学生(很多学生)public List<Student> queryAllStudent() {List<Student> students = new ArrayList<>();Student student = null;Connection connection = null;PreparedStatement pstmt = null;ResultSet rs = null;try {Class.forName("com.mysql.jdbc.Driver");connection = DriverManager.getConnection(URL, USERNAME, PASSWOED);String sql = "select * from student";pstmt = connection.prepareStatement(sql);rs = pstmt.executeQuery();while(rs.next()) {int no = rs.getInt("sno");String name = rs.getString("sname");int age = rs.getInt("sage");String address = rs.getString("saddress");student = new Student(no,name,age,address);students.add(student);}return students;} catch (ClassNotFoundException e) {e.printStackTrace();return null;} catch (SQLException e) {e.printStackTrace();return null;} catch (Exception e) {e.printStackTrace();return null;} finally {try {if(rs != null) rs.close();if(pstmt != null) pstmt.close();if(connection != null) connection.close();} catch (SQLException e) {e.printStackTrace();}}}
}
Student.java
package org.student.entity;/*** @author 11441*student实体类*/
public class Student {private int sno;private String sname;private int sage;private String saddress;public Student(String sname, int sage, String saddress) {this.sname = sname;this.sage = sage;this.saddress = saddress;}public Student(int sno, String sname, int sage, String saddress) {this.sno = sno;this.sname = sname;this.sage = sage;this.saddress = saddress;}public Student() {}public Student(int sno, String sname, int sage) {this.sno = sno;this.sname = sname;this.sage = sage;}public Student(int sage, String saddress) {this.sage = sage;this.saddress = saddress;}public int getSno() {return sno;}public void setSno(int sno) {this.sno = sno;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public int getSage() {return sage;}public void setSage(int sage) {this.sage = sage;}public String getSaddress() {return saddress;}public void setSaddress(String saddress) {this.saddress = saddress;}@Overridepublic String toString() {return this.getSno()+"-"+this.getSname()+"-"+this.getSage()+"-"+this.getSaddress();}
}
StudentService.java
package org.student.service;import java.util.List;import org.student.dao.StudentDao;
import org.student.entity.Student;/*** @author 11441*业务逻辑层,逻辑性的增删改查,(增:查+增)到Dao层进行的组装*/
public class StudentService {StudentDao studentDao = new StudentDao();public boolean addStudent(Student student) {if(!studentDao.isExist(student.getSno())) {//学生不存在,增加该学生studentDao.addStudent(student);return true;}else {System.out.println("该学生已存在");return false;}}public boolean deleteStudentBySno(int sno) {if(studentDao.isExist(sno)) {return studentDao.deleteStudentBySno(sno);}else {return false;}}public boolean updateStudentBySno(int sno,Student student) {if(studentDao.isExist(sno)) {return studentDao.updateStudentBySno(sno,student);}else {return false;}}//根据学号查询学生public Student queryStudentBySno(int sno) {return studentDao.queryStudentBySno(sno);}//查询所有学生public List<Student> queryAllStudents() {return studentDao.queryAllStudent();}
}
QueryAllStudentdServlet.java
(关于Servlet还有AddStudentServlet,DeleteStudentServlet,QueryStudentBySnoServlet,UpdateStudentServlet,这里就不再展示)
package org.student.servlet;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.student.entity.Student;
import org.student.service.StudentService;public class QueryAllStudentdServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");StudentService service = new StudentService();List<Student> students = service.queryAllStudents();System.out.println(students);request.setAttribute("students", students);//因为request域中有数据,因此需要通过请求转发的方式跳转(重定向会丢失request域)request.getRequestDispatcher("index.jsp").forward(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
WebContent中的jsp文件:
index.jsp
<%@page import="org.student.service.StudentService"%>
<%@page import="org.student.dao.StudentDao"%>
<%@page import="java.util.List"%>
<%@page import="org.student.entity.Student"%>
<%@page import="org.student.servlet.QueryAllStudentdServlet"%>
<%@ 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>学生信息列表</title>
<style type="text/css">
#formstyle
{margin:100px 500px;
}
table{width: 80%;height: 100%;
}
</style>
</head>
<body background="photo/海浪.jpg">
<div id="formstyle"><table border="#008C8C 3px solid"><caption>学生信息管理系统</caption><tr align="left" ><th>学号</th><th>姓名</th><th>年龄</th><th>操作</th></tr><!-- 学生信息有几行,要通过循环 --><%/* 获取request域中的数据 */List<Student> students = (List<Student>)request.getAttribute("students");for(Student student : students){%><tr><td><a href="QueryStudentBySnoServlet?sno=<%=student.getSno() %>"><%=student.getSno()%></a></td><td><%=student.getSname()%></td><td><%=student.getSage()%></td><td><a href="DeleteStudentServlet?sno=<%=student.getSno()%>">删除</a></td> </tr><%}%></table><a href="add.jsp">新增</a>
</div>
</body>
</html>
add.jsp
<%@page import="org.student.entity.Student"%>
<%@ 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><%Student student = (Student)request.getAttribute("student");%> <!--通过表单展示学生 --><form action="UpdateStudentServlet">学号:<input type="text" name="sno" value="<%=student.getSno() %>" readonly="readonly" /><br/>姓名:<input type="text" name="sname" value="<%=student.getSname() %>"/><br/>年龄:<input type="text" name="sage" value="<%=student.getSage() %>"/><br/>地址:<input type="text" name="saddress" value="<%=student.getSaddress() %>"/><br/><input type="submit" value=" 修改" /><br/><a href="QueryAllStudentdServlet">返回</a></form>
</body>
</html>
studentInfo.jsp
<%@page import="org.student.entity.Student"%>
<%@ 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><%Student student = (Student)request.getAttribute("student");%> <!--通过表单展示学生 --><form action="UpdateStudentServlet">学号:<input type="text" name="sno" value="<%=student.getSno() %>" readonly="readonly" /><br/>姓名:<input type="text" name="sname" value="<%=student.getSname() %>"/><br/>年龄:<input type="text" name="sage" value="<%=student.getSage() %>"/><br/>地址:<input type="text" name="saddress" value="<%=student.getSaddress() %>"/><br/><input type="submit" value=" 修改" /><br/><a href="QueryAllStudentdServlet">返回</a></form>
</body>
</html>
总结
web项目中信息在各个层次之间的传递的过程,跳转页面重定向和请求转发的区别,在JSP中怎样获取参数,调用方法等各种细节,需要不断的练习