javabean不能读数据库
为什么我的JavaBean怎么就是读不出sqlserver2000里的数据呢?如果用在.jsp文件中直接饮用sql语言则可以。请大家帮忙看看,着急呀!DataBase.j...
为什么我的JavaBean怎么就是读不出sql server2000里的数据呢?如果用在.jsp文件中直接饮用sql语言则可以。请大家帮忙看看,着急呀!DataBase.java:
package ch;
import java.sql.*;
public class DataBase{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:deng";
Connection conn= null;
ResultSet rs = null;
public DataBase(){
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("DataBase();"+e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("DataBase.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeInsert(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DataBase.executeUpdate"+ex.getMessage());
}
}
//执行删除操作
public void executeDelete(String sql)
{
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DataBese.executeDelete:"+ex.getMessage());
}
}
}
test.jsp文件为:(其中数据库的表是tearg)
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="ch.DataBase" %>
<jsp:useBean id="DbBean" scope="page" class="ch.DataBase"/>
<%String sqlQuery,state,sqlQuerydetail;
sqlQuery = "SELECT * FROM teareg";
ResultSet RS = DbBean.executeQuery(sqlQuery);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<body>
<table width=670 border="0" cellspacing="1" bgcolor=#6681D9 align=center>
<tr bgcolor="#6681D9">
<td height=20 colspan=4>
<div align="center"><b><font color="#ffffff">商品基本信息</font></b></div>
</td>
</tr>
<tr bgcolor="#ffffff" height=20>
<td align=center width=100 bgcolor=#d9e0f9> 商品名称</td>
<td align=center width=100 bgcolor=#d9e0f9> 商品类别</td>
<td align=center width=100 bgcolor=#d9e0f9> 卖方ID</td>
<td align=center width=100 bgcolor=#d9e0f9> 商品地点</td>
<td align=center width=100 bgcolor=#d9e0f9> 商品数量</td>
<td align=center width=100 bgcolor=#d9e0f9> 商品底价</td>
</tr>
<%while(RS.next()){%>
<tr bgcolor="#ffffff" height=20>
<td align=center><%=RS.getString("id")%></td>
<td align=center><%=RS.getString("name")%></td>
<td align=center><%=RS.getString("userr")%></td>
<td align=center><%=RS.getString("cod")%></td>
<td align=center><%=RS.getString("authority")%></td>
<td align=center><%=RS.getString("phone")%></td>
</tr>
<%}
RS.close();%>
</table>
</body>
</html> 展开
package ch;
import java.sql.*;
public class DataBase{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:deng";
Connection conn= null;
ResultSet rs = null;
public DataBase(){
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("DataBase();"+e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("DataBase.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeInsert(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DataBase.executeUpdate"+ex.getMessage());
}
}
//执行删除操作
public void executeDelete(String sql)
{
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DataBese.executeDelete:"+ex.getMessage());
}
}
}
test.jsp文件为:(其中数据库的表是tearg)
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="ch.DataBase" %>
<jsp:useBean id="DbBean" scope="page" class="ch.DataBase"/>
<%String sqlQuery,state,sqlQuerydetail;
sqlQuery = "SELECT * FROM teareg";
ResultSet RS = DbBean.executeQuery(sqlQuery);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<body>
<table width=670 border="0" cellspacing="1" bgcolor=#6681D9 align=center>
<tr bgcolor="#6681D9">
<td height=20 colspan=4>
<div align="center"><b><font color="#ffffff">商品基本信息</font></b></div>
</td>
</tr>
<tr bgcolor="#ffffff" height=20>
<td align=center width=100 bgcolor=#d9e0f9> 商品名称</td>
<td align=center width=100 bgcolor=#d9e0f9> 商品类别</td>
<td align=center width=100 bgcolor=#d9e0f9> 卖方ID</td>
<td align=center width=100 bgcolor=#d9e0f9> 商品地点</td>
<td align=center width=100 bgcolor=#d9e0f9> 商品数量</td>
<td align=center width=100 bgcolor=#d9e0f9> 商品底价</td>
</tr>
<%while(RS.next()){%>
<tr bgcolor="#ffffff" height=20>
<td align=center><%=RS.getString("id")%></td>
<td align=center><%=RS.getString("name")%></td>
<td align=center><%=RS.getString("userr")%></td>
<td align=center><%=RS.getString("cod")%></td>
<td align=center><%=RS.getString("authority")%></td>
<td align=center><%=RS.getString("phone")%></td>
</tr>
<%}
RS.close();%>
</table>
</body>
</html> 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询