jsp连接Oracle的增删改查
****************************************db包中的stu学生************************************
package db;
public class stu { //学生类
private String sid;
private String name;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
package db;
public class stu { //学生类
private String sid;
private String name;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
}
*****************************************db包中的ConnDb************************************
package db;
import java.sql.Connection;
import java.sql.DriverManager;
public void setName(String name) {
this.name = name;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
}
*****************************************db包中的ConnDb************************************
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class ConnDb { //数据库连接和操作的类
private static Connection con=null;
private static Statement stmt=null;
private static ResultSet rs=null;
public static void statrconn(){ //连接数据库方法
try {
Class.forName("oracle.acledriver "); //创建连接数据库中间件
try {
Connection("jdbc:odbc:thin:@127.0.0.1:1521:orcl","",""); //连接access数据源
} catch (SQLException e) {
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class ConnDb { //数据库连接和操作的类
private static Connection con=null;
private static Statement stmt=null;
private static ResultSet rs=null;
public static void statrconn(){ //连接数据库方法
try {
Class.forName("oracle.acledriver "); //创建连接数据库中间件
try {
Connection("jdbc:odbc:thin:@127.0.0.1:1521:orcl","",""); //连接access数据源
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void endconn() throws SQLException{ //关闭数据源的类
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void endconn() throws SQLException{ //关闭数据源的类
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(con!=null){
con.close();
con=null;
}
}
public static void update(String sql) throws SQLException{ //数据库更新类
statrconn();
ateStatement();
uteUpdate(sql);
endconn();
}
public static ArrayList getList1(String sql) throws SQLException{ //数据库查询类,查询结果集以列表返回
}
if(con!=null){
con.close();
con=null;
}
}
public static void update(String sql) throws SQLException{ //数据库更新类
statrconn();
ateStatement();
uteUpdate(sql);
endconn();
}
public static ArrayList getList1(String sql) throws SQLException{ //数据库查询类,查询结果集以列表返回
ArrayList list=new ArrayList();
statrconn();
ateStatement();
uteQuery(sql);
()){
stu st=new stu();
st.String("sid"));
st.String("sname"));
st.String("sage"));
list.add(st);
}
endconn();
return list;
}
}
statrconn();
ateStatement();
uteQuery(sql);
()){
stu st=new stu();
st.String("sid"));
st.String("sname"));
st.String("sage"));
list.add(st);
}
endconn();
return list;
}
}
********************************主jsp页面list.jsp****************************************
<%@ page language="java" import="db.ConnDb,db.stu,java.util.*" pageEncoding="gbk"%><%--jsp头文件,包括引入的包和支持语言,gbk为支持中文--%><%
ArrayList list = List1("select sid,sname,sage from test"); //获得test表中所有数据并以列表形式返回
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'list.jsp' starting page</title>
</head>
<body>
<input type="button" name="bottom" value="添加新学生" onclick="javascript:window.location.href='add.jsp'">
<%@ page language="java" import="db.ConnDb,db.stu,java.util.*" pageEncoding="gbk"%><%--jsp头文件,包括引入的包和支持语言,gbk为支持中文--%><%
ArrayList list = List1("select sid,sname,sage from test"); //获得test表中所有数据并以列表形式返回
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'list.jsp' starting page</title>
</head>
<body>
<input type="button" name="bottom" value="添加新学生" onclick="javascript:window.location.href='add.jsp'">
<br><br>
<table border="1" >
<tr>
<td>编号</td><td>姓名</td><td>年龄</td><td>操作</td>
</tr>
<% {
for(Iterator it=list.iterator();it.hasNext();){
stu s=(();%>
<tr>
<td><%=s.getSid()%></td>
<td><%=s.getName()%></td>
<td><%=s.getAge()%></td>
<td><a href="update.jsp?sid=<%=s.getSid()%>">修改</a><a href="delete.jsp?sid=<%=s.getSid()%>" onclick="return confirm('确定删除?');">删除</a></td>
</tr><% }}%></table>
<table border="1" >
<tr>
<td>编号</td><td>姓名</td><td>年龄</td><td>操作</td>
</tr>
<% {
for(Iterator it=list.iterator();it.hasNext();){
stu s=(();%>
<tr>
<td><%=s.getSid()%></td>
<td><%=s.getName()%></td>
<td><%=s.getAge()%></td>
<td><a href="update.jsp?sid=<%=s.getSid()%>">修改</a><a href="delete.jsp?sid=<%=s.getSid()%>" onclick="return confirm('确定删除?');">删除</a></td>
</tr><% }}%></table>
</body>
</html>
************************************************添加学生的jsp页面add.jsp******************************************
<%@ page language="java" pageEncoding="gbk"%>
<%
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>add</title>
</head>
<body>
<form action="addinfo.jsp" method="post"> 提交到addinfo.jsp
<p>添加学生:</p>
</html>
************************************************添加学生的jsp页面add.jsp******************************************
<%@ page language="java" pageEncoding="gbk"%>
<%
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>add</title>
</head>
<body>
<form action="addinfo.jsp" method="post"> 提交到addinfo.jsp
<p>添加学生:</p>
姓名:<input type="text" name="sname"><br><br>
年龄:<input type="text" name="sage"><br><br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
*******************************************添加学生的处理jsp页面addinfo.jsp*******************************************
<%@ page language="java" import="db.ConnDb" pageEncoding="gbk"%>
<%
String name = new Parameter("sname").getBytes("iso-8859-1"));
String age = new Parameter("sage").getBytes("iso-8859-1"));
ConnDb.update("insert into test (sname,sage) values ('"+name+"','"+age+"')");
response.sendRedirect("list.jsp");
%>
年龄:<input type="text" name="sage"><br><br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
*******************************************添加学生的处理jsp页面addinfo.jsp*******************************************
<%@ page language="java" import="db.ConnDb" pageEncoding="gbk"%>
<%
String name = new Parameter("sname").getBytes("iso-8859-1"));
String age = new Parameter("sage").getBytes("iso-8859-1"));
ConnDb.update("insert into test (sname,sage) values ('"+name+"','"+age+"')");
response.sendRedirect("list.jsp");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'addinfo.jsp' starting page</title>
</head>
<body>
添加成功! <br>
</body>
</html>
***********************************修改学生信息的jsp页面update.jsp*******************************************************
<%@ page language="java" import="db.ConnDb,db.stu,java.util.*" pageEncoding="gbk"%>
<%
String sid=new Parameter("sid")); //获得要修改人的sid
<html>
<head>
<title>My JSP 'addinfo.jsp' starting page</title>
</head>
<body>
添加成功! <br>
</body>
</html>
***********************************修改学生信息的jsp页面update.jsp*******************************************************
<%@ page language="java" import="db.ConnDb,db.stu,java.util.*" pageEncoding="gbk"%>
<%
String sid=new Parameter("sid")); //获得要修改人的sid
ArrayList List1("select sid,sname,sage from test where sid=" +sid); //查询要修改学生的信息,并以列表返回
stu s = ((0); //将列表内容换成学生类
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'update.jsp' starting page</title>
</head>
<body>
<form method="post" action="updateInfo.jsp"> 提交到updateInfo.jsp页面
<p>
修改学生信息
</p>
stu s = ((0); //将列表内容换成学生类
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'update.jsp' starting page</title>
</head>
<body>
<form method="post" action="updateInfo.jsp"> 提交到updateInfo.jsp页面
<p>
修改学生信息
</p>
<p>
姓名:
<input type="text" name="sname" value="<%=s.getName() %>"> 修改学生信息
</p>
<p>
年龄:
<input type="text" name="sage" value="<%=s.getAge()%>">
<input type="hidden" name="sid" value="<%=sid%>">
</p>
<p>
<input type="submit" value="提交" name="button1">
</p>
</form>
</body>
姓名:
<input type="text" name="sname" value="<%=s.getName() %>"> 修改学生信息
</p>
<p>
年龄:
<input type="text" name="sage" value="<%=s.getAge()%>">
<input type="hidden" name="sid" value="<%=sid%>">
</p>
<p>
<input type="submit" value="提交" name="button1">
</p>
</form>
</body>
</html>
**************************************修改的处理页面updateInfo.jsp*********************************************************
<%@ page language="java" import="db.ConnDb" pageEncoding="gbk"%>
<%
String name = new Parameter("sname").getBytes("ISO-8859-1")); //接收信息
String age = new Parameter("sage").getBytes("ISO-8859-1"));
String sid = new Parameter("sid").getBytes("ISO-8859-1"));
String sql = "update test set sname='"+name+"',sage='"+age+"' where sid="+sid;
ConnDb.update(sql); //执行修改
response.sendRedirect("list.jsp"); //转到list.jsp页面
%>
**************************************修改的处理页面updateInfo.jsp*********************************************************
<%@ page language="java" import="db.ConnDb" pageEncoding="gbk"%>
<%
String name = new Parameter("sname").getBytes("ISO-8859-1")); //接收信息
String age = new Parameter("sage").getBytes("ISO-8859-1"));
String sid = new Parameter("sid").getBytes("ISO-8859-1"));
String sql = "update test set sname='"+name+"',sage='"+age+"' where sid="+sid;
ConnDb.update(sql); //执行修改
response.sendRedirect("list.jsp"); //转到list.jsp页面
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<html>
<head>
<title>My JSP 'updateInfo.jsp' starting page</title>
</head>
<title>My JSP 'updateInfo.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
******************************************删除的处理页面delete.jsp********************************************************
<%@ page language="java" import="db.ConnDb" pageEncoding="gbk"%>
<%
ConnDb.update("delete from test where sid ="+Parameter("sid")); //获得要删除人的sid并删除
response.sendRedirect("list.jsp"); //转到list.jsp页面
This is my JSP page. <br>
</body>
</html>
******************************************删除的处理页面delete.jsp********************************************************
<%@ page language="java" import="db.ConnDb" pageEncoding="gbk"%>
<%
ConnDb.update("delete from test where sid ="+Parameter("sid")); //获得要删除人的sid并删除
response.sendRedirect("list.jsp"); //转到list.jsp页面
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
jsp创建
<title>My JSP 'delete.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
</body>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
jsp创建
<title>My JSP 'delete.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
</body>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论