2023年8月3日发(作者:)

实验 JDBC基础(综合)

一、相关知识点

1、JDBC基本概念

2、元数据

二、实验目的:

理解元数据的概念,利用jdbc完成复杂业务逻辑。

三、实验内容:

1、 在booklib工程的DBUtil类中增加main函数,并在main函数中编写代码,列出booklib数据库中所有表的名字。

提示:会用到如下代码片段,请自行测试resultset中那一列为表名

【实验结果与分析】

A、写出代码部分。

public static void main(String[] args) throws SQLException {

DatabaseMetaData dbmd = null;

Connection conn = null;

conn = nection();

dbmd = aData();

String types[] = {"VIEW","TABLE"};

ResultSet rs = les(null, null, "%", types);

while(())

{

}

(ing("TABLE_NAME")+"t"); }

2、在上述main函数中,增加代码,提取特定表中的字段信息,要求输出字段名字和数据类型。

提示:除了用课件中的方法,还可以用如下代码片段:

A、写出代码部分。

}

public static void main(String[] args) throws SQLException {

DatabaseMetaData dbmd = null;

Connection conn = null;

conn = nection();

dbmd = aData();

//String types[] = {"VIEW","TABLE"};

ResultSet rs = umns(null, "%", "beanbook","%");

while(())

{

}

(ing("COLUMN_NAME")+" ");

n(ing("TYPE_NAME"));

3、在BookLendManager类中设计函数public void calPenal();完成如下功能:提取所有超期未归还的借阅记录,输出其读者id、图书条码、借阅时间、超期天数(假设用户仅能借阅30天,请自行将数据库表中的已有数据的借阅时间往前调);计算每条数据的罚金(罚金算法为超期30天内每天0.1元,31-60天每天0.15元,60天以上每天0.2元),并写入数据库表的penalSum字段中。函数编写完成后,通过main函数进行测试验证。

注:请注意事务控制

【实验结果与分析】

A、 请提供程序代码。

public void calPenal() throws DbException {

List result=new ArrayList();

Connection conn=null;

try {

conn=nection();

String sql="select

readerid,bookBarcode,lendDate,TIMESTAMPDIFF(DAY,lendDate,now()) from

BeanBookLendRecord where TIMESTAMPDIFF(DAY,lendDate,now())>30 and returndate

is null";

edStatement pst=eStatement(sql);

Set rs=eQuery();

while(()){

String readerid = ing(1);

String bookBarcode = ing(2);

lendDate = (e(3));

int days = (4)-30;

double penalSum = -1.0;

if(days<=30){

}

else if(days>30 && days<=60){

}

else if(days>60){

}

n(readerid);

n(bookBarcode);

n(lendDate);

n(penalSum+"n");

sql="update beanbooklendrecord set penalSum=? where readerid=?

edStatement pst2=eStatement(sql);

ble(1, penalSum);

ing(2, readerid);

ing(3, bookBarcode);

penalSum = 0.2*(days-60)+4.5+3;

penalSum = 0.15*(days-30)+3;

penalSum = 0.1*days;

and bookBarcode=?";

}

}

}

e();

();

();

();

tackTrace();

throw new DbException(e);

} catch (SQLException e) {

finally{

}

if(conn!=null)

try {

}

();

// TODO Auto-generated catch block

tackTrace();

} catch (SQLException e) {

public static void main(String[] args) throws DbException{

}

BookLendManager b = new BookLendManager();

al();

4、在BookLendManager类中设计函数public void stopReader(),完成如下功能:停止总罚金超过10元的读者服务(将其stopDate字段值设为当前时间,stopUserId值设为任意一个管理员)。

要求:(1)任意一个管理员的账号需要通过sql语句查询出来,不能为常量

(2)统计读者罚金时,仅统计未归还的图书罚金

(3)再编写函数public void stopReader2(),要求采用不同的方法(一种方法是找到相应的读者一个一个的停止服务,另一种方式是通过一个sql语句完成设置;注意第一种方法时需要进行事务控制)

【实验结果与分析】

A、请给出两个函数的代码。

public void stopReader() {

Connection conn=null;

String ad = null;

try {

conn=nection();

oCommit(false);

String sql="select userid from beansystemuser WHERE usertype like

ent st=Statement();

Set rs= eQuery(sql);

if(()) ad=ing(1);

sql="select readerid,penalSum from beanbooklendrecord where

ent st2=Statement();

Set rs2= eQuery(sql);

while(()) {

}

sql="update beanreader set stopDate=? WHERE readerid=?";

edStatement pst=eStatement(sql);

estamp(1, new

ing(2, ing(1));

eUpdate();

sql="update beanreader set stopUserId=? WHERE readerid=?";

edStatement pst2=eStatement(sql);

ing(1, ad);

ing(2, ing(1));

eUpdate();

"管理员"";

// ();

penalSum>10 and returnDate is null";

// ();

amp(tTimeMillis()));

// ();

// ();

}

}

();

();

tackTrace();

}catch (SQLException e) {

finally{

}

if(conn!=null)

try {

}

();

// TODO Auto-generated catch block

tackTrace();

} catch (SQLException e) {

public void stopReader2() {

Connection conn=null;

String ad = null;

try {

conn=nection();

oCommit(false);

String sql="select userid from beansystemuser WHERE usertype like

ent st=Statement();

Set rs= eQuery(sql);

if(()) ad=ing(1);

sql="update beanreader set stopDate=?, stopUserId=? rn" +

"where readerid in (rn" +

" select readerid from beanbooklendrecord GROUP BY readerid

"管理员"";

HAVING sum(penalsum)>10 and rn" +

}

}

" readerid in {rn" +

" select readerid from beanbooklendrecord WHERE

returndate is not nullrn" +

")rn" +

")";

edStatement pst=eStatement(sql);

estamp(1, new

ing(2, ad);

eUpdate();

();

();

tackTrace();

amp(tTimeMillis()));

}catch (SQLException e) {

2023年8月3日发(作者:)

实验 JDBC基础(综合)

一、相关知识点

1、JDBC基本概念

2、元数据

二、实验目的:

理解元数据的概念,利用jdbc完成复杂业务逻辑。

三、实验内容:

1、 在booklib工程的DBUtil类中增加main函数,并在main函数中编写代码,列出booklib数据库中所有表的名字。

提示:会用到如下代码片段,请自行测试resultset中那一列为表名

【实验结果与分析】

A、写出代码部分。

public static void main(String[] args) throws SQLException {

DatabaseMetaData dbmd = null;

Connection conn = null;

conn = nection();

dbmd = aData();

String types[] = {"VIEW","TABLE"};

ResultSet rs = les(null, null, "%", types);

while(())

{

}

(ing("TABLE_NAME")+"t"); }

2、在上述main函数中,增加代码,提取特定表中的字段信息,要求输出字段名字和数据类型。

提示:除了用课件中的方法,还可以用如下代码片段:

A、写出代码部分。

}

