大三暑假在實習,第二周已經結束了,現在可以自己動手寫一些比較小的工程。就是寫出來調試真的好費人。
簡單介紹一下,有一個簡單的學生信息表,數據庫設計如下:
然後,根據MVC進行分層處理: 程序運行結果: 1、查詢數據庫的結果。 2、點擊新增 3、點擊編輯,會獲取到Id,進行對應的編輯 4、點擊刪除,直接刪除。package com.mm.bean; public class Student { int id;//學號 String name;//姓名 int age;//年齡 String classes;//班級 public Student(){} 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getClasses() { return classes; } public void setClasses(String classes) { this.classes = classes; } }
package com.mm.Dao; import java.util.List; import com.mm.bean.Student; public interface IStuDao { /** * 新增一個學生 * @param stu */ public void addstu(Student stu); /** * 修改一個學生 * @param stu */ public void updatestu(Student stu); /** * 通過ID刪除一個學生 * @param id */ public void delstu(int id); /** * 找到所有的學生 * @return 返回一個集合 */ public List<Student> findall(); /** * 通過id找到一個學生 * @param id * @return */ public Student findStubyId(int id); }
package com.mm.Dao.impl;
package com.mm.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtils { /** * @param args * @throws SQLException */ public static Connection getconnection() throws SQLException { try { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "mxning", "mxning"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void release(ResultSet rs,PreparedStatement ps,Connection con){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
package com.mm.sevlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.mm.Dao.IStuDao; import com.mm.Dao.impl.StuDaoImpl; import com.mm.bean.Student; public class studentServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { IStuDao sd = new StuDaoImpl(); String op = request.getParameter("op"); if(op==null) op = "list"; if("list".equals(op)){ //接收 顯示所有的請求信息 List<Student> list = sd.findall(); request.setAttribute("list", list); response.sendRedirect("studentlist.jsp"); return ; }else if("add".equals(op)){ //若是新增,則跳轉到新增界面處理 response.sendRedirect("studentadd.jsp"); return ; }else if("edit".equals(op)){ //取到要編輯的數據,然後 若command=view 則轉向view.jsp;否則跳轉到編輯頁面 String id = request.getParameter("id"); //System.out.println(id); Student s = sd.findStubyId(Integer.parseInt(id)); request.setAttribute("stu", s); String command = request.getParameter("command"); if("view".equals(command)){ request.getRequestDispatcher("studentedit.jsp").forward(request, response); //response.sendRedirect("studentedit.jsp"); return ; }else{ response.sendRedirect("studentview.jsp?id="+id); return ; } }else if("delete".equals(op)){ //刪除數據,然後跳轉到列表頁面 //1.獲取參數值 String id =request.getParameter("id"); //2.調用dao刪除指定的數據 sd.delstu(Integer.parseInt(id)); //3.通過response返回到列表頁面 response.sendRedirect("studentServlet?op=list"); return ; }else if("store".equals(op)){ //若是新增,則add;若是編輯,則調用update;然後跳轉到列表頁面 //1.獲取參數值 String id =request.getParameter("id"); String name = request.getParameter("name"); String age = request.getParameter("age"); String classes = request.getParameter("class"); System.out.println(id+name+age); System.out.println(classes+"sssssssssssssss"); //2.封裝成對象 Student obj =new Student(); obj.setName(name); obj.setAge(Integer.parseInt(age)); obj.setClasses(classes); if(id==null||"".equals(id)){ //新增 sd.addstu(obj); //System.out.println("sssssssssssssssssss"); }else{ //編輯 obj.setId(Integer.parseInt(id)); sd.updatestu(obj); } response.sendRedirect("studentServlet?op=list"); return ; } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
<%@ page language="java" import="java.util.*,com.mm.Dao.*,com.mm.Dao.impl.*,com.mm.bean.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); IStuDao is = new StuDaoImpl(); List<Student> list = is.findall(); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>學生列表顯示界面</title> </head> <body> <table align="center" border="1" cellspacing="0"> <tr> <td colspan="5"><button onclick="javascript:document.location.href='studentServlet?op=add';">新增</button> </td> </tr> <tr> <th>學號</th> <th>姓名</th> <th>年齡</th> <th>班級</th> <th>操作</th> </tr> <% int i = 1; for (Student st : list) { %> <tr> <td><%=i%></td> <td> <a href="studentview.jsp?op=edit&id=<%=st.getId()%>"><%=st.getName()%></a> </td> <td><%=st.getAge()%></td> <td><%=st.getClasses()%></td> <td><a href="studentServlet?op=edit&command=view&id=<%=st.getId()%>">編輯</a> <a href="studentServlet?op=delete&id=<%=st.getId()%>">刪除</a> </td> </tr> <% i++; } %> </table> </body> </html>
<%@ page language="java" import="java.util.*,com.mm.Dao.*,com.mm.Dao.impl.*,com.mm.bean.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; String id = request.getParameter("id"); IStuDao is = new StuDaoImpl(); Student s = is.findStubyId(Integer.parseInt(id)); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>每個學生查看界面</title> </head> <body> <h3 align="center">學生查看</h3> <table align="center" width="60%" border="1" cellspacing="0"> <tr><td colspan="2"><button onclick="javascript:history.back(-1);">返回</button></td></tr> <tr> <td>姓名</td> <td><%=s.getName()%></td> </tr> <tr> <td>年齡</td> <td><%=s.getAge()%></td> </tr> <tr> <td>班級</td> <td><%=s.getClasses() %></td> </tr> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'studentadd.jsp' starting page</title> </head> <body> <form action="studentServlet?1=1&op=store" method="post"> <table border = "1" cellspacing = "0" align = "center"> <tr> <th colspan="2" align="left"><button type="submit">提交</button></button> </th> </tr> <tr><td>姓名</td><td><input type = "text" name = "name" /></td></tr> <tr><td>年齡</td><td><input type = "text" name = "age" /></td></tr> <tr><td>班級</td><td><input type = "text" name = "class" /></td></tr> </table> </form> </body> </html>
<%@ page language="java" import="java.util.*,com.mm.bean.*,com.mm.sevlet.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>修改學生界面</title> </head> <body> <% Student s = (Student) request.getAttribute("stu"); %> <form action="studentServlet?op=store" method = "post"> <input type="hidden" name="id" value="<%=s.getId()%>" /> <table border="1" align="center" cellspacing="0"> <tr> <td colspan="2"><button type="submit">提交</button> <button type="button" onclick="javascript:history.back(-1);">返回</button> </td> </tr> <tr> <td>姓名</td> <td><input type="text" name="name" value="<%=s.getName()%>" /> </td> </tr> <tr> <td>年齡</td> <td><input type="text" name="age" value="<%=s.getAge()%>" /> </td> </tr> <tr> <td>班級</td> <td><input type="text" name="class" value="<%=s.getClasses()%>" /> </td> </tr> </table> </form> </body> </html>