要实现的功能:
点击图书查询,在页面出现要查询的图书详情信息
实现思路:
修改一下sql语句即可,通过ArrayList将所有查询结果进行存储,在model层的构造方法中修改输出的toString方法的内容格式,在页面接受一下查询结果输出展示即可;详情代码如下:
DButil工具类
package com.wbj.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DButil {
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getconn(){
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=MyDB", "sa", "1");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,PreparedStatement ps,ResultSet rs){
try {
if (conn!=null)
conn.close();
if (ps!=null)
ps.close();
if (rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
工具类查询方法
package com.wbj.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.wbj.model.Book;
public class DBselsect {
public static ArrayList<Book> getbookName(String bookName){
Connection conn = DButil.getconn();
String sql = "select * from mybooks where bookName=? ";
PreparedStatement ps = null;
Book book = null;
ResultSet rs = null;
ArrayList<Book> list = new ArrayList<Book>();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, bookName);
rs = ps.executeQuery();
while (rs.next()) {
//存储查询的值
book = new Book();
book.setAuteur(rs.getString("auteur"));
book.setBrief(rs.getString("brief"));
book.setbookName(rs.getString("bookName"));
book.setPrice(rs.getString("price"));
list.add(book);
}
} catch (SQLException e) {
}finally{
DButil.close(conn, ps, rs);
}
return list;
}
}
model层构造方法
package com.wbj.model;
/**
*
* @author model层 UserIfo类
*
*/
public class Book {
private Integer id;
private String bookName;//书名
private String auteur;//作者
private String brief;//简介
private String price;//价格
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuteur() {
return auteur;
}
public void setAuteur(String auteur) {
this.auteur = auteur;
}
public String getbookName() {
return bookName;
}
public void setbookName(String bookName) {
this.bookName = bookName;
}
public String getBrief() {
return brief;
}
public void setBrief(String brief) {
this.brief = brief;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return " 作者:" + auteur + " 书名:" + bookName
+ " 简介:" + brief +" 价格:" + price+"元";
}
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(Integer id, String auteur, String bookName, String brief,
String price) {
super();
this.id = id;
this.auteur = auteur;
this.bookName = bookName;
this.brief = brief;
this.price = price;
}
}
Servlet类
package com.wbj.action;
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 com.wbj.model.Book;
import com.wbj.util.DBselsect;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
//获取请求
String bookName = request.getParameter("bookName");
ArrayList<Book> list = DBselsect.getbookName(bookName);
//响应
request.setAttribute("list", list);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
页面代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.wbj.model.Book"%>
<% %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.bt{
width: 100%;
text-align: center;
background-color: coral;
padding-top: 30px;
padding-bottom: 30px;
}
.bt:hover{
color: aliceblue;
}
span{
text-align: center;
font-size: 30px;
}
table{
margin: 0 auto;
padding-top: 80px;
}
td{
padding-top:30px ;
font-size: 25px;
text-align: center;
}
th{
padding-bottom: 20px;
font-size: 30px;
}
input{
font-size: 20px;
}
#dl{
background-color: cyan;
}
#zc{
background-color: coral;
}
#f:hover{
}
</style>
</head>
<body>
<div class="bt" >
<span>图书管理系统</span>
</div>
<form action="text" method="post">
<table >
<tr><th colspan="3" id="f">图书查询</th></tr>
<tr><td colspan="3" >查询:<input type="text" id="" name="bookName" placeholder="请输入书名"/></td></tr>
<tr><td colspan="2"><%
ArrayList<Book> list =(ArrayList) request.getAttribute("list");
if(list!=null&&list.size()>0){
for(Book book:list){
%><%=book.toString()%><%
}
}else if(list!=null){
out.print("查无此书");
}else{}
%></td></tr>
<tr><td colspan="3"><input type="submit" id="dl" name="login" value="查询"/></td></tr>
</table></form>
</body>
</html>
以上就是整个代码流程,有不妥的地方请帮忙改正