JSP对Oracle数据库进行增删改查实例
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
%>
<tr>
<td><%=s.getSid()%></td>
<td><%=s.getName()%></td>
<td><%=s.getAge()%></td>
<td>
<ahref="update.jsp?sid=<%=s.getSid()%>">修改</a>
<ahref="delete.jsp?sid=<%=s.getSid()%>"
}catch(SQLException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}catch(ClassNotFoundException e) {
//TODOAuto-generated catch block
e.printStackTrace();
3、在db包中建立ConnDb.java
packagedb;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
sidvarchar2(50)primarykey,
snamevarchar2(50),
sagevarchar2(50)
);
insertintotest(sid,sname,sage)values('1000','yujian','26');
insertintotest(sid,sname,sage)values('2000','yechuangyu','26');
importjava.util.ArrayList;
publicclassConnDb {//数据库连接和操作的类
privatestaticConnectioncon=null;
privatestaticStatementstmt=null;
privatestaticResultSetrs=null;
st.setName(rs.getString("sname"));
st.setAge(rs.getString("sage"));
list.add(st);
}
endconn();
returnlist;
}
}
4、在包db中建立stu.java
packagedb;
publicclassstu {
privateStringsid;
//TODOAuto-generated method stub
}
}
5、建立list.jsp
<%@pagelanguage="java"import="db.ConnDb,db.stu,java.util.*"
pageEncoding="gbk"%>
<%--jsp头文件,包括引入的包和支持语言,gbk为支持中文--%>
<%
ArrayList list = ConnDb.getList1("select sid,sname,sage from test");//获得test表中所有数据并以列表形式返回
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01 Transitional//EN">
<html>
<head>
<title>MyJSP'list.jsp' starting page</title>
</head>
<body>
<inputtype="button"name="bottom"value="添加新学生"
onclick="javascript:window.location.href='add.jsp'">
onclick="return confirm('确定删除?');">删除</a>
</td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
6、建立add.jsp
<%@pagelanguage="java"pageEncoding="gbk"%>
<%%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01 Transitional//EN">
privateStringname;
privateStringage;
publicString getSid() {
returnsid;
}
publicvoidsetSid(String sid) {
this.sid= sid;
}
publicString getName() {
returnname;
}
response.sendRedirect("list.jsp");
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01 Transitional//EN">
<html>
<head>
<title>MyJSP'addinfo.jsp' starting page</title>
</p>
<Байду номын сангаас>
<inputtype="submit"value="提交"name="button1">
</p>
</form>
</body>
</html>
8、建立updateInfo.jsp
<%@pagelanguage="java"import="db.ConnDb"pageEncoding="gbk"%>
con=null;
}
}
publicstaticvoidupdate(String sql)throwsSQLException {
//数据库更新
statrconn();
stmt=con.createStatement();
stmt.executeUpdate(sql);
endconn();
}
publicstaticArrayList getList1(String sql)throwsSQLException {
//数据库查询
ArrayList list =newArrayList();
statrconn();
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()) {
stu st =newstu();
st.setSid(rs.getString("sid"));
<br>
<br>
<tableborder="1">
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
操作
</td>
</tr>
<%
{
for(Iterator it = list.iterator(); it.hasNext();) {
stu s = (stu) it.next();
<%
String name =newString(request.getParameter("sname").getBytes("ISO-8859-1"));//接收信息
String age =newString(request.getParameter("sage").getBytes("ISO-8859-1"));
publicstaticvoidstatrconn() {//连接数据库方法
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建连接数据库中间件
try{
con= DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:YUJIAN","scott","root");
}
}
publicstaticvoidendconn()throwsSQLException {//关闭连接
if(rs!=null) {
rs.close();
rs=null;
}
if(stmt!=null) {
stmt.close();
stmt=null;
}
if(con!=null) {
con.close();
publicvoidsetName(String name) {
this.name= name;
}
publicString getAge() {
returnage;
}
publicvoidsetAge(String age) {
this.age= age;
}
publicstaticvoidmain(String[] args) {
<html>
<head>
<title>add</title>
</head>
<body>
<formaction="addInfo.jsp"method="post">
提交到addinfo.jsp
<p>
添加学生:
</p>
SID:
<inputtype="text"name="sid">
<br>
<br>
姓名:
ArrayList list = ConnDb.getList1("select sid,sname,sage from test where sid="+sid);//查询要修改学生的信息,并以列表返回
stu s = (stu) list.get(0);//将列表内容换成学生类
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01 Transitional//EN">
<inputtype="text"name="sname">
<br>
<br>
年龄:
<inputtype="text"name="sage">
<br>
<br>
<inputtype="submit"name="submit"value="提交">
</form>
</body>
</html>
7、建立addInfo.jsp
String sid =newString(request.getParameter("sid").getBytes("ISO-8859-1"));
String sql ="update test set sname='"+name+"',sage='"+age+"' where sid="+sid;
<html>
<head>
<title>MyJSP'update.jsp' starting page</title>
</head>
<body>
<formmethod="post"action="updateInfo.jsp">
提交到updateInfo.jsp页面
<p>
修改学生信息
</p>
JSP对Oracle数据库进行增删改查实例
时间:2012-3-20 20:38:22作者:雨剑电脑来源:原创查看:224评论:0
JSP对Oracle数据库进行增删改查实例
1、建立一个web工程Oracle,引入Oracle驱动包classes12.jar。
2、建立数据库
createtabletest(
</head>
<body>
添加成功!
<br>
</body>
</html>
8、建立update.jsp
<%@pagelanguage="java"import="db.ConnDb,db.stu,java.util.*"
pageEncoding="gbk"%>
<%
String sid =newString(request.getParameter("sid"));//获得要修改人的sid
String age =newString(request.getParameter("sage").getBytes("iso-8859-1"));
ConnDb.update("insert into test (sid,sname,sage) values ('"+sid+"','"+name+"','"+age+"')");
<%@pagelanguage="java"import="db.ConnDb"pageEncoding="gbk"%>
<%
String sid=newString(request.getParameter("sid").getBytes("iso-8859-1"));
String name =newString(request.getParameter("sname").getBytes("iso-8859-1"));
<p>
姓名:
<inputtype="text"name="sname"value="<%=s.getName()%>">
修改学生信息
</p>
<p>
年龄:
<inputtype="text"name="sage"value="<%=s.getAge()%>">
<inputtype="hidden"name="sid"value="<%=sid%>">
<tr>
<td><%=s.getSid()%></td>
<td><%=s.getName()%></td>
<td><%=s.getAge()%></td>
<td>
<ahref="update.jsp?sid=<%=s.getSid()%>">修改</a>
<ahref="delete.jsp?sid=<%=s.getSid()%>"
}catch(SQLException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}catch(ClassNotFoundException e) {
//TODOAuto-generated catch block
e.printStackTrace();
3、在db包中建立ConnDb.java
packagedb;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
sidvarchar2(50)primarykey,
snamevarchar2(50),
sagevarchar2(50)
);
insertintotest(sid,sname,sage)values('1000','yujian','26');
insertintotest(sid,sname,sage)values('2000','yechuangyu','26');
importjava.util.ArrayList;
publicclassConnDb {//数据库连接和操作的类
privatestaticConnectioncon=null;
privatestaticStatementstmt=null;
privatestaticResultSetrs=null;
st.setName(rs.getString("sname"));
st.setAge(rs.getString("sage"));
list.add(st);
}
endconn();
returnlist;
}
}
4、在包db中建立stu.java
packagedb;
publicclassstu {
privateStringsid;
//TODOAuto-generated method stub
}
}
5、建立list.jsp
<%@pagelanguage="java"import="db.ConnDb,db.stu,java.util.*"
pageEncoding="gbk"%>
<%--jsp头文件,包括引入的包和支持语言,gbk为支持中文--%>
<%
ArrayList list = ConnDb.getList1("select sid,sname,sage from test");//获得test表中所有数据并以列表形式返回
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01 Transitional//EN">
<html>
<head>
<title>MyJSP'list.jsp' starting page</title>
</head>
<body>
<inputtype="button"name="bottom"value="添加新学生"
onclick="javascript:window.location.href='add.jsp'">
onclick="return confirm('确定删除?');">删除</a>
</td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
6、建立add.jsp
<%@pagelanguage="java"pageEncoding="gbk"%>
<%%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01 Transitional//EN">
privateStringname;
privateStringage;
publicString getSid() {
returnsid;
}
publicvoidsetSid(String sid) {
this.sid= sid;
}
publicString getName() {
returnname;
}
response.sendRedirect("list.jsp");
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01 Transitional//EN">
<html>
<head>
<title>MyJSP'addinfo.jsp' starting page</title>
</p>
<Байду номын сангаас>
<inputtype="submit"value="提交"name="button1">
</p>
</form>
</body>
</html>
8、建立updateInfo.jsp
<%@pagelanguage="java"import="db.ConnDb"pageEncoding="gbk"%>
con=null;
}
}
publicstaticvoidupdate(String sql)throwsSQLException {
//数据库更新
statrconn();
stmt=con.createStatement();
stmt.executeUpdate(sql);
endconn();
}
publicstaticArrayList getList1(String sql)throwsSQLException {
//数据库查询
ArrayList list =newArrayList();
statrconn();
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()) {
stu st =newstu();
st.setSid(rs.getString("sid"));
<br>
<br>
<tableborder="1">
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
操作
</td>
</tr>
<%
{
for(Iterator it = list.iterator(); it.hasNext();) {
stu s = (stu) it.next();
<%
String name =newString(request.getParameter("sname").getBytes("ISO-8859-1"));//接收信息
String age =newString(request.getParameter("sage").getBytes("ISO-8859-1"));
publicstaticvoidstatrconn() {//连接数据库方法
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建连接数据库中间件
try{
con= DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:YUJIAN","scott","root");
}
}
publicstaticvoidendconn()throwsSQLException {//关闭连接
if(rs!=null) {
rs.close();
rs=null;
}
if(stmt!=null) {
stmt.close();
stmt=null;
}
if(con!=null) {
con.close();
publicvoidsetName(String name) {
this.name= name;
}
publicString getAge() {
returnage;
}
publicvoidsetAge(String age) {
this.age= age;
}
publicstaticvoidmain(String[] args) {
<html>
<head>
<title>add</title>
</head>
<body>
<formaction="addInfo.jsp"method="post">
提交到addinfo.jsp
<p>
添加学生:
</p>
SID:
<inputtype="text"name="sid">
<br>
<br>
姓名:
ArrayList list = ConnDb.getList1("select sid,sname,sage from test where sid="+sid);//查询要修改学生的信息,并以列表返回
stu s = (stu) list.get(0);//将列表内容换成学生类
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01 Transitional//EN">
<inputtype="text"name="sname">
<br>
<br>
年龄:
<inputtype="text"name="sage">
<br>
<br>
<inputtype="submit"name="submit"value="提交">
</form>
</body>
</html>
7、建立addInfo.jsp
String sid =newString(request.getParameter("sid").getBytes("ISO-8859-1"));
String sql ="update test set sname='"+name+"',sage='"+age+"' where sid="+sid;
<html>
<head>
<title>MyJSP'update.jsp' starting page</title>
</head>
<body>
<formmethod="post"action="updateInfo.jsp">
提交到updateInfo.jsp页面
<p>
修改学生信息
</p>
JSP对Oracle数据库进行增删改查实例
时间:2012-3-20 20:38:22作者:雨剑电脑来源:原创查看:224评论:0
JSP对Oracle数据库进行增删改查实例
1、建立一个web工程Oracle,引入Oracle驱动包classes12.jar。
2、建立数据库
createtabletest(
</head>
<body>
添加成功!
<br>
</body>
</html>
8、建立update.jsp
<%@pagelanguage="java"import="db.ConnDb,db.stu,java.util.*"
pageEncoding="gbk"%>
<%
String sid =newString(request.getParameter("sid"));//获得要修改人的sid
String age =newString(request.getParameter("sage").getBytes("iso-8859-1"));
ConnDb.update("insert into test (sid,sname,sage) values ('"+sid+"','"+name+"','"+age+"')");
<%@pagelanguage="java"import="db.ConnDb"pageEncoding="gbk"%>
<%
String sid=newString(request.getParameter("sid").getBytes("iso-8859-1"));
String name =newString(request.getParameter("sname").getBytes("iso-8859-1"));
<p>
姓名:
<inputtype="text"name="sname"value="<%=s.getName()%>">
修改学生信息
</p>
<p>
年龄:
<inputtype="text"name="sage"value="<%=s.getAge()%>">
<inputtype="hidden"name="sid"value="<%=sid%>">