Java程序设计大作业.docx
《Java程序设计大作业.docx》由会员分享,可在线阅读,更多相关《Java程序设计大作业.docx(27页珍藏版)》请在冰豆网上搜索。
Java程序设计大作业
Java程序设计_大作业
专业:
计算机科学与技术专业
学号:
1245713131
姓名:
2014年12月10日
大作业:
Java数据库程序设计
作业内容:
图书馆要进行出版商的统计,如下图所示。
要求创建一个具有交互功能的用户接口的出版商信息应用程序,列表的信息从数据库查询,动态添加。
该应用程序应该使管理人员能够从出版商表中添加、更新信息。
要求使用PreparedStatement对象创建SQL语句。
1.IPublisherDao接口:
packagecom.zy.dao;
importjava.util.List;
importcom.zy.entity.Publisher;
publicinterfaceIPublisherDao{
publicbooleandoCreate(Publisherpublisher)throwsException;
publicbooleandoUpdate(Publisherpublisher)throwsException;
publicbooleandoDelete(Stringid)throwsException;
publicPublisherfindById(Stringid)throwsException;
publicListfindAll(Stringkeyword)throwsException;
publicListall()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();
}
@Override
publicbooleandoCreate(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;
}finally{
this.jdbc.closeAll(con,pstmt,rs);
}
returnflag;
}
@Override
publicbooleandoUpdate(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());
pstmt.setString(7,publisher.getPublisherId());
System.out.println("更新成功");
if(pstmt.executeUpdate()>0){
flag=true;
}
}catch(Exceptione){
throwe;
}finally{
this.jdbc.closeAll(con,pstmt,rs);
}
returnflag;
}
@Override
publicbooleandoDelete(Stringid)throwsException{
booleanflag=false;
PreparedStatementpstmt=null;
ResultSetrs=null;
Stringsql="DELETEFROMPublisherWHEREPublish_ID=?
";
try{
pstmt=this.con.prepareStatement(sql);
pstmt.setString(1,id);
if(pstmt.executeUpdate()>0){
flag=true;
}
}catch(Exceptione){
throwe;
}finally{
this.jdbc.closeAll(con,pstmt,rs);
}
returnflag;
}
@Override
publicPublisherfindById(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("*************");
//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;
}
@Override
publicListfindAll(Stringkeyword)throwsException{
Listall=newArrayList();
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();
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));
all.add(publisher);
}
}catch(Exceptione){
throwe;
}finally{
this.jdbc.closeAll(con,pstmt,rs);
}
returnall;
}
@Override
publicListall()throwsException{
Listall=newArrayList();
PreparedStatementpstmt=null;
ResultSetrs=null;
Stringsql="SELECTPublisher_ID,Publisher_Name,Phone_Number,Address,City,State,ZipFrompublisher";
try{
pstmt=this.con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
Publisherpublisher=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));
all.add(publisher);
}
}catch(Exceptione){
throwe;
}finally{
this.jdbc.closeAll(con,pstmt,rs);
}
returnall;
}
}
3.Publisher类:
packagecom.zy.entity;
publicclassPublisher{
privateStringpublisherId;
privateStringpublisherName;
privateStringphoneNum;
privateStringaddress;
privateStringcity;
privateStringstate;
privateStringzip;
publicStringgetPublisherId(){
returnpublisherId;
}
publicvoidsetPublisherId(StringpublisherId){
this.publisherId=publisherId;
}
publicStringgetPublisherName(){
returnpublisherName;
}
publicvoidsetPublisherName(StringpublisherName){
this.publisherName=publisherName;
}
publicStringgetPhoneNum(){
returnphoneNum;
}
publicvoidsetPhoneNum(StringphoneNum){
this.phoneNum=phoneNum;
}
publicStringgetAddress(){
returnaddress;
}
publicvoidsetAddress(Stringaddress){
this.address=address;
}
publicStringgetCity(){
returncity;
}
publicvoidsetCity(Stringcity){
this.city=city;
}
publicStringgetState(){
returnstate;
}
publicvoidsetState(Stringstate){
this.state=state;
}
publicStringgetZip(){
returnzip;
}
publicvoidsetZip(Stringzip){
this.zip=zip;
}
}
4.DataBaseConnection类:
packagecom.zy.jdbc;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ResourceBundle;
publicclassDataBaseConnection{
privateConnectioncon;
//获取连接的方法
publicDataBaseConnection(){
//读取数据库配置文件
ResourceBundlebundle=ResourceBundle.getBundle("ConnDB");
StringmyDriver=bundle.getString("driver");
Stringurl=bundle.getString("url");
Stringusername=bundle.getString("username");
Stringpwd=bundle.getString("pwd");
try{
//加载驱动
Class.forName(myDriver);
//获取链接
con=DriverManager.getConnection(url,username,pwd);
System.out.println("成功!
");
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
System.out.println("失败1");
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
System.out.println("失败2");
e.printStackTrace();
}
}
publicConnectiongetConnection(){
returnthis.con;
}
/**
*关闭连接
*
*@paramcon
*连接
*@parampstmt
*处理器
*@paramrs
*结果集
*/
publicvoidcloseAll(Connectioncon,Statementpstmt,java.sql.ResultSetrs){
try{
if(rs!
=null){
rs.close();
rs=null;
}
if(pstmt!
=null){
pstmt.close();
pstmt=null;
}
if(con!
=null){
con.close();
con=null;
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
5.TestFrame类:
packagecom.zy.ui;
importjava.awt.BorderLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItemListener;
importjava.util.Vector;
importjavax.swing.BorderFactory;
importjavax.swing.Box;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importcom.zy.dao.IPublisherDaoImpl;
importcom.zy.entity.Publisher;
publicclassTestFrameextendsJFrameimplementsActionListener,ItemListener{
privateJButtoninsertJButton,updateJButton,clearJButton,exitJButton;
pri