81,092
社区成员
发帖
与我相关
我的任务
分享
package cn.itcast.domain;
public class Book {
private String bid;
private String bname;
private double price;
private int category;
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCategory() {
return category;
}
public void setCategory(int category) {
this.category = category;
}
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + ", category=" + category + "]";
}
}
package cn.itcast.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import cn.itcast.domain.Book;
import cn.itcast.jdbc.utils.C3p0Utils;
public class BookDao {
private QueryRunner qr=new QueryRunner(C3p0Utils.getDataSource());
//查询所有
public List<Book>findAll(){
try {
String sql="select * from t_book";
return qr.query(sql,new BeanListHandler<Book>(Book.class));
}catch(SQLException e) {
throw new RuntimeException(e);
}
}
//按分类查询
public List<Book>findByCategory(int category){
try {
String sql="select * from t_book where category=?";
return qr.query(sql, new BeanListHandler<Book>(Book.class), category);
}catch(SQLException e) {
throw new RuntimeException(e);
}
}
}
package cn.itcast.chapter04.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.dao.BookDao;
import cn.itcast.domain.Book;
@WebServlet("/BookServlet")
public class BookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param=request.getParameter("category");
System.out.println(param);
BookDao dao=new BookDao();
System.out.println(dao.findAll());
List<Book>bookList=null;
//如果category参数不存在,表示查询所有
if(param==null||param.trim().isEmpty()) {
bookList=dao.findAll();
}else {
int category=Integer.parseInt(param);//把参数转换成int类型
//按分类查询图书
bookList=dao.findByCategory(category);
}
//把图书保存到request中
request.setAttribute("bookList", bookList);
request.getRequestDispatcher("/show.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package cn.itcast.jdbc.utils;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Utils {
private static DataSource ds;
static {
ds=new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return ds;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="user">root</property>
<property name="password">ding</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/chapter04</property>
<property name="checkoutTimeout">30000</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<named-config name="a">
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">15</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/chapter04</property>
<property name="user">root</property>
<property name="password">ding</property>
</named-config>
</c3p0-config>
<%@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 'index_book.jsp' starting page</title>
<meta content="no-cache" http-equiv="pragma">
<meta content="no-cache" http-equiv="cache-control">
<meta content="0" http-equiv="expires">
<meta content="keyword1,keyword2,keyword3" http-equiv="keywords">
<meta content="This is my page" http-equiv="description">
</head>
<body>
<a href="<%=request.getContextPath() %>/BookServlet">全部图书</a><br>
<a href="<%=request.getContextPath() %>/BookServlet?category=1">JavaSE分类</a><br>
<a href="<%=request.getContextPath() %>/BookServlet?category=2">JavaEE分类</a><br>
<a href="<%=request.getContextPath() %>/BookServlet?category=3">Java框架分类</a><br>
</body>
</html>
<%@page import="cn.itcast.domain.Book"%>
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'show.jsp' starting page</title>
<meta content="no-cache" http-equiv="pragma">
<meta content="no-cache" http-equiv="cache-control">
<meta content="0" http-equiv="expires">
<meta content="keyword1,keyword2,keyword3" http-equiv="keywords">
<meta content="This is my page" http-equiv="description">
<meta content="text/html;charset=utf-8" http-equiv="content-type">
<!--
<link rel="stylesheet" type="text/css" href="styles/css">
-->
</head>
<body>
<table boder="1" align="center" width="50%">
<tr>
<th>图书名称</th>
<th>图书单价</th>
<th>图书分类</th>
</tr>
<%
List<Book>list=(List)request.getAttribute("bookList");
for(Book b:list){
%>
<tr>
<td><%=b.getBname() %></td>
<td><%=b.getPrice() %></td>
<td>
<%if(b.getCategory()==1){ %>
<p style="color:red;">JavaSE分类</p>
<%}else if(b.getCategory()==2){ %>
<p style="color:blue;">JavaEE分类</p>
<%}else{ %>
<p style="color:green;">Java框架分类</p>
<%} %>
</td>
</tr>
<%} %>
</table>
</body>
</html>