Web数据库学生实验报告
- 格式:doc
- 大小:187.00 KB
- 文档页数:26
Web数据库技术
教师实验指导手册及评分标准
院 系: 信息科学与技术学院
专 业: 信息管理与信息系统
班 级: 信A1321/22
任课教师: 张海
实验报告(一)
院系:信息学院 课程名称:Web数据库技术 日期:
班 级 姓 名
专 业 学 号 实 验 室
实验名称 Jdbc应用 成 绩 评 定
教 师 签 名
实验目的 1、 掌握数据库驱动的加载方式
2、 掌握connection对象的使用方法
3、 掌握statement对象使用方法
4、 掌握事务的处理机制
5、掌握数据持久层的设计
实验内容 connection对象、statement对象等应用
1、 请设计一个工程类通过配置文件如下db.properties来获得数据库连接的相关信息,并通过该配置文件获得数据库连接对象。(20分)
url=jdbc\:mysql\://127.0.0.1/\u5B66\u751F\u5E93
userName=admin
pwd=admin
public class connectionFactory {
public static Connection getConnection() throws SQLException{
}
}
请把getConnection()方法补全。要求设计合理规范,必须有截图。
答案:
}
2、 已知学生定义如下:
public class student {
private int id;
private String stuId;
private String name;
private String domCard;//楼栋宿舍号“31-507” private String bedNo;//床铺号
public student(String stuId, String name, String domCard, String bedNo)
{
super();
this.stuId = stuId;
= name;
this.domCard = domCard;
this.bedNo = bedNo;
}
public String toString(){
return "id="+id+";学号="+stuId+”;姓名=”+name+”;宿舍号=”+domCard+”;床铺号=”+bedNo;
}
//相应get、set方法省略
}
有一
class studentDatas{
public static ArrayList students=new
ArrayList();
static{
student stu=new student("会计A001121","张三","31栋908",1);
students.add(stu);
stu=new student("会计A001166","李四","31栋908",2);
students.add(stu);
stu=new student("会计A001177","王五","31栋807",4);
students.add(stu);
}
}
现要求
(1)根据student类建立一个学生表用来保存student类的相关属性。
(2)通过jdbc,将studentDatas的students集合中的所有学生保持到学生表中;
(3)通过jdbc,将学生表中所有的宿舍是” 31栋908”学生全部调整到“20栋371”宿舍;
(4)通过jdbc,删除"31栋807"床铺号是4的学生。
20分
3、 ResultSet对象操作
请将上题中宿舍号最后一位是”8”的学生信息全部显示出来。
4、请用perparedStatement对象保存如下:
student stu=new student("信息A110099","刘六","31栋818",8);
student对象的实例stu到数据中。
兴趣题:
1、分页功能的实现原理
方法一:JDBC实现
用JDBC从ResultSet中取出一个指定范围(offset, span)的数据,可以采用这样的方法。
PrepareStatement ps = con.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ps.setMaxRows(offset span);//最终读到第几条
rs = ps.executeQuery();
rs.absolute(offset);//定位到每页的开始记录处
while(rs.next())
// conn.prepareStatement(sql,游标类型,能否更新记录);
// 游标类型:
// ResultSet.TYPE_FORWORD_ONLY:只进游标
// ResultSet.TYPE_SCROLL_INSENSITIVE:可滚动。但是不受其他用户对数据库更改的影响。
// ResultSet.TYPE_SCROLL_SENSITIVE:可滚动。当其他用户更改数据库时这个记录也会改变。
// 能否更新记录:
// ResultSet.CONCUR_READ_ONLY,只读
// ResultSet.CONCUR_UPDATABLE,可更新
方法二:数据库的sql语言实现
Mysql 的limit SQL为
Select * from message where forum_id = ? and created_time > ? order
by created_time desc limit ?, ?
后面的两个limit ?, ? 分别为 offset, span。
Oracle的limit SQL为
select * from ( select row_.*, rownum rownum_ from (Select * from message
where forum_id = ? and created_time > ? order by created_time desc) row_ where rownum <= ?)
where rownum_ > ?后面的两个limit ?, ? 分别为 offset span, offset。
2、请执行在mysql中执行insert into 宿舍表(学号,姓名,宿舍,床铺) values (‘会计A001121’,’张三’,’24栋908’,1)两次。 请通过JDBC将宿舍表中学号重复的记录去掉,只保留其中的一条。
5、事务处理
请先在mysql中运行如下语句:
CREATE TABLE `账户表` (
`账户名` varchar(10) NOT NULL DEFAULT '',
`金额` double DEFAULT NULL,
PRIMARY KEY (`账户名`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of 账户表
-- ----------------------------
INSERT INTO 账户表 VALUES ('张三', '100');
INSERT INTO 账户表 VALUES ('李四', '200');
INSERT INTO 账户表 VALUES ('王五', '50');
请运行如下程序查看结果
(1)程序1:
public class trans1 {
/**
* @param args
*/
public static void main(String[] args) {
Connection conn1=null;
Statement stmn1=null;
try {
();
conn1.setAutoCommit(true);
stmn1=conn1.createStatement();
stmn1.executeUpdate("update 账户表 set 金额=金额+5 where 账户名='张三'");
System.out.println("update语句执行");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }finally{
if(stmn1!=null)
try {
stmn1.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(conn1!=null)
try {
conn1.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(2)程序2:
public class trans2 {
/**
* @param args
*/
public static void main(String[] args) {
Connection conn1=null;
Statement stmn1=null;
try {
conn1=connectionFactory.getConnection();
conn1.setAutoCommit(false);
nt();
stmn1.executeUpdate("update 账户表 set 金额=金额+5 where 账户名='张三'");
System.out.println("update语句执行");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(stmn1!=null)
try {
stmn1.close();