MVC学生管理

三个包,首先在数据库中新建一个名字为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 block
			e.printStackTrace();
		}
		try {
			return DriverManager.getConnection(url,username,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static void close(Connection conn){
		if(conn!=null)
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.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 {

	@Override
	public void save(Student s) {
		// TODO Auto-generated method stub
		String 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 block
			e.printStackTrace();
		}finally{
			DBUtil.close(conn);
		}
	}

	@Override
	public List list() {
		// TODO Auto-generated method stub
		
		String 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 block
			e.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 stub
		
		
		String 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 stub
		doGet(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页面输入之后就可以在数据库中找到更新的数据了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值