public static void main(String[] args){
BeanSystemUser user=new BeanSystemUser(); user.setUserid(\); user.setUsername(\系统管理员\); user.setUsertype(\管理员\); try {
new SystemUserManager().modifyUserName(\超级管理员\); } catch (BaseException e) { }
}
// TODO Auto-generated catch block e.printStackTrace();
B、
思考:如果上述方法的返回值为布尔类型,即如果成功修改了用户名称,则返回true,如果用户不存在或修改失败返回false。应该如何完善代码。提示:主要statement或PreparedStatement对象的execute方法和executeUpdate方法的区别。
public static void main(String[] args){ }
public void modifyUserName(String username)throws BaseException{ Connection conn=null; }
try {
conn=DBUtil.getConnection();
String sql = \java.sql.Statement st=conn.createStatement(); boolean rs = st.execute(sql); if(rs == true)
BeanSystemUser user=new BeanSystemUser(); user.setUserid(\); user.setUsername(\系统管理员\); user.setUsertype(\管理员\);
try {
new SystemUserManager().modifyUserName(\超级管理员1\); } catch (BaseException e) { }
// TODO Auto-generated catch block e.printStackTrace();
'\+username+\;
System.out.print(\); else
System.out.print(\); } catch (SQLException e) { }
finally{ }
if(conn!=null)
try { }
conn.close();
// TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); throw new DbException(e);
4、 Delete语句的执行。修改用户管理类中的用户删除方法,用删除数据库表中数据的形
式代替现有软删除模式。
【实验结果与分析】 A、修改后的sql语句部分是。
sql=\BeanSystemUser where userid=?\; //delete BeanSystemUser where userID='001'
B、如果对删除函数进行限制,要求不能删除已经有过借阅操作的用户。应如何修改代码。提示:可参考读者管理类中的读者类别删除方法。
public void deleteUser(String userid)throws BaseException{
Connection conn=null; try {
conn=DBUtil.getConnection();
String sql=\java.sql.PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1,userid);
java.sql.ResultSet rs=pst.executeQuery();
if(!rs.next()) throw new BusinessException(\登陆账号不 存在\); if(rs.getDate(1)!=null) throw new BusinessException(\该账号
userid=?\;
已经被删除\); rs.close();
}
finally{
if(conn!=null)
try { pst.close();
sql=\pst=conn.prepareStatement(sql); pst.setString(1,userid); rs=pst.executeQuery(); if(!rs.next()){
lendOperUserid=?\;
sql=\; pst=conn.prepareStatement(sql); pst.setString(1, userid); pst.execute(); pst.close(); } else{
throw new BusinessException(\该账号已有借阅信息,不能删除\); }
e.printStackTrace(); throw new DbException(e);
} catch (SQLException e) {
}
}
}
conn.close();
// TODO Auto-generated catch block e.printStackTrace();
} catch (SQLException e) {
5、 在数据库中建立一张BeanBookLendRecord_backup表,用于保存已经归还图书的借阅
记录。其表结构与BeanBookLendRecord表完全一致。要求在借阅管理类中,增加方法,实现已经归还数据的备份功能(备份完成后,在原表中删除备份成功的数据)。提示:注意事务控制。
【实验结果与分析】
A 请提供备份表的建表语句
select * into BeanBookLendRecord_backup from BeanBookLendRecord
B 请提供备份函数代码
public void a() {
java.sql.Connection con = null;
try {
con = DBUtil.getConnection();
String sql = \BeanBookLendRecord_backup select * java.sql.Statement st = con.createStatement();
from BeanBookLendRecord\;
java.sql.ResultSet rs = st.executeQuery(sql); sql=\; st=con.createStatement(); rs=st.executeQuery(sql); }catch(SQLException e) { } finally { }
if( con != null) { }
try {
con.close();
} catch (SQLException e) { }
e.printStackTrace();
e.printStackTrace();
}
6、 如果需要记录图书的入库时间(需要包含时分秒),应如何修改数据库表结构和相关
代码?
【实验结果与分析】