該部份內容實現的功能主要是:從數據庫中查詢數據並展現到頁面,在頁面中新增數據信息,在頁面中修改數據信息,在頁面中刪除數據信息。 =================stuList.jsp================ //本頁爲數據顯示頁面 <%@ page import="java.util.List" %> <%@ page import="com.ibeifeng.student.Student" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>stuList</title> </head> <body> <a href="addStu.jsp">新增學員</a> <table width="100%" border="1px solid black"> <tr> <td>學號</td> <td>姓名</td> <td>密碼</td> <td>性別</td> <td>郵箱</td> <td>操做</td> </tr> <% List<Student> list = (List<Student>)request.getAttribute("list"); for(Student stu : list){ %> <tr> <td><%=stu.getId()%></td> <td><%=stu.getName()%></td> <td><%=stu.getPwd()%></td> <td><%=stu.getSex()%></td> <td><%=stu.getEmail()%></td> <td><a href="updateStu.jsp?sid=<%=stu.getId()%>">修改</a> | <a href="doDel.jsp?sid=<%=stu.getId()%>">刪除</a></td> </tr> <% } %> </table> </body> </html> ===============doList02.jsp================== //本頁是數據顯示頁面stuList的後臺數據處理部分 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="com.ibeifeng.student.Student" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.List" %> <%@ page import="com.ibeifeng.student.ConfigUtil" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% Class.forName(new ConfigUtil().getValue("driver")); Connection conn = DriverManager.getConnection( new ConfigUtil().getValue("url"), new ConfigUtil().getValue("username"), new ConfigUtil().getValue("password")); String sql = "select * from student"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); List<Student> list = new ArrayList<Student>(); while(rs.next()){ Student stu = new Student(); stu.setId(rs.getInt(1)); stu.setName(rs.getString(3)); stu.setPwd(rs.getString(2)); stu.setSex(rs.getInt(4)); stu.setEmail(rs.getString(5)); list.add(stu); } if(rs != null){ rs.close(); } if(ps != null){ ps.close(); } if(conn != null){ conn.close(); } request.setAttribute("list",list); request.getRequestDispatcher("stuList.jsp").forward(request,response); %> ====================addStu.jsp============== //本頁爲新增數據的頁面展現部分 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>新增學員信息</title> </head> <body> <form action="doSave.jsp" method="post"> <table align="center" border="1px solid black"> <tr> <td>學號</td> <td><input type="text" name="stuId" > </td> </tr> <tr> <td>姓名</td> <td><input type="text" name="stuName" ></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="pwd" ></td> </tr> <tr> <td>性別</td> <td> <input type="radio" name="gender" value="0">女 <input type="radio" name="gender" value="1">男 </td> </tr> <tr> <td>郵箱</td> <td><input type="text" name="email" ></td> </tr> <tr> <td colspan="2"> <input type="submit" name="submit" value="保存"> <input type="button" name="return" value="取消"> </td> </tr> </table> </form> </body> </html> ===================doSave.jsp============== //本頁爲數據新增頁面,保存按鈕的數據處理部分 <%@ page import="com.ibeifeng.student.ConfigUtil" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.ResultSet" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); Class.forName(new ConfigUtil().getValue("driver")); Connection conn = DriverManager.getConnection( new ConfigUtil().getValue("url"),new ConfigUtil().getValue("username"),new ConfigUtil().getValue("password")); String sql = "insert into student(stu_id,stu_name,pwd,sex,email) values(?,?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); String rid = request.getParameter("stuId"); int id = Integer.parseInt(rid); ps.setInt(1,id); ps.setString(2,request.getParameter("stuName")); ps.setString(3,request.getParameter("pwd")); ps.setInt(4,1); ps.setString(5,request.getParameter("email")); int isSuccess = ps.executeUpdate(); System.out.print(isSuccess); if(isSuccess <= 0){ System.out.print("新增有誤,請重試"); request.getRequestDispatcher("/stuInfo/addStu.jsp").forward(request,response); }else{ request.getRequestDispatcher("/stuInfo/doList02.jsp").forward(request,response); } if(ps != null){ ps.close(); } if(conn != null){ conn.close(); } %> ================updateStu.jsp=============== //本頁爲數據修改頁面展現部分 <%@ page import="com.ibeifeng.student.StudentDao" %> <%@ page import="com.ibeifeng.student.Student" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //獲取前端頁面傳遞的參數 String sid = request.getParameter("sid"); //該參數由stuList頁面的<a href="updateStu.jsp?sid=stu.getStudentId()">傳遞過來 //建立StudentDao對象 StudentDao stuDao = new StudentDao(); Student stu = stuDao.getStuByStuId(sid); if(stu != null){ request.setAttribute("stu",stu); } %> <html> <head> <title>新增學員信息</title> </head> <body> <form action="doUpdate.jsp" method="post"> <table align="center" border="1px solid black"> <tr> <td>學號</td> <td><input type="text" name="stuId" readonly="readonly" value="<%=stu.getId()%>"> </td> </tr> <tr> <td>姓名</td> <td><input type="text" name="stuName" value="<%=stu.getName()%>"></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="pwd" value="<%=stu.getPwd()%>"></td> </tr> <tr> <td>性別</td> <td> <input type="radio" name="gender" value="0" <%if(stu.getSex() == 0){%>checked="checked"<%}%>>女 <input type="radio" name="gender" value="1" <%if(stu.getSex() == 1){%>checked="checked"<%}%>>男 </td> </tr> <tr> <td>郵箱</td> <td><input type="text" name="email" value="<%=stu.getEmail()%>"></td> </tr> <tr> <td colspan="2"> <input type="submit" name="submit" value="保存"> <input type="button" name="return" value="取消"> </td> </tr> </table> </form> </body> </html> =================doUpdate.jsp============== //本頁爲數據修改部分,後臺數據處理操做的內容 <%@ page import="com.ibeifeng.student.ConfigUtil" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Connection" %> <%@ page import="com.ibeifeng.student.StudentDao" %> <%@ page import="com.ibeifeng.student.Student" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); Class.forName(new ConfigUtil().getValue("driver")); Connection conn = DriverManager.getConnection( new ConfigUtil().getValue("url"),new ConfigUtil().getValue("username"),new ConfigUtil().getValue("password")); String stuid = request.getParameter("stuId"); int sid = Integer.parseInt(stuid); String name = request.getParameter("stuName"); String pwd = request.getParameter("pwd"); String gender = request.getParameter("gender"); int gen = Integer.parseInt(gender); String email = request.getParameter("email"); StudentDao stuDao = new StudentDao(); Student stu = new Student(); stu.setId(sid); stu.setName(name); stu.setPwd(pwd); stu.setSex(gen); stu.setEmail(email); int resule = stuDao.updateStu(stu); if(resule > 0){ request.getRequestDispatcher("doList02.jsp").forward(request,response); }else{ request.getRequestDispatcher("updateStu.jsp?sno="+stuid).forward(request,response); } %> ===============doDel.jsp=================== //本頁爲數據刪除部分後臺處理內容 <%@ page import="com.ibeifeng.student.StudentDao" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String sid = request.getParameter("sid"); StudentDao stuDao = new StudentDao(); stuDao.delStu(sid); request.getRequestDispatcher("doList02.jsp").forward(request,response); %> ===============BaseDao.java================= //本頁爲封裝的方法,主要是涉及到數據庫鏈接的初始化、查詢、更新、資源釋放 package com.ibeifeng.student; import java.sql.*; /* * JDBC操做DAO通用類方法封裝 * */ public class BaseDao { static Connection conn = null; static PreparedStatement ps = null; static ResultSet rs = null; //靜態語句塊 static{ try { Class.forName(ConfigUtil.getValue("driver")); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConn(){ try { conn = DriverManager.getConnection(ConfigUtil.getValue("url"), ConfigUtil.getValue("username"), ConfigUtil.getValue("password")); } catch (SQLException e) { e.printStackTrace(); } return conn; } /* * 查詢 * */ public ResultSet search(String sql){ try { rs = ps.executeQuery(); return rs; } catch (SQLException e) { throw new RuntimeException(); } } /* * 更新數據 * */ public int modify(String sql){ try { return ps.executeUpdate(sql); } catch (SQLException e) { throw new RuntimeException(); } } public static void close(ResultSet rs, PreparedStatement ps, Connection conn){ try { if(rs != null) { rs.close(); } if(ps != null) { ps.close(); } if(conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } =================ConfigUtil.java============== //本頁封裝了從配置文件讀取數據的方法 package com.ibeifeng.student; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ConfigUtil { //經過key獲取jdbc配置的value public static String getValue(String key){ Properties pro = new Properties(); //獲取流信息 InputStream is = ConfigUtil.class.getResourceAsStream("/jdbc.properties"); try { //加載流文件 pro.load(is); //獲取屬性值 return pro.getProperty(key); } catch (IOException e) { throw new RuntimeException(); } } } ================StudentDao.java============== //本頁封裝了對於數據庫,數據處理的一些方法 package com.ibeifeng.student; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class StudentDao { /* * Student Dao用於處理Student類 * */ Connection conn = null; PreparedStatement ps = null; public int updateStu(Student stu){ /* * 更新學員信息 * */ String sql = "update student set stu_name=?,pwd=?,sex=?,email=? where stu_id=?"; try { ps = BaseDao.getConn().prepareStatement(sql); ps.setString(1,stu.getName()); ps.setString(2,stu.getPwd()); ps.setInt(3,stu.getSex()); ps.setString(4,stu.getEmail()); ps.setInt(5,stu.getId()); return ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(); }finally { BaseDao.close(null,ps,conn); } } public Student getStuByStuId(String stu_id){ String sql = "select * from student where stu_id=?"; Student stu = null; try { conn = BaseDao.getConn(); ps = conn.prepareStatement(sql); int sid = Integer.parseInt(stu_id); ps.setInt(1,sid); ResultSet rs = ps.executeQuery(); if(rs.next()){ stu = new Student(); stu.setId(rs.getInt("stu_id")); stu.setName(rs.getString("stu_name")); stu.setPwd(rs.getString("pwd")); stu.setSex(rs.getInt("sex")); stu.setEmail(rs.getString("email")); } } catch (SQLException e) { e.printStackTrace(); }finally { BaseDao.close(null,ps,conn); } return stu; } /* * 根據學號刪除學生信息 * */ public int delStu(String sid){ String sql = "delete from student where stu_id = ?"; conn = BaseDao.getConn(); try { ps = conn.prepareStatement(sql); int id = Integer.parseInt(sid); ps.setInt(1,id); return ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(); } } } ===================Student.java============== //封裝類對象 package com.ibeifeng.student; public class Student { private int id; private String name; private String pwd; private int sex; private String email; public Student(){} public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setPwd(String pwd) { this.pwd = pwd; } public void setSex(int sex) { this.sex = sex; } public void setEmail(String email) { this.email = email; } public int getId() { return id; } public String getName() { return name; } public String getPwd() { return pwd; } public int getSex() { return sex; } public String getEmail() { return email; } } =============jdbc.properties============= //配置文件信息 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=123456