Đầu tiên ta tạo một trang index.jsp với form cho phép nhận dữ liệu vào từ người dùng như dưới:
<%--
Document : Search
Created on : Jul 19, 2014, 11:36:41 PM
Author : Nguyen Minh An
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Enter the key</h2>
<div>
<form action="search" method="get">
<input type="text" name="keyword"/>
<input type="submit" value="Search"/>
</form>
</div>
</body>
</html>
Tiếp đó ta tạo một 3 trang nữa là. Recommended.jsp, SearchingResult.jsp và SearchingResult2.jsp như dưới.
Recommended.jsp
<%--
Document : Recomment
Created on : Jul 19, 2014, 11:48:33 PM
Author : Nguyen Minh An
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Tìm kiếm nâng cao:</h2>
<form action="searchdetail">
Title: <input type="text" name="title"/><br>
Author: <input type="text" name="author"/><br>
Price: <input type="text" name="price"/><br>
<input type="submit" value="Search"/>
</form>
<a href="back">Back</a>
</body>
</html>
Recommended.jsp
<%--
Document : Recomment
Created on : Jul 19, 2014, 11:48:33 PM
Author : Nguyen Minh An
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Tìm kiếm nâng cao:</h2>
<form action="searchdetail">
Title: <input type="text" name="title"/><br>
Author: <input type="text" name="author"/><br>
Price: <input type="text" name="price"/><br>
<input type="submit" value="Search"/>
</form>
<a href="back">Back</a>
</body>
</html>
SearchingResult.jsp
<%--
Document : SearchResult
Created on : Jul 19, 2014, 11:50:14 PM
Author : Nguyen Minh An
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Search - Result</title>
</head>
<body>
<h2>Kết quả tìm thấy:</h2>
<table border="1">
<s:iterator value="list">
<tr>
<td>Title: <s:property value="title"/></td>
<td>Author: <s:property value="author"/></td>
<td>Price: <s:property value="price"/></td>
</tr>
</s:iterator>
</table>
<a href="back">Back</a>
</body>
</html>
và SearchingResult2.jsp
<%--
Document : SearchResult2
Created on : Jul 20, 2014, 1:02:34 AM
Author : Nguyen Minh An
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Kết quả tìm thấy:</h2>
<table border="1">
<s:iterator value="lists">
<tr>
<td>Title: <s:property value="title"/></td>
<td>Author: <s:property value="author"/></td>
<td>Price: <s:property value="price"/></td>
</tr>
</s:iterator>
</table>
<a href="back">Back</a>
</body>
</html>
Tiếp đó đến phần xử lý nghiệp vụ ta tạo ra một lớp java với tên BookManagement.java như dưới:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package obrc.bussiness;
import db.connection.DataAccess;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import obrc.entity.Book;
/**
*
* @author Nguyen Minh An
*/
public class BookManagement {
public List<Book> searchByName(String name) {
List<Book> list = new ArrayList<>();
try {
Connection conn = DataAccess.getConnection();
PreparedStatement ps = conn.prepareCall("select * from BOOK WHERE title like %'" + name + "'%");
ResultSet res = ps.executeQuery();
while (res.next()) {
int id = res.getInt("id");
String title = res.getString("title");
String author = res.getString("author");
String price = res.getString("price");
Book b = new Book(id, title, author, price);
list.add(b);
}
} catch (Exception ex) {
Logger.getLogger(BookManagement.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public ArrayList<Book> getBookByAdvanceSearch(String title, String author, String price) {
try {
ArrayList<Book> bl = new ArrayList<>();
Connection conn = DataAccess.getConnection();
PreparedStatement ps = conn.prepareCall("select * from Book where title like ? or author like ? or price like ?");
ps.setString(1, "%" + title + "%");
ps.setString(2, "%" + author + "%");
ps.setString(3, "%" + price + "%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String titles = rs.getString("title");
String authors = rs.getString("author");
String prices = rs.getString("price");
Book b = new Book(id, titles, authors, prices);
bl.add(b);
}
return bl;
} catch (SQLException ex) {
Logger.getLogger(BookManagement.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public ArrayList<Book> getBookByAdvanceSearch(String title, String author, String price) {
try {
ArrayList<Book> bl = new ArrayList<>();
Connection conn = DataAccess.getConnection();
PreparedStatement ps = conn.prepareCall("select * from Book where title like ? or author like ? or price like ?");
ps.setString(1, "%" + title + "%");
ps.setString(2, "%" + author + "%");
ps.setString(3, "%" + price + "%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String titles = rs.getString("title");
String authors = rs.getString("author");
String prices = rs.getString("price");
Book b = new Book(id, titles, authors, prices);
bl.add(b);
}
return bl;
} catch (SQLException ex) {
Logger.getLogger(BookManagement.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
Cấu hình cho file Struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="search" class="obrc.ui.SearchAction" method="execute">
<result name="success">SearchResult.jsp</result>
<result name="error">Recomment.jsp</result>
</action>
<action name="searchdetail" class="obrc.ui.AdvanceSearching" method="execute">
<result name="success">SearchResult2.jsp</result>
<result name="error">Recomment.jsp</result>
</action>
<action name="back" class="obrc.ui.Back">
<result name="success">Search.jsp</result>
</action>
</package>
</struts>
Tạo đối tượng Book.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package obrc.entity;
/**
*
* @author Nguyen Minh An
*/
public class Book {
private int id;
private String title;
private String author;
private String price;
public Book(int id, String title, String author, String price) {
this.id = id;
this.title = title;
this.author = author;
this.price = price;
}
public Book(int aInt, String string, String string0) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="search" class="obrc.ui.SearchAction" method="execute">
<result name="success">SearchResult.jsp</result>
<result name="error">Recomment.jsp</result>
</action>
<action name="searchdetail" class="obrc.ui.AdvanceSearching" method="execute">
<result name="success">SearchResult2.jsp</result>
<result name="error">Recomment.jsp</result>
</action>
<action name="back" class="obrc.ui.Back">
<result name="success">Search.jsp</result>
</action>
</package>
</struts>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package obrc.entity;
/**
*
* @author Nguyen Minh An
*/
public class Book {
private int id;
private String title;
private String author;
private String price;
public Book(int id, String title, String author, String price) {
this.id = id;
this.title = title;
this.author = author;
this.price = price;
}
public Book(int aInt, String string, String string0) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Tạo tiếp Action cho việc search.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package obrc.ui;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import obrc.bussiness.BookManagement;
import obrc.entity.Book;
/**
*
* @author Nguyen Minh An
*/
public class SearchAction extends ActionSupport {
private String keyword;
private List<Book> list;
public SearchAction() {
}
public String execute() throws Exception {
if (!"".equals(keyword)) {
list = new BookManagement().searchByName(keyword);
if (list.size() > 0) {
list = new BookManagement().searchByName(keyword);
return SUCCESS;
} else {
return ERROR;
}
}
return ERROR;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public List<Book> getList() {
return list;
}
public void setList(List<Book> list) {
this.list = list;
}
}
Tiếp theo là tạo Action cho Advance Search.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package obrc.ui;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import obrc.bussiness.BookManagement;
import obrc.entity.Book;
/**
*
* @author Nguyen Minh An
*/
public class AdvanceSearching extends ActionSupport {
private String title;
private String author;
private String price;
private List<Book> lists;
public AdvanceSearching() {
}
public String execute() throws Exception {
lists = new BookManagement().getBookByAdvanceSearch(title, author, price);
if (lists.size() > 0) {
return SUCCESS;
}else{
return ERROR;
}
}
public List<Book> getLists() {
return lists;
}
public void setLists(List<Book> lists) {
this.lists = lists;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Tiếp tới cái quan trọng nhất là tạo lớp kết nối xuống cơ sở dữ liệu.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package db.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nguyen Minh An
*/
public class DataAccess {
public static Connection getConnection() throws SQLException {
String url = "jdbc:sqlserver://localhost:1433;databaseName=READER_CLUB";
String username = "LUCKY";
String password = "123456";
try {
Connection conn = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (ClassNotFoundException ex) {
Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
Các bạn có thể download Source Code ở đây.
treo đầu dê bán thịt chó sao?
Trả lờiXóa