数据库系统应用与开发实验四Word文件下载.docx

上传人:b****6 文档编号:20818825 上传时间:2023-01-25 格式:DOCX 页数:14 大小:193.92KB
下载 相关 举报
数据库系统应用与开发实验四Word文件下载.docx_第1页
第1页 / 共14页
数据库系统应用与开发实验四Word文件下载.docx_第2页
第2页 / 共14页
数据库系统应用与开发实验四Word文件下载.docx_第3页
第3页 / 共14页
数据库系统应用与开发实验四Word文件下载.docx_第4页
第4页 / 共14页
数据库系统应用与开发实验四Word文件下载.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

数据库系统应用与开发实验四Word文件下载.docx

《数据库系统应用与开发实验四Word文件下载.docx》由会员分享,可在线阅读,更多相关《数据库系统应用与开发实验四Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。

数据库系统应用与开发实验四Word文件下载.docx

'

;

java.sql.Statementst=conn.createStatement();

//st.setString(1,p.getPubid());

java.sql.ResultSetrs=st.executeQuery(sql);

if(rs.next())thrownewBusinessException("

出版社编号已经被占用"

);

rs.close();

st.close();

sql="

select*fromBeanPublisherwherepublisherName='

+p.getPublisherName()+"

st=conn.createStatement();

//st.setString(1,p.getPublisherName());

rs=st.executeQuery(sql);

出版社名称已经存在"

insertintoBeanPublisher(pubid,publisherName,address)values('

'

+p.getAddress()+"

)"

//st.setString(1,p.getPubid());

//st.setString(2,p.getPublisherName());

//st.setString(3,p.getAddress());

st.execute(sql);

}catch(SQLExceptione){

e.printStackTrace();

thrownewDbException(e);

}

2、利用insert语句添加数据时,未指定字段值处理。

运行图书管理系统,打开读者类别管理界面,并尝试添加一个读者类别;

系统将会报一个错误,请分析说明错误原因。

reader.Typeid是主码,不能为空

 

通过数据库表结构的修改,解决上述问题。

并用同样的方式解决图书借阅功能的bug。

打开企业管理器;

打开beanreadertype表;

打开设计表;

将标识改成是;

第三步:

如果表结构不修改,应该如何修改程序,使新增读者类别的ID为表中现有数据的最大ID值+1。

