基于ServletJsp的网上书店设计(二)
生活随笔
收集整理的這篇文章主要介紹了
基于ServletJsp的网上书店设计(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
五,代碼解析
5.1數(shù)據(jù)表JavaBean
Book.java
package com.beans;public class Book {public static final int PAGE_SIZE=6;private int bookId;private String name;private String author;private String publisher;private String price;public Book(){}public Book(int bookId, String name,String author,String publisher,String price){this.bookId=bookId;this.name=name;this.author=author;this.publisher=publisher;this.price=price; }public int getBookId() {return bookId;}public void setBookId(int bookId) {this.bookId = bookId;}public String getName() {return name;} public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getPublisher() {return publisher;}public void setPublisher(String publisher) {this.publisher = publisher;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;} }User.java
package com.beans;public class User {private String ID;private String Password;private String Sex;private String Phone;private String Home;private String Email;private String Header;public User(){}public User(String ID,String Password, String Sex,String Phone,String Home,String Email,String Header){this.ID=ID;this.Password=Password;this.Sex=Sex;this.Phone=Phone;this.Home=Home;this.Email=Email; this.Header=Header;}public String getID() {return ID;}public void setID(String iD) {ID = iD;}public String getPassword() {return Password;}public void setPassword(String password) {Password = password;}public String getSex() {return Sex;}public void setSex(String sex) {Sex = sex;}public String getPhone() {return Phone;}public void setPhone(String phone) {Phone = phone;}public String getHome() {return Home;}public void setHome(String home) {Home = home;}public String getEmail() {return Email;}public void setEmail(String email) {Email = email;}public String getHeader() {return Header;}public void setHeader(String header) {Header = header;}}CartBook.java是用戶的購(gòu)物車表
package com.beans;public class CartBook {private int Id;private String name;private String price;private int num;private int total;public CartBook(){}public CartBook(int Id, String name,String price,int num,int total){this.Id=Id;this.name=name;this.price=price; this.num=num;this.total=total;}public int getBookId() {return Id;}public void setBookId(int 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;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public int getTotal() {return total;}public void setTotal(int total) {this.total = total;} }
5.2操作數(shù)據(jù)庫(kù)DAO
BookDao.java主要有兩個(gè)功能,:返回圖書列表,根據(jù)圖書Id返回這本書的信息
?
package com.Dao;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 com.beans.Book; import com.tools.DBConnection;public class BookDao {DBConnection DB=new DBConnection();Connection conn=null;//返回所有圖書列表public List<Book> getBookList(){List<Book> list=new ArrayList<Book>();try {conn=DB.getCon();String sql="select * from books";PreparedStatement pstm=conn.prepareStatement(sql);ResultSet rs=pstm.executeQuery();while(rs.next()){Book book=new Book();book.setBookId(rs.getInt(1));book.setName(rs.getString(2));book.setAuthor(rs.getString(3));book.setPublisher(rs.getString(4));book.setPrice(rs.getString(5));list.add(book);}return list;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}//根據(jù)圖書ID返回這本書的信息public Book getBookById(int bookid){Book book=new Book();try {conn=DB.getCon();String sql="select * from books where BookID=?";PreparedStatement pstm=conn.prepareStatement(sql);pstm.setInt(1, bookid);ResultSet rs=pstm.executeQuery();while(rs.next()){book.setBookId(rs.getInt(1));book.setName(rs.getString(2));book.setAuthor(rs.getString(3));book.setPublisher(rs.getString(4));book.setPrice(rs.getString(5)); }return book;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} return null;}}CartDao.java主要是對(duì)購(gòu)物車中的書籍進(jìn)行增刪查改操作
package com.Dao;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 com.beans.Book; import com.beans.CartBook; import com.tools.DBConnection;public class CartDao {DBConnection DB=new DBConnection();Connection conn=null;//獲得所有已買書籍public List<CartBook> getAllCartBooks(String userid){conn = DB.getCon(); //獲取數(shù)據(jù)庫(kù)連接 List<CartBook> list=new ArrayList<CartBook>();System.out.println("已經(jīng)進(jìn)入函數(shù)");if(conn!= null){ try { System.out.println(userid);String sql="select * from "+userid;System.out.println(sql);PreparedStatement pstm=conn.prepareStatement(sql);ResultSet rs=pstm.executeQuery();while(rs.next()){CartBook cb=new CartBook();cb.setBookId(rs.getInt(1));cb.setName(rs.getString(2));cb.setPrice(rs.getString(3));cb.setNum(rs.getInt(4));cb.setTotal(rs.getInt(5));list.add(cb);} } catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return list;}//插入書籍public boolean InsertBook(String userid,Book b){ conn = DB.getCon(); //獲取數(shù)據(jù)庫(kù)連接 //System.out.println(userid);if(conn!=null){try { String sql="insert into "+userid+" values(?,?,?,?,?)";System.out.println(sql);PreparedStatement pstm=conn.prepareStatement(sql); pstm.setInt(1, b.getBookId());//System.out.println(b.getName());pstm.setString(2, b.getName()); pstm.setString(3, b.getPrice());pstm.setInt(4, 1);pstm.setInt(5, Integer.parseInt(b.getPrice()));System.out.println("語(yǔ)句沒錯(cuò)");pstm.executeUpdate();return true; } catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} }return false; }//刪除已買書籍public boolean DeleteBook(String userid,int bookid){ conn = DB.getCon(); //獲取數(shù)據(jù)庫(kù)連接 if(conn!=null){ try {String sql="delete from "+userid+" where ID='"+bookid+"'";PreparedStatement pstm=conn.prepareStatement(sql);pstm.executeUpdate();return true;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return false;}//修改數(shù)量public boolean ModifyNum(String userid,int bookid,int num){conn = DB.getCon(); //獲取數(shù)據(jù)庫(kù)連接 int total=0;int oldnum=0;if(conn!=null){try{//獲得原來的數(shù)量String sql3="select Num from "+userid+" where ID='"+bookid+"'";PreparedStatement pstm3 = conn.prepareStatement(sql3);ResultSet rs=pstm3.executeQuery();if(rs.next()){oldnum=rs.getInt(1);}if(oldnum==1){if(num==-1){return true;}}//更新數(shù)量String sql="update "+userid+" set Num='"+(num+oldnum)+"' where ID='"+bookid+"'";PreparedStatement pstm = conn.prepareStatement(sql);pstm.executeUpdate();//計(jì)算總價(jià)String sql1="select Price from "+userid+" where ID='"+bookid+"'";PreparedStatement pstm1 = conn.prepareStatement(sql1);ResultSet rs1=pstm1.executeQuery();if(rs1.next()){total=Integer.parseInt(rs1.getString("Price"))*(num+oldnum);}//修改總價(jià)String sql2="update "+userid+" set Total='"+total+"' where ID='"+bookid+"'";PreparedStatement pstm2 = conn.prepareStatement(sql2);pstm2.executeUpdate(); } catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} }return false;}//書籍是否存在public boolean isContainBook(String userid,String bookid){conn = DB.getCon(); //獲取數(shù)據(jù)庫(kù)連接 if(conn!=null){ try { String sql="select * from "+userid+" where ID='"+bookid+"'";PreparedStatement pstm = conn.prepareStatement(sql);ResultSet rs= pstm.executeQuery();if(rs.next())//如果存在這本書return true;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else{System.out.println("創(chuàng)建連接失敗");} return false;}//返回書的總價(jià)格public int getTotalPrice(String userid){conn = DB.getCon(); //獲取數(shù)據(jù)庫(kù)連接 if(conn!=null){ try {String sql="select sum(Total) as total from "+userid;PreparedStatement pstm = conn.prepareStatement(sql);ResultSet rs= pstm.executeQuery();if(rs.next()){return rs.getInt(1);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return 0;}//清空所有書籍public boolean ClearCartBook(String userid){conn = DB.getCon(); //獲取數(shù)據(jù)庫(kù)連接 if(conn!=null){ try {String sql="delete from "+userid;PreparedStatement pstm=conn.prepareStatement(sql);pstm.executeUpdate();return true;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return false;} }PageQueryDao.java實(shí)現(xiàn)分頁(yè)查詢顯示書籍
5.3連接數(shù)據(jù)庫(kù)代碼
package com.tools; import java.sql.*; public class DBConnection { private Connection con; //定義數(shù)據(jù)庫(kù)連接類對(duì)象 private PreparedStatement pstm; private String user="root"; //連接數(shù)據(jù)庫(kù)用戶名 private String password="123456"; //連接數(shù)據(jù)庫(kù)密碼 private String driverName="com.mysql.jdbc.Driver"; //數(shù)據(jù)庫(kù)驅(qū)動(dòng) private String url="jdbc:mysql://localhost:3306/shoppingcart"; //連接數(shù)據(jù)庫(kù)的URL,后面的是為了防止插入數(shù)據(jù) 庫(kù)出現(xiàn)亂碼,?useUnicode=true&characterEncoding=UTF-8 //構(gòu)造函數(shù) public DBConnection(){} /**創(chuàng)建數(shù)據(jù)庫(kù)連接*/ public Connection getCon(){try{Class.forName("com.mysql.jdbc.Driver");}catch(ClassNotFoundException e){System.out.println("加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)失敗!");e.printStackTrace();}try {con=DriverManager.getConnection(url,user,password); //獲取數(shù)據(jù)庫(kù)連接} catch (SQLException e) {System.out.println("創(chuàng)建數(shù)據(jù)庫(kù)連接失敗!");con=null;e.printStackTrace();}return con; //返回?cái)?shù)據(jù)庫(kù)連接對(duì)象 } /***@功能:對(duì)數(shù)據(jù)庫(kù)進(jìn)行增、刪、改、查操作*@參數(shù):sql為SQL語(yǔ)句;params為Object數(shù)組,里面存儲(chǔ)的是為sql表示的SQL語(yǔ)句中"?"占位符賦值的數(shù)據(jù) */public void doPstm(String sql,Object[] params){if(sql!=null&&!sql.equals("")){if(params==null)params=new Object[0]; getCon();if(con!=null){try{ System.out.println(sql);pstm=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);for(int i=0;i<params.length;i++){pstm.setObject(i+1,params[i]);}pstm.execute();}catch(SQLException e){System.out.println("doPstm()方法出錯(cuò)!");e.printStackTrace();} } }} public ResultSet getRs() throws SQLException{return pstm.getResultSet(); }public int getCount() throws SQLException{return pstm.getUpdateCount(); }public void closed(){try{if(pstm!=null)pstm.close(); }catch(SQLException e){System.out.println("關(guān)閉pstm對(duì)象失敗!");e.printStackTrace();}try{if(con!=null){con.close();}}catch(SQLException e){System.out.println("關(guān)閉con對(duì)象失敗!");e.printStackTrace();}} }總結(jié)
以上是生活随笔為你收集整理的基于ServletJsp的网上书店设计(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于ServletJsp的网上书店设计(
- 下一篇: 基于ServletJsp的网上书店设计(