public static void main(String[] args) throws SQLException {

DatabaseMetaData dbmd = null;

Connection conn = null;

conn = nection();

dbmd = aData();

//String types[] = {"VIEW","TABLE"};

ResultSet rs = umns(null, "%", "beanbook","%");

while(())

{

}

(ing("COLUMN_NAME")+" ");

n(ing("TYPE_NAME"));

3、在BookLendManager类中设计函数public void calPenal();完成如下功能:提取所有超期未归还的借阅记录,输出其读者id、图书条码、借阅时间、超期天数(假设用户仅能借阅30天,请自行将数据库表中的已有数据的借阅时间往前调);计算每条数据的罚金(罚金算法为超期30天内每天0.1元,31-60天每天0.15元,60天以上每天0.2元),并写入数据库表的penalSum字段中。函数编写完成后,通过main函数进行测试验证。

注:请注意事务控制

【实验结果与分析】

A、 请提供程序代码。

public void calPenal() throws DbException {

List result=new ArrayList();

Connection conn=null;

try {

conn=nection();

String sql="select

readerid,bookBarcode,lendDate,TIMESTAMPDIFF(DAY,lendDate,now()) from

BeanBookLendRecord where TIMESTAMPDIFF(DAY,lendDate,now())>30 and returndate

is null";

edStatement pst=eStatement(sql);

Set rs=eQuery();

while(()){

String readerid = ing(1);

String bookBarcode = ing(2);

lendDate = (e(3));

int days = (4)-30;

double penalSum = -1.0;

if(days<=30){

}

else if(days>30 && days<=60){

}

else if(days>60){

}

n(readerid);

n(bookBarcode);

n(lendDate);

n(penalSum+"n");

sql="update beanbooklendrecord set penalSum=? where readerid=?

edStatement pst2=eStatement(sql);

ble(1, penalSum);

ing(2, readerid);

ing(3, bookBarcode);

penalSum = 0.2*(days-60)+4.5+3;

penalSum = 0.15*(days-30)+3;

penalSum = 0.1*days;

and bookBarcode=?";

}

}

}

e();

();

();

();

tackTrace();

throw new DbException(e);

} catch (SQLException e) {

finally{

}

if(conn!=null)

try {

}

();

// TODO Auto-generated catch block

tackTrace();

} catch (SQLException e) {

public static void main(String[] args) throws DbException{

}

BookLendManager b = new BookLendManager();

al();

4、在BookLendManager类中设计函数public void stopReader(),完成如下功能:停止总罚金超过10元的读者服务(将其stopDate字段值设为当前时间,stopUserId值设为任意一个管理员)。

要求:(1)任意一个管理员的账号需要通过sql语句查询出来,不能为常量

(2)统计读者罚金时,仅统计未归还的图书罚金

(3)再编写函数public void stopReader2(),要求采用不同的方法(一种方法是找到相应的读者一个一个的停止服务,另一种方式是通过一个sql语句完成设置;注意第一种方法时需要进行事务控制)

【实验结果与分析】

A、请给出两个函数的代码。

public void stopReader() {

Connection conn=null;

String ad = null;

try {

conn=nection();

oCommit(false);

String sql="select userid from beansystemuser WHERE usertype like

ent st=Statement();

Set rs= eQuery(sql);

if(()) ad=ing(1);

sql="select readerid,penalSum from beanbooklendrecord where

ent st2=Statement();

Set rs2= eQuery(sql);

while(()) {

}

sql="update beanreader set stopDate=? WHERE readerid=?";

edStatement pst=eStatement(sql);

estamp(1, new

ing(2, ing(1));

eUpdate();

sql="update beanreader set stopUserId=? WHERE readerid=?";

edStatement pst2=eStatement(sql);

ing(1, ad);

ing(2, ing(1));

eUpdate();

"管理员"";

// ();

penalSum>10 and returnDate is null";

// ();

amp(tTimeMillis()));

// ();

// ();

}

}

();

();

tackTrace();

}catch (SQLException e) {

finally{

}

if(conn!=null)

try {

}

();

// TODO Auto-generated catch block

tackTrace();

} catch (SQLException e) {

public void stopReader2() {

Connection conn=null;

String ad = null;

try {

conn=nection();

oCommit(false);

String sql="select userid from beansystemuser WHERE usertype like

ent st=Statement();

Set rs= eQuery(sql);

if(()) ad=ing(1);

sql="update beanreader set stopDate=?, stopUserId=? rn" +

"where readerid in (rn" +

" select readerid from beanbooklendrecord GROUP BY readerid

"管理员"";

HAVING sum(penalsum)>10 and rn" +

}

}

" readerid in {rn" +

" select readerid from beanbooklendrecord WHERE

returndate is not nullrn" +

")rn" +

")";

edStatement pst=eStatement(sql);

estamp(1, new

ing(2, ad);

eUpdate();

();

();

tackTrace();

amp(tTimeMillis()));

}catch (SQLException e) {