MVC学生管理
来源:互联网 发布:python mobi 下载 编辑:程序博客网 时间:2024/06/11 21:13
三个包,首先在数据库中新建一个名字为students的table
第一个javabean的包
Student.java封装数据库中的行,得到get与set方法
package bean;public class Student {private int id;private int age;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
第二个包,封装数据库
DBUtil.java,与数据库建立联系
package DB;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBUtil {private static String driver;private static String url;private static String username;private static String password;static {driver="com.mysql.jdbc.Driver";url="jdbc:mysql://localhost:3306/tree";username="root";password="wonshy123..";}public static Connection open(){try {Class.forName(driver);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {return DriverManager.getConnection(url,username,password);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}public static void close(Connection conn){if(conn!=null)try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
Dao.java
package DB;import java.util.List;import bean.Student;public interface Dao {public void save(Student s);public List list();}
DaoImpl.java
package DB;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import bean.Student;public class DaoImpl implements Dao {@Overridepublic void save(Student s) {// TODO Auto-generated method stubString sql="insert into students(age,name)values(?,?)";Connection conn=DBUtil.open();try {PreparedStatement pstmt=conn.prepareStatement(sql);pstmt.setInt(1, 10);pstmt.setString(2, "tree");pstmt.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.close(conn);}}@Overridepublic List list() {// TODO Auto-generated method stubString sql="select id,age,name from students";Connection conn=DBUtil.open();try {Statement stmt=conn.createStatement();ResultSet rs=stmt.executeQuery(sql);List list=new ArrayList();while(rs.next()){int id=rs.getInt(1);int age=rs.getInt(2);String name=rs.getString(3);Student s=new Student();s.setId(id);s.setAge(age);s.setName(name);list.add(s);}return list;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.close(conn);}return null;}}
第三个包,处理事务的servlet
package mine;import java.io.IOException;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 DB.DaoImpl;import bean.Student;/** * Servlet implementation class stuservlet */@WebServlet("/stuservlet")public class stuservlet extends HttpServlet {private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public stuservlet() { super(); // 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 stubString name=request.getParameter("name");int age=Integer.parseInt(request.getParameter("age"));DaoImpl stu=new DaoImpl();Student s=new Student();s.setAge(age);s.setName(name);stu.save(s);request.getRequestDispatcher("stu.jsp").forward(request, response);}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}
最后一个jsp页面显示
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!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=ISO-8859-1"><title>Insert title here</title></head><body><form name="f1" id="f1" action="stuservlet" method="post"><table border="0"><tr><td>Name:</td><td><input type="text" name="name" id="name"></td></tr><tr><td>Age:</td><td><input type="text" name="age" id="age"></td></tr><tr><td colspan="2" align="center"><input type="submit" value="save"></td></tr></table></form><hr></body></html>
在jsp页面输入之后就可以在数据库中找到更新的数据了
0 0
- MVC学生管理
- mvc模式 学生管理系统
- MVC实现学生管理系统的一些技巧和注意事项
- 学生管理系统——MVC设计思想
- 学生管理
- 基于MVC的JSP学生管理系统(目前只可以浏览数据)(1)
- java EE 开发入门小程序 mvc设计模式 学生信息的管理
- 一个学生管理系统
- 学生同学录管理系统
- 学生管理系统
- 销售学生管理系统
- 学生通讯管理系统
- 学生管理系统
- 学生成绩管理
- 学生管理系统
- 学生管理系统
- 学生管理系统
- 学生成绩管理
- 使用Cocos构建一个完整项目
- C++中string类详解
- [Java] 常用方法的解释
- 蓝桥杯_算法训练_安慰奶牛(用Kruskal、Prim算法分别实现)
- stackoverflow转载(关于对attributeSet理解)
- MVC学生管理
- centos log4cxx编译安装
- 路由器和网关的区别?
- equalsIgnoreCase()和equals()以及'=='的区别以及使用,
- dom4j 新增节点 自动出现xmlns=""
- 《Python绝技:运用Python成为顶级黑客》试读感想
- BZOJ_P1318 [SPOJ744] Longest Permutation(单调栈)
- xib绘制的cell中动态改变cell子视图的frame
- 外层边框使用100%时候,缩小窗口会导致元素错乱