急javabean连接数据库的问题(源代码)

xueblueice 2002-03-12 02:07:21
javabean连接数据库的问题(源代码)
在main.jsp中调用basicDBBean,发生下列错误,请高手指点

javax.servlet.ServletException: basicDBBean
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:457)
at org.apache.jsp.main$jsp._jspService(main$jsp.java:139)
...


root cause

java.lang.InstantiationException: basicDBBean
at org.apache.jsp.main$jsp._jspService(main$jsp.java:92)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
....

源代码:
--------------------------------------------------------------------------//basicFBBean.java
package lyb;

import java.sql.*;
public class basicDBBean {
//jdbc驱动程序
String DBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//数据源地址
String DBLocation="jdbc:microsoft:sqlserver://ERDOS00:1433";
//数据库连接
Connection conn=null;
//查询结果的记录集
ResultSet rs=null;

//构造函数
public basicDBBean() {
}

//执行一个查询
public ResultSet executeQuery(String sql){
//如果没有可用的连接,试着建立连接
if (conn == null){
DBConnect();
}

//如果仍然没有可用的连接,返回null值
if (conn == null)
rs=null;
else{
//必须捕获SQLException异常
try{
Statement s=conn.createStatement();
//执行查询
rs=s.executeQuery(sql);
}
catch(SQLException e){
}
}
return(rs);
}

//建立一个缺省的连接
public String DBConnect(){
String strExc="";
try{
//建立连接
Class.forName(DBDriver);
conn=DriverManager.getConnection(DBLocation,"gb","gb");
}
catch(ClassNotFoundException e){
strExc=e.toString();
}
catch(SQLException e){
strExc=e.toString();
}
return(strExc);
}

//设置一个可用的数据源地址,param Location
public void setDBLocation(String location){
DBLocation=location;
}

//设置一个可用的JDBC驱动程序,param driver
public void setDBDriver(String driver){
DBDriver=driver;
}

//设置一个已经可用的数据连接,param conn
public void setConn(Connection conn){
conn=conn;
}


}




//main.jsp
<%--留言标题的查看main.jsp--%>
<%--从数据库中输出中文一定要设置文件的ContentType--%>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" import="java.sql.*" %>
<%@ page import = "lyb.basicDBBean" %>
<%--载入一个数据库连接的JavaBean--%>
<jsp:useBean id="basicDB" class="basicDBBean" scope="page" />
<html>
<head>
<title>留言版
</title>
<meta http-equiv="Content-Type" content="text/html";charset=gb2312">
</head>
<style type="text/css">
<!--
.n9pt {font-size:9pt}
a {font-size:9pt; color:#000000; text-decoration:none}
a:hover :font-size:9pt; color:#FF0000 ; text-decoration:none}
-->
</style>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" align=center valign=middle>
<br><div align=center style="font-size:14pt;color:black">我的留言版</div><br>
<table width="760" border="0" align="center" cellpadding="2" cellspacing="2" class="n9pt">
<tr>
<td width="500" bgcolor="#CCCCCC">
<div align="center"><font="FFFFFF">标题</font></div>
</td>
<td width="130" bgcolor="#CCCCCC">
<div align="center"><font="FFFFFF">作者</font></div>
</td>
<td width="130" bgcolor="#CCCCCC">
<div align="center"><font="FFFFFF">发表时间</font></div>
</td>
</tr>

<%
String sql;
ResultSet rs;
sql= "SELECT * FROM basiclyb ORDER BY addtime DESC";

//建立并执行查询
basicDB.DBConnect();
rs=basicDB.executeQuery(sql);

//输出数据(标题、作者、时间)
while (rs.next()){
out.print("<tr><td width=500 bgcolor=#eeeeee><a herf=view.jsp?id=");
out.print(rs.getInt("id"));
out.print(" target=bottom> ");
out.print(rs.getString("title"));
out.print("</a></td><td width=130 bgcolor=#eeeeee><div align=center>");
out.print(rs.getString("author"));
out.print("</div></td><td width=130 bgcolor=#eeeeee><div align=center>");
out.print(rs.getString("addtime"));
out.print("</div></td></tr>");
}
%>
</table>
</body>
</html>
...全文
16 点赞 收藏 回复
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复

还没有回复,快来抢沙发~

发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告