数据库系统应用与开发实验四.docx

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

数据库系统应用与开发实验四.docx

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

数据库系统应用与开发实验四.docx

数据库系统应用与开发实验四

实验JDBC基础(3)

一、相关知识点

1、JDBC基本概念

2、JDBC数据增、删、改,事务控制等

二、实验目的:

理解Java连接数据库的基本概念。

理解利用Statement对象、PreparedStatement对象进行增、删、改操作,理解事务的概念和JDBC编程方式。

三、实验内容:

1、利用Statement对象进行数据添加。

第一步:

修改PublisherManager类的createPublisher方法,将其中的insert语言改成用Statement对象执行;

第二步:

运行图书管理系统,进行添加出版社测试。

【实验结果与分析】

A、写出替换的代码部分。

Connectionconn=null;

try{

conn=DBUtil.getConnection();

Stringsql="select*fromBeanPublisherwherepubid='"+p.getPubid()+"'";

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);

if(rs.next())thrownewBusinessException("出版社名称已经存在");

rs.close();

st.close();

sql="insertintoBeanPublisher(pubid,publisherName,address)values('"+p.getPubid()+"','"+p.getPublisherName()+"','"+p.getAddress()+"')";

st=conn.createStatement();

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

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

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

st.execute(sql);

st.close();

}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){

thrownewBusinessException("借阅图书数量必须在0-100之间");

}

Connectionconn=null;

try{

conn=DBUtil.getConnection();

Stringsql="select*fromBeanReaderTypewherereaderTypeName=?

";

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

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

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

if(rs.next())thrownewBusinessException("读者类别名称已经被占用");

rs.close();

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);

}

sql="insertintoBeanReaderType(readerTypeId,readerTypeName,lendBookLimitted)values(?

?

?

)";

pst=conn.prepareStatement(sql);

pst.setInt(1,i);

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

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

pst.execute();

rs.close();

pst.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewDbException(e);

}

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!

=null)

try{

conn.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

publicstaticvoidmain(String[]args){

BeanSystemUseruser=newBeanSystemUser();

user.setUserid("admin");

user.setUsername("系统管理员");

user.setUsertype("管理员");

try{

newSystemUserManager().modifyUserName("超级管理员");

}catch(BaseExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

B、思考:

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

应该如何完善代码。

提示:

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

publicstaticvoidmain(String[]args){

BeanSystemUseruser=newBeanSystemUser();

user.setUserid("admin");

user.setUsername("系统管理员");

user.setUsertype("管理员");

try{

newSystemUserManager().modifyUserName("超级管理员1");

}catch(BaseExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

publicvoidmodifyUserName(Stringusername)throwsBaseException{

Connectionconn=null;

try{

conn=DBUtil.getConnection();

Stringsql="updatebeansystemusersetusername='"+username+"'whereuserid='admin'";

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

booleanrs=st.execute(sql);

if(rs==true)

System.out.print("ok");

else

System.out.print("no");

}catch(SQLExceptione){

e.printStackTrace();

thrownewDbException(e);

}

finally{

if(conn!

=null)

try{

conn.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

 

4、Delete语句的执行。

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

【实验结果与分析】

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

sql="deleteBeanSystemUserwhereuserid=?

";//deleteBeanSystemUserwhereuserID='001'

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

应如何修改代码。

提示:

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

publicvoiddeleteUser(Stringuserid)throwsBaseException{

Connectionconn=null;

try{

conn=DBUtil.getConnection();

Stringsql="selectremoveDatefromBeanSystemUserwhereuserid=?

";

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

pst.setString(1,userid);

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

if(!

rs.next())thrownewBusinessException("登陆账号不存在");

if(rs.getDate

(1)!

=null)thrownewBusinessException("该账号已经被删除");

rs.close();

pst.close();

sql="select*fromBeanBookLendRecordwherelendOperUserid=?

";

pst=conn.prepareStatement(sql);

pst.setString(1,userid);

rs=pst.executeQuery();

if(!

rs.next()){

sql="deleteBeanSystemUserwhereuserID=?

";

pst=conn.prepareStatement(sql);

pst.setString(1,userid);

pst.execute();

pst.close();

}

else{

thrownewBusinessException("该账号已有借阅信息,不能删除");

}

}catch(SQLExceptione){

e.printStackTrace();

thrownewDbException(e);

}

finally{

if(conn!

=null)

try{

conn.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

 

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

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

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

提示:

注意事务控制。

【实验结果与分析】

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

select*intoBeanBookLendRecord_backupfromBeanBookLendRecord

 

B请提供备份函数代码

publicvoida()

{

java.sql.Connectioncon=null;

try{

con=DBUtil.getConnection();

Stringsql="insertintoBeanBookLendRecord_backupselect*fromBeanBookLendRecord";

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

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

sql="deletefromBeanBookLendRecord";

st=con.createStatement();

rs=st.executeQuery(sql);

}catch(SQLExceptione)

{

e.printStackTrace();

}

finally

{

if(con!

=null)

{

try{

con.close();

}catch(SQLExceptione)

{

e.printStackTrace();

}

}

}

}

 

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

【实验结果与分析】

在bookbeak中添加:

localdate

publicvoidcreateBook(BeanBookb)throwsBaseException{

if(b.getBarcode()==null||"".equals(b.getBarcode())||b.getBarcode().length()>20){

thrownewBusinessException("条码必须是1-20个字");

}

if(b.getBookname()==null||"".equals(b.getBookname())||b.getBookname().length()>50){

thrownewBusinessException("图书名称必须是1-50个字");

}

Connectionconn=null;

try{

conn=DBUtil.getConnection();

Stringsql="select*fromBeanBookwherebarcode=?

";

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

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

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

if(rs.next())thrownewBusinessException("条码已经被占用");

rs.close();

pst.close();

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

?

?

?

'在库',?

)";

pst=conn.prepareStatement(sql);

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

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

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

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

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

pst.execute();

pst.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewDbException(e);

}

finally{

if(conn!

=null)

try{

conn.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

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

当前位置:首页 > 小学教育 > 小升初

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

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