在jsp中使用JavaBean连接数据库
我想在jsp中使用JavaBean在JavaBean中写好连接数据库以及查询的类之后,怎么样在jsp页面中获取JavaBean中查询的结果,给出一个实例,JavaBean...
我想在jsp中使用JavaBean 在JavaBean中写好连接数据库以及查询的类之后,怎么样在jsp页面中获取JavaBean中查询的结果,给出一个实例,JavaBean的代码和jsp的代码都给出~~谢谢
展开
2个回答
展开全部
这个是连接access的javaBean,首先配置access数据源,数据源名称设为info.mdb,此文件放在test文件夹下,编译后将生成的infoBean.class放在claeese下的test文件夹下即可。用access写个数据库info.mdb里面写个表student,两个字段id name。
package test;
import java.sql.*;
public class infoBean{
private String ab="sun.jdbc.odbc.JdbcOdbcDriver";
private String ac="JDBC:odbc:info.mdb";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
public infoBean(){
try{
Class.forName(ab);}
catch(java.lang.ClassNotFoundException e){
System.out.println("infoBean():"+e.getMessage());}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
conn=DriverManager.getConnection(ac);
stmt=conn.createStatement();
rs = stmt.executeQuery(sql);}
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());}
return rs;
}
public void executeUpdate(String sql)
{stmt=null;
rs=null;
try{
conn=DriverManager.getConnection(ac);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();}
catch(SQLException e1)
{System.err.println("executeUpdate:" + e1.getMessage()); }
}
public void closeStmt()
{
try{
stmt.close();
}
catch (SQLException e2)
{
e2.printStackTrace();
}
}
public void closeConn()
{
try{
conn.close();
}
catch (SQLException e3)
{
e3.printStackTrace();
}
}
}
在jsp页面中引用时,显示数据库内容:
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body><table>
<tr><th>11</th><th>11</th></tr>
<%
ResultSet rs=inBean.executeQuery("SELECT * FROM student");
while rs.next(){
%>
<tr><td><%=rs.getString(1)%></td><td><%=rs.getString(2)%></td></tr>
<%}%>
</table></body></html>
在jsp中删除,
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body>
<%
String id=request.getParameter("id");
inBean.executeUpdate("Delete from student where id='"+id+"'");
%>
</body></html>
其中id是上一个页面传过来的;
jsp中修改:
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body>
<%
String id=request.getParameter("id");
String name=request.getParameter("name");
inBean.executeUpdate("update student set name='"+name+"' where id='"+id+"'");
%>
</body></html>
我还有javaBean连接mysql,sql server的例子,也有关于javaBean完整简单的小项目,你要的话,给邮箱,发给你
package test;
import java.sql.*;
public class infoBean{
private String ab="sun.jdbc.odbc.JdbcOdbcDriver";
private String ac="JDBC:odbc:info.mdb";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
public infoBean(){
try{
Class.forName(ab);}
catch(java.lang.ClassNotFoundException e){
System.out.println("infoBean():"+e.getMessage());}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
conn=DriverManager.getConnection(ac);
stmt=conn.createStatement();
rs = stmt.executeQuery(sql);}
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());}
return rs;
}
public void executeUpdate(String sql)
{stmt=null;
rs=null;
try{
conn=DriverManager.getConnection(ac);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();}
catch(SQLException e1)
{System.err.println("executeUpdate:" + e1.getMessage()); }
}
public void closeStmt()
{
try{
stmt.close();
}
catch (SQLException e2)
{
e2.printStackTrace();
}
}
public void closeConn()
{
try{
conn.close();
}
catch (SQLException e3)
{
e3.printStackTrace();
}
}
}
在jsp页面中引用时,显示数据库内容:
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body><table>
<tr><th>11</th><th>11</th></tr>
<%
ResultSet rs=inBean.executeQuery("SELECT * FROM student");
while rs.next(){
%>
<tr><td><%=rs.getString(1)%></td><td><%=rs.getString(2)%></td></tr>
<%}%>
</table></body></html>
在jsp中删除,
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body>
<%
String id=request.getParameter("id");
inBean.executeUpdate("Delete from student where id='"+id+"'");
%>
</body></html>
其中id是上一个页面传过来的;
jsp中修改:
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body>
<%
String id=request.getParameter("id");
String name=request.getParameter("name");
inBean.executeUpdate("update student set name='"+name+"' where id='"+id+"'");
%>
</body></html>
我还有javaBean连接mysql,sql server的例子,也有关于javaBean完整简单的小项目,你要的话,给邮箱,发给你
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你我自己写的一个简单的例子吧:
<%@ page contentType="text/html; charset=gb18030" language="java" import="java.util.*,java.sql.*,com.ncu.cn.model.*,com.ncu.cn.service.*" errorPage="" pageEncoding="GB18030"%>
<jsp:useBean id="s" class="com.ncu.cn.service.studentmanage" scope="request"></jsp:useBean>
<jsp:useBean id="stu" class="com.ncu.cn.model.student" scope="request"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
<title>学生基本信息管理</title>
<style type="text/css">
<!--
.STYLE1 {color: #3366FF}
-->
</style>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("/StudentManage/index.jsp");
}
%>
</head>
<body>
<table width="100%" border="1" cellspacing="0">
<tr>
<td width="12%"><div align="center" class="STYLE1">学号</div></td>
<td width="12%"><div align="center" class="STYLE1">姓名</div></td>
<td width="12%"><div align="center" class="STYLE1">性别</div></td>
<td width="8%"><div align="center" class="STYLE1">年龄</div></td>
<td width="16%"><div align="center" class="STYLE1">学院</div></td>
<td width="18%"><div align="center" class="STYLE1">班级</div></td>
<td width="22%"><div align="center" class="STYLE1">修改/删除</div></td>
</tr>
<%
ArrayList<student> l = s.getAllstudentinfo();
ListIterator i = l.listIterator();
while(i.hasNext()){
stu = (student)i.next();
out.println("<tr>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSno()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSname()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSsex()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSage()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSdept()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSclass()+"</td>");
out.println("<td><div align='center' class='STYLE5'><a href='stuUpdate.jsp?Sno="+stu.getSno()+"'>更改</a> <a href='stuDel.jsp?Sno="+stu.getSno()+"'>删除</a></td></tr>");
}
%>
</table>
<p align="center"><span class="STYLE7"><a href="stuadd.jsp">增加学生</a></span></p>
</body>
</html>
<%@ page contentType="text/html; charset=gb18030" language="java" import="java.util.*,java.sql.*,com.ncu.cn.model.*,com.ncu.cn.service.*" errorPage="" pageEncoding="GB18030"%>
<jsp:useBean id="s" class="com.ncu.cn.service.studentmanage" scope="request"></jsp:useBean>
<jsp:useBean id="stu" class="com.ncu.cn.model.student" scope="request"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
<title>学生基本信息管理</title>
<style type="text/css">
<!--
.STYLE1 {color: #3366FF}
-->
</style>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("/StudentManage/index.jsp");
}
%>
</head>
<body>
<table width="100%" border="1" cellspacing="0">
<tr>
<td width="12%"><div align="center" class="STYLE1">学号</div></td>
<td width="12%"><div align="center" class="STYLE1">姓名</div></td>
<td width="12%"><div align="center" class="STYLE1">性别</div></td>
<td width="8%"><div align="center" class="STYLE1">年龄</div></td>
<td width="16%"><div align="center" class="STYLE1">学院</div></td>
<td width="18%"><div align="center" class="STYLE1">班级</div></td>
<td width="22%"><div align="center" class="STYLE1">修改/删除</div></td>
</tr>
<%
ArrayList<student> l = s.getAllstudentinfo();
ListIterator i = l.listIterator();
while(i.hasNext()){
stu = (student)i.next();
out.println("<tr>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSno()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSname()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSsex()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSage()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSdept()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSclass()+"</td>");
out.println("<td><div align='center' class='STYLE5'><a href='stuUpdate.jsp?Sno="+stu.getSno()+"'>更改</a> <a href='stuDel.jsp?Sno="+stu.getSno()+"'>删除</a></td></tr>");
}
%>
</table>
<p align="center"><span class="STYLE7"><a href="stuadd.jsp">增加学生</a></span></p>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询