实验内容:
在数据库中建立一个表: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>
书号 书名 价格 <br>
<%
if(books!= null){
out.println(books.get(0).getId()+"\t"+books.get(0).getName()+"\t"+books.get(0).getPrice());
}
%>
</body>
</html>
实现效果图: