您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > Java > Java程序设计_大作业
1Java程序设计_大作业专业:计算机科学与技术专业学号:1245713131姓名:2014年12月10日1目录作业内容:....................................................21.IPublisherDao接口:.................................32.IPublisherDao类:.....................................43.Publisher类:...........................................134.DataBaseConnection类:.........................155.TestFrame类:.........................................186.RunApplication类:.................................29具体运行结果:..........................................29总结:..........................................................332大作业:Java数据库程序设计作业内容:图书馆要进行出版商的统计,如下图所示。要求创建一个具有交互功能的用户接口的出版商信息应用程序,列表的信息从数据库查询,动态添加。该应用程序应该使管理人员能够从出版商表中添加、更新信息。要求使用PreparedStatement对象创建SQL语句。31.IPublisherDao接口:packagecom.zy.dao;importjava.util.List;importcom.zy.entity.Publisher;publicinterfaceIPublisherDao{publicbooleandoCreate(Publisherpublisher)throwsException;publicbooleandoUpdate(Publisherpublisher)throwsException;publicbooleandoDelete(Stringid)throwsException;publicPublisherfindById(Stringid)throwsException;4publicListPublisherfindAll(Stringkeyword)throwsException;publicListPublisherall()throwsException;}2.IPublisherDao类:packagecom.zy.dao;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.PreparedStatement;importjava.util.ArrayList;importjava.util.List;importcom.zy.entity.Publisher;importcom.zy.jdbc.DataBaseConnection;publicclassIPublisherDaoImplimplementsIPublisherDao{privateDataBaseConnectionjdbc=null;privateConnectioncon=null;publicIPublisherDaoImpl(){this.jdbc=newDataBaseConnection();this.con=this.jdbc.getConnection();}@Override5publicbooleandoCreate(Publisherpublisher)throwsException{booleanflag=false;PreparedStatementpstmt=null;ResultSetrs=null;Stringsql=INSERTINTOPublisher(Publisher_ID,Publisher_Name,Phone_Number,Address,City,State,Zip)VALUES(?,?,?,?,?,?,?);try{pstmt=this.con.prepareStatement(sql);pstmt.setString(1,publisher.getPublisherId());pstmt.setString(2,publisher.getPublisherName());pstmt.setString(3,publisher.getPhoneNum());pstmt.setString(4,publisher.getAddress());pstmt.setString(5,publisher.getCity());pstmt.setString(6,publisher.getState());pstmt.setString(7,publisher.getZip());if(pstmt.executeUpdate()0){flag=true;}}catch(Exceptione){throwe;6}finally{this.jdbc.closeAll(con,pstmt,rs);}returnflag;}@OverridepublicbooleandoUpdate(Publisherpublisher)throwsException{booleanflag=false;PreparedStatementpstmt=null;ResultSetrs=null;Stringsql=UPDATEPublisherSETPublisher_Name=?,Phone_Number=?,Address=?,City=?,State=?,Zip=?WHEREPublisher_ID=?;try{pstmt=this.con.prepareStatement(sql);pstmt.setString(1,publisher.getPublisherName());pstmt.setString(2,publisher.getPhoneNum());pstmt.setString(3,publisher.getAddress());pstmt.setString(4,publisher.getCity());pstmt.setString(5,publisher.getState());pstmt.setString(6,publisher.getZip());7pstmt.setString(7,publisher.getPublisherId());System.out.println(更新成功);if(pstmt.executeUpdate()0){flag=true;}}catch(Exceptione){throwe;}finally{this.jdbc.closeAll(con,pstmt,rs);}returnflag;}@OverridepublicbooleandoDelete(Stringid)throwsException{booleanflag=false;PreparedStatementpstmt=null;ResultSetrs=null;Stringsql=DELETEFROMPublisherWHEREPublish_ID=?;try{pstmt=this.con.prepareStatement(sql);pstmt.setString(1,id);8if(pstmt.executeUpdate()0){flag=true;}}catch(Exceptione){throwe;}finally{this.jdbc.closeAll(con,pstmt,rs);}returnflag;}@OverridepublicPublisherfindById(Stringid)throwsException{Publisherpublisher=null;PreparedStatementpstmt=null;ResultSetrs=null;Stringsql=SELECTPublisher_ID,Publisher_Name,Phone_Number,Address,City,State,ZipFROMpublisherWHEREPublisher_IDlike?;System.out.println(id);try{pstmt=this.con.prepareStatement(sql);System.out.println(*************);9//pstmt.setString(1,id);pstmt.setString(1,id);rs=pstmt.executeQuery();if(rs.next()){System.out.println(按照ID查询成功);publisher=newPublisher();publisher.setPublisherId(rs.getString(1));publisher.setPublisherName(rs.getString(2));publisher.setPhoneNum(rs.getString(3));publisher.setAddress(rs.getString(4));publisher.setCity(rs.getString(5));publisher.setState(rs.getString(6));publisher.setZip(rs.getString(7));}}catch(Exceptione){throwe;}finally{this.jdbc.closeAll(con,pstmt,rs);}returnpublisher;}@Override10publicListPublisherfindAll(Stringkeyword)throwsException{ListPublisherall=newArrayListPublisher();PreparedStatementpstmt=null;ResultSetrs=null;Stringsql=SELECTPublisher_ID,Publisher_Name,Phone_Number,Address,City,State,ZipFROMpublisherWHEREPublisher_IDlike?ORPublisher_Namelike?ORPhone_Numberlike?ORAddresslike?ORCitylike?ORStatelike?ORZiplike?;try{pstmt=this.con.prepareStatement(sql);pstmt.setString(1,%+keyword+%);pstmt.setString(2,%+keyword+%);pstmt.setString(3,%+keyword+%);pstmt.setString(4,%+keyword+%);pstmt.setString(5,%+keyword+%);pstmt.setString(6,%+keyword+%);pstmt.setString(7,%+keyword+%);rs=pstmt.executeQuery();while(rs.next()){Publisherpublisher=newPublisher();11publisher.setPublisherId(rs.getString(1));publisher.setPublisherName(rs.getString(2));publisher.setPhoneNum(rs.getString(3));p
本文标题:Java程序设计_大作业
链接地址:https://www.777doc.com/doc-3550497 .html