《DAOJavaBean结构.ppt》由会员分享,可在线阅读,更多相关《DAOJavaBean结构.ppt(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、3 Sept.2007 Neusoft ConfidentialJava WebJava Web技术技术Copyright 2008 By Neusoft Group.All rights reserved3 Sept.2008Confidentialbookshopbookshop演示演示项目项目bookshopbookshop项目项目的需求的需求bookshopbookshop项目项目的静态页面的静态页面bookshopbookshop项目项目的数据库设计的数据库设计bookshopbookshop项目项目的数据库编码的数据库编码3 Sept.2008Confidential1 1、改进、
2、改进1 1 前程序中存在的问题前程序中存在的问题?要改变当前数据库的地址,现有代码如何更改?要改变当前数据库的地址,现有代码如何更改?要改变当前数据库的名字,现有代码如何更改?要改变当前数据库的名字,现有代码如何更改?要改变当前数据库的用户名,现有代码如何更改?要改变当前数据库的用户名,现有代码如何更改?要改变当前数据库的密码名,现有代码如何更改?要改变当前数据库的密码名,现有代码如何更改?代码冗余(重复)、没有考虑到代码重用代码冗余(重复)、没有考虑到代码重用 解决之道:解决之道:将重复的代码(能重用的代码放到类的一个方法中)将重复的代码(能重用的代码放到类的一个方法中)将重复的代码(能重用
3、的代码放到类的一个方法中)将重复的代码(能重用的代码放到类的一个方法中)DBMain.javaDBMain.javaDBMain.javaDBMain.java 一、Java数据库编程改进3 Sept.2008Confidential DBMain.java DBMain.java的编写的编写3 Sept.2008Confidential 代码代码public class DBMain private Connection con=null;public PreparedStatement getPreparedStatement(String sqlStr)throws ClassNotFo
4、undException,SQLExceptionPreparedStatement pst=null;/加载数据库驱动加载数据库驱动Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);/获得数据库连接获得数据库连接if(con=null|con.isClosed()con=DriverManager.getConnection(jdbc:sqlserver:/localhost:1433;databaseName=bookshop,sa,sa);pst=con.prepareStatement(sqlStr);return
5、pst;public void releaes()throws SQLExceptionif(con!=null&!con.isClosed()con.close();3 Sept.2008Confidential2 2、改进、改进2 2 当前程序中存在的问题当前程序中存在的问题?代码凌乱,例如对BookBook表的每个操作都单独的放在一个类中,造成类的数量急剧增加,不便于管理。解决之道:解决之道:将每个表的所有操作的JDBC代码放在一个类的各个方法中,例如:3 Sept.2008Confidentialpublicpublic calsscalss BookDAOBookDAO public
6、 public ResultSetResultSet getAllBooksgetAllBooks()()publicpublic ResultSetResultSet getBookByID(intgetBookByID(int Id)Id)publicpublic voidvoid addBookaddBook(intint bookType_id,StirngbookType_id,Stirng book_ISBNbook_ISBN,StirngStirng book_namebook_name,String String book_authbook_auth,String,String
7、 book_publishbook_publish,float,float book_pricebook_price,String,String book_facebook_face )3 Sept.2008Confidential3 3、改进、改进3 3 前程序中存在的问题前程序中存在的问题 参数列表太长,很容易造成传参错误。解决之道:解决之道:DAO+JavaBeanDAO+JavaBean3 Sept.2008Confidentialpublic class Book private int book_id;private int bookType_id;private String b
8、ook_ISBN;private String book_name;private String book_auth;private String book_publish;private float book_price;private String book_face;public String getBook_auth()return book_auth;public void setBook_auth(String book_auth)this.book_auth=book_auth;public String getBook_face()return book_face;BookBo
9、ok的代码(的代码(JavaBeanJavaBean)3 Sept.2008Confidentialpublic void setBook_face(String book_face)this.book_face=book_face;public int getBook_id()return book_id;public void setBook_id(int book_id)this.book_id=book_id;public String getBook_ISBN()return book_ISBN;public void setBook_ISBN(String book_ISBN)th
10、is.book_ISBN=book_ISBN;public String getBook_name()return book_name;3 Sept.2008Confidentialpublic void setBook_name(String book_name)this.book_name=book_name;public float getBook_price()return book_price;public void setBook_price(float book_price)this.book_price=book_price;public String getBook_publ
11、ish()return book_publish;public void setBook_publish(String book_publish)this.book_publish=book_publish;public int getBookType_id()return bookType_id;public void setBookType_id(int bookType_id)this.bookType_id=bookType_id;3 Sept.2008Confidentialpublicpublic classclass BookDAO extendsextends DAOBase
12、publicpublic ArrayList getAllBook()throwsthrows ClassNotFoundException,SQLException ArrayList books=newnew ArrayList();/对SQL语句进行封装String sqlStr=select*from book;pst=dBMain.getPreparedStatement(sqlStr);/执行sql语句rs=pst.executeQuery();Book book=nullnull;whilewhile(rs.next()book=thisthis.assemble(rs);boo
13、ks.add(book);thisthis.release();returnreturn books;BookDAOBookDAO的代码的代码3 Sept.2008Confidentialpublic Book getBookById(int book_id)throws ClassNotFoundException,SQLException/对SQL语句进行封装String sqlStr=select*from book where book_id=?;pst=dBMain.getPreparedStatement(sqlStr);/参数指定pst.setInt(1,book_id);/执行
14、sql语句rs=pst.executeQuery();Book book=null;while(rs.next()book=this.assemble(rs);this.release();return book;3 Sept.2008Confidentialpublic void addBook(Book book)throws ClassNotFoundException,SQLException/对SQL语句进行封装String sqlStr=insert into book(bookType_ID,book_ISBN,book_name,book_auth,book_publish,b
15、ook_price,book_face)values(?,?,?,?,?,?,?);pst=dBMain.getPreparedStatement(sqlStr);/参数指定pst.setInt(1,book.getBookType_id();pst.setString(2,book.getBook_ISBN();pst.setString(3,book.getBook_name();pst.setString(4,book.getBook_auth();pst.setString(5,book.getBook_publish();pst.setFloat(6,book.getBook_pri
16、ce();pst.setString(7,book.getBook_face();/执行sql语句pst.executeUpdate();this.release();3 Sept.2008Confidentialpublic void modifyBook(Book book)throws ClassNotFoundException,SQLException/对SQL语句进行封装String sqlStr=update book set bookType_ID=?,book_ISBN=?,book_name=?,book_auth=?,book_publish=?,book_price=?
17、,book_face=?where book_id=?;pst=dBMain.getPreparedStatement(sqlStr);pst.setInt(1,book.getBookType_id();pst.setString(2,book.getBook_ISBN();pst.setString(3,book.getBook_name();pst.setString(4,book.getBook_auth();pst.setString(5,book.getBook_publish();pst.setFloat(6,book.getBook_price();pst.setString(
18、7,book.getBook_face();pst.setInt(8,book.getBook_id();/执行sql语句pst.executeUpdate();this.release();3 Sept.2008Confidentialpublic void deleteBook(int book_id)throws ClassNotFoundException,SQLException/对SQL语句进行封装String sqlStr=delete from book where book_id=?;pst=dBMain.getPreparedStatement(sqlStr);pst.se
19、tInt(1,book_id);pst.executeUpdate();this.release();public static Book assemble(ResultSet rs)throws SQLException Book book=new Book();book.setBook_id(rs.getInt(book_id);book.setBookType_id(rs.getInt(bookType_id);book.setBook_ISBN(rs.getString(book_ISBN);book.setBook_name(rs.getString(book_name);book.setBook_auth(rs.getString(book_auth);book.setBook_publish(rs.getString(book_publish);book.setBook_price(rs.getFloat(book_price);book.setBook_face(rs.getString(book_face);return book;3 Sept.2008Confidential4 4、当前结构、当前结构3 Sept.2008ConfidentialCopyright 2008 版权所有 东软集团