MVC简单实验(JSP+Servlet+VO+DAO)

实验内容:

在数据库中建立一个表:book(id,name,price)。

1、 创建一个JavaBean类Book。

2、 根据表单文本框中提供的id,通过JSP+Servlet+VO+DAO完成书籍的查询.

实现过程:

vo层:

package beans;

public class Book {

	private Integer id;
	private String name;
	private String price;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	
}

Dao层:

接口类

package dao;

import java.util.ArrayList;
import beans.Book;

public interface BookDao {
	 ArrayList<Book> qurryBooksByid(String id) throws Exception;
}

实现类

package dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.BookDao;

import beans.Book;

public class BookDaoImpl implements BookDao {
	public ArrayList<Book> qurryBooksByid(String id)  {
		Connection conn =null;
		ArrayList<Book> books = new ArrayList<Book>();
		try{
			//获取连接
			try {
				Class.forName("com.mysql.cj.jdbc.Driver");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/ts","root","123456");
			String sql = "select * from book where id = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				Book book  = new Book();
				book.setId(rs.getInt("id"));
				book.setName(rs.getString("name"));
				book.setPrice(rs.getString("price"));
				books.add(book);
			}
			rs.close();
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();
		}finally {
			try{
				if(conn !=null){
					conn.close();
					conn = null;
				}
			}catch(Exception ex){}
		}
		return books;
	}

}

servlet层

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.Book;
import dao.impl.*;

public class BookServlet extends HttpServlet {

	/**
	 * 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 {
		request.setCharacterEncoding("gb2312");
		String id = request.getParameter("id");
		ArrayList<Book> books = new ArrayList<Book>();
		BookDaoImpl bookDao = new BookDaoImpl();
		books = bookDao.qurryBooksByid(id);
		HttpSession session = request.getSession();
		session.setAttribute("books",books);
		response.sendRedirect("/prj04/qurryBook.jsp");
	}

}

jsp层:

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%@page import= "beans.Book" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<boby>
 <P>根据id查询书籍信息:</P>
    <form method="post" action='/prj04/servlet/BookServlet'>
    	<br>输入id:<input type="text" name='id'>
    	<input value="提交" type="submit">
    </form> 
   <br><hr>
    <%
  			ArrayList<Book> books= (ArrayList<Book>)session.getAttribute("books");
  		 %> 
  		 书籍信息为:<br>
  		 书号&nbsp;&nbsp;书名&nbsp;&nbsp;&nbsp;价格&nbsp;&nbsp;&nbsp;<br>
  		 <%
  		 if(books!= null){
  			out.println(books.get(0).getId()+"\t"+books.get(0).getName()+"\t"+books.get(0).getPrice());
		  }
  		 %> 
  </body>
</html>

实现效果图:

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShiBaLover

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值