目录
图书信息查询系统
分层结构
util包
DButil代码
package top.xinsir.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();
}
}
}
bean包
Book.java代码
package top.xinsir.bean;
public class Book {
private Integer id;
private String name;
private String isbn;
private Float price;
private String author;
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 getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Book(Integer id, String name, String isbn, Float price, String author) {
super();
this.id = id;
this.name = name;
this.isbn = isbn;
this.price = price;
this.author = author;
}
public Book(String name, String isbn, Float price, String author) {
super();
this.name = name;
this.isbn = isbn;
this.price = price;
this.author = author;
}
public Book() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "<tr>"
+"<td>"+id+"</td>"
+"<td>"+name+"</td>"
+"<td>"+isbn+"</td>"
+"<td>"+price+"</td>"
+"<td>"+author+"</td>"
+"</tr>";
}
}
action包
BookSearchServlet.java代码
package top.xinsir.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import top.xinsir.bean.Book;
import top.xinsir.test.BookTest;
public class BookSearchServlet 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");
// 获取参数
String bookName = request.getParameter("bookName");
// 获取传递过来的Book对象
Book book = BookTest.getBookByName(bookName);
// 将图书信息显示在bookInfo.jsp页面
request.setAttribute("book", book);
request.getRequestDispatcher("bookInfo.jsp").forward(request, response);
}
}
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>BookSearchServlet</servlet-name>
<servlet-class>top.xinsir.action.BookSearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookSearchServlet</servlet-name>
<url-pattern>/bookSearch</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
test包
BookTest.java代码
package top.xinsir.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import top.xinsir.bean.Book;
import top.xinsir.util.DBUtil;
public class BookTest {
public static Book getBookByName(String bookName){
// 连接数据库并且把数据查询出来 存储在book对象中,最后返回book对象即可
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// 查询代码
conn = DBUtil.getConn();// 加载驱动
String sql = "select * from book where name=?";//准备sql语句
Book book = new Book();// 实例化Book对象
try {
ps = conn.prepareStatement(sql);
// 设置值
ps.setString(1, bookName);
rs = ps.executeQuery();
if(rs.next()){
// 如果判断查询到值得话赋值到book对象中
book = new Book();// 重新实例化Book对象
// 设置book里的值
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setIsbn(rs.getString("isbn"));
book.setPrice(rs.getFloat("price"));
book.setAuthor(rs.getString("author"));
}else{
// 如果没有查询到内容给的话book为空值
book = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 关闭资源
DBUtil.close(conn, ps, rs);
// 返回book对象
return book;
}
}
WebRoot下jsp页面
search.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'search.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
// 如果没有该图书返回一个msg值 接收该值
String msg = (String)request.getAttribute("msg");
if(msg==null)
msg="";
%>
<form action="bookSearch" method="post">
图书名称:<input type="text" name="bookName" /><span style="color: red"><%=msg %></span><br>
<input type="submit" value="查询" />
</form>
</body>
</html>
bookInfo.jsp页面
<%@page import="top.xinsir.bean.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'bookInfo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
table{
margin: 0 auto;
}
h1{
text-align: center;
color: red;
}
td{
text-align: center;
}
.id,.price{
width: 60px;
}
.name,.author{
width: 100px;
}
.isbn{
width: 250px;
}
</style>
</head>
<body>
<%
// 接收Book值
Book book = (Book) request.getAttribute("book");
// 如果book的值不是空值得话 打印出来图书信息
%>
<h1>查询结果</h1>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th class="id">id</th>
<th class="name">书籍名称</th>
<th class="isbn">ISBN</th>
<th class="price">价钱</th>
<th class="author">作者</th>
</tr>
<%
if (book != null) {
out.println(book.toString() + "<br>");
} else {
// 如果为空值的话说明没有这个图书信息
request.setAttribute("msg", "很抱歉,没有查询到该图书");
// 继续转发到search.jsp页面
request.getRequestDispatcher("search.jsp").forward(request,
response);
}
%>
</table>
</body>
</html>
book表数据
浏览器效果
search.jsp页面
输入数据库没有的数据之后
点击查询
输入数据库中已有的数据之后
点击查询之后
这样一个图书查询系统就算完成了