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();
}
}
}
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>";
}
}
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);
}
}
<?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>
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;
}
}
<%@ 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>
<%@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>
輸入資料庫沒有的資料之後
點選查詢
輸入資料庫中已有的資料之後
點選查詢之後
這樣一個圖書查詢系統就算完成了