mvc增删改查(一)
2016-03-17 11:29
633 查看
index.jsp
<%@ page language="java" import="java.util.*" 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> </head> <body> <form action="/TomcatTest/StudentSer" method="post"> 姓名:<input type="text" name="name"/><br/> 学号:<input type="text" name="ID"/><br/> 年龄:<input type="text" name="age"/><br/> 专业:<input type="text" name="major"/><br/> 分数:<input type="text" name="grade"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
注意action 要写xml中设置的路径
web.xml
servlet标签对应StudentSer类
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <!-- 逻辑名 --> <servlet-name>StudentSer</servlet-name> <!-- 物理名 --> <servlet-class>ser.StudentSer</servlet-class> </servlet> <servlet-mapping> <!-- 逻辑名 --> <servlet-name>StudentSer</servlet-name> <!-- 访问次Ser的url路径 --> <url-pattern>/StudentSer</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/WEB-INF/index.jsp</welcome-file> </welcome-file-list> </web-app>
Student.java(要与数据库的表对应)
package model; import java.util.Date; public class Student { private String name; private String age; private String ID; private String major; private String grade; public Student(String name,String ID,String age,String major,String grade) { this.name=name; this.ID=ID; this.age=age; this.major=major; this.grade=grade; } public void print() { System.out.println("name="+this.name); System.out.println("ID="+this.ID); System.out.println("age="+this.age); System.out.println("major="+this.major); System.out.println("grade="+this.grade); } public Student() { } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setAge(String age) { this.age = age; } public String getAge() { return this.age; } public void setID(String ID) { this.ID = ID; } public String getID() { return this.ID; } public void setMajor(String major) { this.major = major; } public String getMajor() { return this.major; } public void setGrade(String grade){ this.grade = grade; } public String getGrade() { return this.grade; } }
StudentSer.java (相当于controller,ser是servlet的简写,可以new servlet 会自动生成一些函数,但是我的eclipse不能新建servlet不能下一步【不知道为什么,也没查到好用的解决办法 有知道怎么弄的大神求告知】,只new class也是可以的)
servlet类 仅在第一次被请求时实例化(执行构造函数,初始化函数),以后常驻内存,再次被请求时不会执行构造函数和初始化函数,容器关闭时销毁(执行destroy)
package ser; import impl.StudentImpl; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.Student; /* * 在JSP中,Ser它的作用是控制层 也就是获取前台的数据,并且把数据提交给业务逻辑层 * * Ser起着桥梁的作用 它符合MVC设计模式 * * V: view 视图 所有的JSP页面 :功能:显示数据 获取数据 * * C: control: 控制层 功能:获取视图层的数据,并且调用业务逻辑类 * * M: Model: 负责数据录入 和业务逻辑的实现 * * */ public class StudentSer extends HttpServlet { /** * Constructor of the object. */ public StudentSer() { super(); System.out.println("----StudentSer-----"); } /** * Destruction of the servlet. <br> */ /* Ser 销毁的时候调用此方法 */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here System.out.println("----destory----"); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("----doPost----"); Student student = new Student(); request.setCharacterEncoding("utf-8"); student.setName(request.getParameter("name")); student.setID(request.getParameter("ID")); student.setAge(request.getParameter("age")); student.setMajor(request.getParameter("major")); student.setGrade(request.getParameter("grade")); student.print(); StudentImpl studentImpl = new StudentImpl(); studentImpl.save(student); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ // 初始化的方法,会在构造方法之后调用 public void init() throws ServletException { // Put your code here System.out.println("---init----"); } }
DbConnection.java
需要修改DataBaseName为自己的数据库名称 还有user和password为自己数据库的用户名和密码
package db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbConnection { private static final String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static final String url="jdbc:sqlserver://localhost:1433;DataBaseName=Student"; private static final String user="sa"; private static final String password="123456"; //加载驱动需要静态代码块 static{ try{ Class.forName(driverName); } catch(ClassNotFoundException e){ e.printStackTrace(); } } //获得连接对象的方法 public static Connection getConnection(){ Connection connection=null; try{ connection=DriverManager.getConnection(url,user,password); } catch(SQLException e) { e.printStackTrace(); } return connection; } //关闭资源对象的方法 public static void close(ResultSet rs,Statement st,Connection conn){ try{ if(rs!=null){ rs.close(); } } catch(SQLException e){ e.printStackTrace(); } finally{ try{ if(st!=null){ st.close(); } }catch(SQLException e){ e.printStackTrace(); } finally { try{ if(conn!=null){ conn.close(); } } catch(SQLException e){ e.printStackTrace(); } } } } }
StudentImpl.java
package impl; import model.Student; import db.DbConnection; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class StudentImpl { public void save(Student student){ Connection con = null; PreparedStatement ps = null; DbConnection conns = new DbConnection(); con = conns.getConnection(); System.out.println("----sava----"); String sql ="insert into student(name,ID,age,major,grade) values(?,?,?,?,?)"; try{ ps = con.prepareStatement(sql); ps.setString(1, student.getName()); ps.setString(2, student.getID()); ps.setString(3,student.getAge()); ps.setString(4, student.getMajor()); ps.setString(5,student.getGrade()); ps.executeUpdate(); } catch(SQLException e){ e.printStackTrace(); } finally{ try{ con.close(); }catch(SQLException e){ e.printStackTrace(); } } } }
相关文章推荐
- java-WEB中的监听器Lisener
- 分享微信开发Html5轻游戏中的几个坑
- GUI - Web前端开发框架
- Extjs4.0 最新最全视频教程
- MyEclipse Web Project转Eclipse Dynamic Web Project
- axis备忘
- Windows Clang开发环境备忘
- Erlang实现的一个Web服务器代码实例
- 防止网页脚本病毒执行的方法-from web
- 自学成才的秘密:115个 web Develop 资源
- 使用批处理修改web打印设置笔记 适用于IE
- Apache Web让JSP“动”起来
- web下载的ActiveX控件自动更新
- 推荐六款WEB上传组件性能测试与比较第1/10页
- 关于三种主流WEB架构的思考
- 使用 Iisext.vbs 列出 Web 服务扩展文件的方法
- 使用 Iisext.vbs 删除 Web 服务扩展文件的方法
- 使用 iisext.vbs 禁用 Web 服务扩展的方法
- 用vbs 实现从剪贴板中抓取一个 URL 然后在浏览器中打开该 Web 站点