-
南昌航空大学实验报告二
二
0
一四
年
三
月
十九
日
课程名称:
JA
V
A WEB
程序设计
实验名称:
p>
JavaBean
和
Servlet
完成增删改查
班级:
11
微软班
姓名:
林平君
同组人:
指导教师评定:
林振荣
签名:
一、
实验目的
完成一个数据列表的增删改
查功能的实现,以自己创建的表格为例,也可以自己设计数据库的表。通
过本实验能够熟
悉和掌握
jsp
中的数据库
CRUD<
/p>
操作。
二、
实验要求
本实验要求通过
javabean
来实现数据的存取和表示,
通过
servlet
来进行访问的控制。
给
出主要运行的界面
和核心代码,并描述系统实现的核心思想。
三、
实验步骤
1
.
实现<
/p>
javaBean
对数据库的操作。
2
.
创建<
/p>
jsp
页面
3
.
利用<
/p>
servlet
对页面对业务流程就行控制。
四、
参考源代码
1.
连接数据库
public
Connection
getConnection()
throws
SQLException,InstantiationException,
IllegalAccessEx
ception,ClassNotFoundException{
Connection
connection=
null
;
Class.
forName
(
).newInst
ance();
String
urlString=
;
String
passwordString=
;
String
userString=
;
connection=DriverManager.
p>
getConnection
(urlString,userSt
ring,passwordS
tring);
return
connection;
}
解释:获取一个数据源,用于连接数据库。
< br>2.
利用传入的
sql
语句查找
public
ArrayList
throws
Exception{
ArrayList
new
ArrayList
Connection
connection=
null
;
Statement
statement=
null
;
ResultSet
resultSet=
null
;
try
{
connection=getConnection();
statement=Statement();
resultSet=eQuery(sql);
while
(())
{
student stu=
new
student();
((
));
o(ing(
));
me(ing(
));
x(ing(
));
anage((
));
inTimes((
));
p>
inLastTime(ing(
));
sword((
));
e((
));
(stu);
}
}
catch
(SQLException e) {
throw
new
SQLException(
+sage());
}
catch
(Exception
exception){
throw
new
Exception(
exception:
+sage());
}
finally
{
try
{
if
(r
esultSet!=
null
){
();
}
}
catch
(Exception
e){
throw
new
Exception(
exception:
+sage());
}
try
{
p>
if
(connection!=
null
){
();
}
}
catch
(Exception
e){
throw
new
Exception(
exception:
+sage());
}
}
return
result;
}
3.
向数据库中插入数据
public
void
insert(student
student)
throws
Exception{
Connection
connection=
null
;
PreparedStatement
pStatement=
null
;
String sql=
student(ssno,sname,ssex,isManage,password,sa ge)
values('
+o()+
+me()+
+
x()+
+anage()+
+sword()+
+
age()+
;
try
{
connection=getConnection();
pStatement=eStatement(sql);
e();
}
cat
ch
(SQLException e){
throw
new
Exception(
+sage());
}
catch
(Exception
e){
throw
new
Exception(
+sage());
}
finally
{
try
{
if
(p
Statement!=
null
)
{
(); }
}
catch
(Exception
exception){
throw
new
Exception(
:
+sage());
}
}
try
{
if<
/p>
(connection!=
null
)
();
}
catch
(
Exception e) {
//
TODO
: handle
exception
throw
new
Exception(
exception:
+sage());
}
}
//
通过传入的
student
对象删除表中的数据
public
void
delete(student student)
throws
Exception{
Connection
connection=
null
;
PreparedStatement
pStatement=
null
;
String sql=
+()+
;
try
{
connection=getConnection();
pStatement=eStatement(sql);
e();
}
catch
(SQLException e) {
//
TODO
: handle
exception
throw
new
Exception(
+sage());
}
catch
(Exception
e){
throw
new
Exception(
+sage());
}
finally
{
try
{
if
(p
Statement!=
null
)
{
(); }
}
catch
(Exception
exception){
throw
new
Exception(
:
+sage());
}
}
try
{
if<
/p>
(connection!=
null
)
();
}
catch
(
Exception e) {
//
TODO
: handle
exception
throw
new
Exception(
exception:
+sage());
}
}
4.
修改数据库中的数据
public
void
update(student
student)
throws
Exception{
Connection
connection=
null
;
PreparedStatement
pStatement=
null
;
String sql=
ssno=
+o()+
+
me()+
+student
.get
Ssex()+
+
+anage()+
+sword()+
p>
+
p>
+e()+
+in
Times()+
+
p>
+inLastTime()+
sid=
+()+
;
try
{
connection=getConnection();
pStatement=eStatement(sql);
e();
}
catch
(SQLException e) {
//
TODO
: handle
exception
throw
new
Exception(
+sage());
-
-
-
-
-
-
-
-
-
上一篇:给自己取一个好听的英语名字吧
下一篇:TM7-TM3模式自适应