java编程一

新建一个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("<form action=\"index.jsp\">"
							+"<input type=\"hidden\" name=\"bookid\" value=\""+s2+"\">"
							+"<input type=\"hidden\" name=\"optionid\" value=\"-1\">"
								+"Id:"+s1
								+"  Name:"+s2+"  Price:"+s3
						+"<input type=\"submit\" value=\"添加\"></form>");
					}
				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("<form action=\"index.jsp\">"
			+"<input type=\"hidden\" name=\"bookid\" value=\"\"><br>"
			+"<input type=\"hidden\" name=\"optionid\" value=\"0\">"
			+ "<input type=\"submit\" value=\"显示购物车\"></form>");
			
		}
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			
		}
}

  

Search.jsp:

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="cn.edu.tzc.bean.books"%>
<%@page import="cn.edu.tzc.dao.BookDao"%>
<%@page import="java.io.Writer"%>
<html>
<body>
<% 
    BookDao dao=new BookDao();
  ArrayList books=  dao.queryAllBooks();
   
     for(int i=0;i<books.size();i++)
     {
     books book2=(books)books.get(i);
    session.setAttribute(book2.getName(),0);
     } %>
     <form action="books">
请输入书名:<br>
<input type ="text" name="name"><br>
<input type ="submit" value="查询"><input type="reset" value="重置">
</form>
</body>
</html>

  

index.jsp:

<%@page import="cn.edu.tzc.bean.books"%>
<%@page import="cn.edu.tzc.dao.BookDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 
  
  <body>
  <%
    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.size();i++)
	 {books book2=(books)books.get(i);
	bookname[i]=book2.getName();}
	for(int i=0;i<books.size();i++){
		a[i]=Integer.parseInt(session.getAttribute(bookname[i]).toString());
	}
	for(int i=0;i<books.size();i++){
	
		%>
		<form action="index.jsp">
		id:<%=i+1 %>书名:<%=bookname[i]%>
		<input type="hidden" name="bookid" value="<%=bookname[i]%>">
		<input type="hidden" name="optionid" value="1">
		<input type="submit" value="删除" > <br>
		书本数量:<%=a[i]%>本<br>
		</form>
		<%
	}
}
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);
	%>
	删除成功!
	<form>
	<input type="hidden" name="bookid" value="">
	<input type="hidden" name="optionid" value="0">
	<input type="submit" value="确认">
	</form>
	<%
}
%>
   
  </body>
</html>

  

上一篇:springmvc异常


下一篇:Multiple Books多账薄