publicvoidcreateReaderType(BeanReaderTypert)throwsBaseException{

if(rt.getReaderTypeName()==null||"

.equals(rt.getReaderTypeName())||rt.getReaderTypeName().length()>

20){

thrownewBusinessException("

读者类别名称必须是1-20个字"

if(rt.getLendBookLimitted()<

0||rt.getLendBookLimitted()>

100){

借阅图书数量必须在0-100之间"

select*fromBeanReaderTypewherereaderTypeName=?

java.sql.PreparedStatementpst=conn.prepareStatement(sql);

pst.setString(1,rt.getReaderTypeName());

java.sql.ResultSetrs=pst.executeQuery();

读者类别名称已经被占用"

pst.close();

sql="

selectmax(readerTypeId)fromBeanReadertype"

inti=1;

pst=conn.prepareStatement(sql);

rs=pst.executeQuery();

//id=rs.getint+1;

if(!

rs.next()){//是否已经有id

i=1;

}

else{

i+=rs.getInt

(1);

insertintoBeanReaderType(readerTypeId,readerTypeName,lendBookLimitted)values(?

?

pst.setInt(1,i);

pst.setString(2,rt.getReaderTypeName());

pst.setInt(3,rt.getLendBookLimitted());

pst.execute();

finally{

if(conn!

=null)

try{

conn.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

3、利用PreparedStatement对象进行数据修改。

在SystemUserManager类中,新建一个modifyUserName方法,实现用户名称(username字段)的修改功能。

并修改其main函数,将admin用户的名称改为:

超级管理员。

A、请提供方法代码和main函数代码。

publicvoidmodifyUserName(Stringusername)throwsBaseException{

Connectionconn=null;

try{

conn=DBUtil.getConnection();

Stringsql="

updatebeansystemusersetusername='

+username+"

whereuserid='

admin'

java.sql.Statementst=conn.createStatement();

intrs=st.executeUpdate(sql);

}catch(SQLExceptione){

e.printStackTrace();

thrownewDbException(e);

finally{

if(conn!

try{

conn.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

publicstaticvoidmain(String[]args){

BeanSystemUseruser=newBeanSystemUser();

user.setUserid("

admin"

user.setUsername("

系统管理员"

user.setUsertype("

管理员"

newSystemUserManager().modifyUserName("

超级管理员"

}catch(BaseExceptione){

//TODOAuto-generatedcatchblock

B、思考:

如果上述方法的返回值为布尔类型,即如果成功修改了用户名称,则返回true,如果用户不存在或修改失败返回false。

应该如何完善代码。

提示:

主要statement或PreparedStatement对象的execute方法和executeUpdate方法的区别。

publicstaticvoidmain(String[]args){

超级管理员1"

publicvoidmodifyUserName(Stringusername)throwsBaseException{

booleanrs=st.execute(sql);

if(rs==true)

System.out.print("

ok"

else

no"

4、Delete语句的执行。

修改用户管理类中的用户删除方法,用删除数据库表中数据的形式代替现有软删除模式。

A、修改后的sql语句部分是。

sql="

deleteBeanSystemUserwhereuserid=?

//deleteBeanSystemUserwhereuserID='

001'

B、如果对删除函数进行限制,要求不能删除已经有过借阅操作的用户。

应如何修改代码。

可参考读者管理类中的读者类别删除方法。

publicvoiddeleteUser(Stringuserid)throwsBaseException{

selectremoveDatefromBeanSystemUserwhereuserid=?

pst.setString(1,userid);

rs.next())thrownewBusinessException("

登陆账号不存在"

if(rs.getDate

(1)!

=null)thrownewBusinessException("

该账号已经被删除"

select*fromBeanBookLendRecordwherelendOperUserid=?

rs=pst.executeQuery();

rs.next()){

deleteBeanSystemUserwhereuserID=?

pst.setString(1,userid);

thrownewBusinessException("

该账号已有借阅信息,不能删除"

5、在数据库中建立一张BeanBookLendRecord_backup表,用于保存已经归还图书的借阅记录。

其表结构与BeanBookLendRecord表完全一致。

要求在借阅管理类中,增加方法,实现已经归还数据的备份功能(备份完成后,在原表中删除备份成功的数据)。

注意事务控制。

A请提供备份表的建表语句

select*intoBeanBookLendRecord_backupfromBeanBookLendRecord

B请提供备份函数代码

publicvoida()

{

java.sql.Connectioncon=null;

con=DBUtil.getConnection();

Stringsql="

insertintoBeanBookLendRecord_backupselect*fromBeanBookLendRecord"

java.sql.Statementst=con.createStatement();

java.sql.ResultSetrs=st.executeQuery(sql);

sql="

deletefromBeanBookLendRecord"

st=con.createStatement();

}catch(SQLExceptione)

{

finally

if(con!

=null)

{

con.close();

}catch(SQLExceptione)

{

6、如果需要记录图书的入库时间(需要包含时分秒),应如何修改数据库表结构和相关代码?

在bookbeak中添加:

localdate

publicvoidcreateBook(BeanBookb)throwsBaseException{

if(b.getBarcode()==null||"

.equals(b.getBarcode())||b.getBarcode().length()>

条码必须是1-20个字"

if(b.getBookname()==null||"

.equals(b.getBookname())||b.getBookname().length()>

50){

图书名称必须是1-50个字"

select*fromBeanBookwherebarcode=?

pst.setString(1,b.getBarcode());

条码已经被占用"

insertintoBeanBook(barcode,bookname,pubid,price,state,localdate)values(?

在库'

pst.setString(2,b.getBookname());

pst.setString(3,b.getPubid());

pst.setDouble(4,b.getPrice());

pst.setTimestamp(5,newjava.sql.Timestamp(System.currentTimeMillis()));

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1