新建一个dao类和一个book类
Dao类:
package cn.edu.tzc.dao;
import java.sql.*;
import java.util.ArrayList;
import cn.edu.tzc.bean.books;
public class BookDao {
public ArrayList queryAllBooks ()throws Exception{
Connection conn=null;
ArrayList books=new ArrayList();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=books","sa","123456");
Statement stat = conn.createStatement();
String sql="SELECT * FROM T_BOOK";
ResultSet rs=stat.executeQuery(sql);
while(rs.next()){
books book1=new books();
book1.setId(rs.getString("Id"));
book1.setName(rs.getString("Name"));
book1.setPrice(rs.getString("Price"));
books.add(book1);
}
rs.close();
stat.close();
}
catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null)
{
conn.close();
conn=null;
}
}
return books;
}
}
Book类:
package cn.edu.tzc.bean;
public class books {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
private String price;
}
books.java(selvert):
package Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpSession;
public class books extends HttpServlet {
public void SQLFind(HttpServletRequest request, HttpServletResponse response) {
HttpSession s=request.getSession();
response.setContentType("text/html;charset=UTF-8");
Connection conn=null;
Statement stat=null;
String name=request.getParameter("name");
String sql= "SELECT * FROM T_BOOK WHERE Name LIKE '%"+name+"%';";
try {
Writer writer=response.getWriter();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=books","sa","123456");
stat = conn.createStatement();
ResultSet rs=stat.executeQuery(sql);
int f=0;
while(rs.next()) {
f=1;
String s1=rs.getString("Id");
String s2=rs.getString("Name");
String s3=rs.getString("Price");
writer.write("
"+""
+""
+"Id:"+s1
+" Name:"+s2+" Price:"+s3
+"
");}
if(f==0)
{
writer.write("没有找到书本!");
}
stat.close();
conn.close();
} catch (Exception e) {
System.out.println(e);
// TODO: handle exception
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
Writer writer=response.getWriter();
SQLFind(request,response);
writer.write("
"+"
"
+""
+ "
");}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
Search.jsp:
pageEncoding="UTF-8"%>
BookDao dao=new BookDao();
ArrayList books= dao.queryAllBooks();
for(int i=0;i
{
books book2=(books)books.get(i);
session.setAttribute(book2.getName(),0);
} %>
请输入书名:
index.jsp:
BookDao dao=new BookDao();
ArrayList books= dao.queryAllBooks();
String id=request.getParameter("optionid");
if(id.equals("0")){
int[] a=new int[1000];
String[] bookname=new String[1000];
for(int i=0;i
{books book2=(books)books.get(i);
bookname[i]=book2.getName();}
for(int i=0;i
a[i]=Integer.parseInt(session.getAttribute(bookname[i]).toString());
}
for(int i=0;i
%>
id:书名:
书本数量:本
}
}
else if(id.equals("-1")){
String s=request.getParameter("bookid");
int b=Integer.parseInt(session.getAttribute(s).toString());
session.setAttribute(s,b+1);
%>
添加成功!
}
else{
String s=request.getParameter("bookid");
System.out.print(s);
int c=Integer.parseInt(session.getAttribute(s).toString());
if(c-1>0)
session.setAttribute(s,c-1);
else
session.setAttribute(s,0);
%>
删除成功!
}
%>