数据库是access文件,建立了ODBC,程序如下:
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " " %>
<jsp:useBean id= "conn " scope= "page " class= "conn.openDB "/>
<%@ include file= "System/SysFunction.jsp " %>
<%@ include file= "System/FkFunction.jsp " %>
<META http-equiv= "Content-Type " content= "text/html; charset=GB2312 ">
<META HTTP-EQUIV= "Pragma " CONTENT= "No-Cache ">
<SCRIPT LANGUAGE= "JavaScript " SRC= "System/Query.js "> </SCRIPT>
<TITLE> </TITLE>
<LINK Rel= "Stylesheet " Type= "text/css " Href= "../System/Query.css ">
</HEAD>
<style type= "text/css ">
<!--
.style1 {
font-size: 20px;
font-weight: bold;
}
-->
</style>
<body>
<div align= "center " class= "style1 "> JSP连接Access数据库 </div>
<br>
<hr>
<p>
<%
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver "); //载入驱动程序类别
Connection con = DriverManager.getConnection( "jdbc:odbc:db "); //建立数据库链接,jspdata为ODBC数据源名称
//建立Statement对象
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
int cc=0;
ResultSet rs = stmt.executeQuery( "select * from tb1 "); //建立ResultSet(结果集)对象,并执行SQL语句
//存储数据%>
</p>
<p align= "center "> NUMB1数据表中记录如下 </p>
<table width= "640 " border= "1 " align= "center " bordercolor= "#7188e0 ">
<tr bgcolor= "d1d1ff ">
<th> NBH </th>
<th> XM </th>
<th> DZ </th>
</tr>
<%
String sql= " ";
int tt=0;
while(rs.next())
{ out.println( " <tr> ");
out.println( " <td> "+rs.getString( "id ")+ " </td> ");
out.println( " <td> "+rs.getString( "zd1 ")+ " </td> ");
out.println( " <td> "+rs.getString( "zd2 ")+ " </td> ");
out.println( " </tr> ");
tt++;
}
rs.close();
stmt.close();
con.close();
%>
</table>
<p align= "center "> <br>
如果您能看到表格中的数据,说明连接数据库成功! </p>
</body>
</html>
发布环境是websphere,这个程序在我机器上是好的,我的机器是xp系统,但是放到另外一台机器上就报JDBC—ODBC错误了,错误如下:
[07-5-9 14:10:30:219 CST] 7760993f WebGroup I SRVE0180I: [web] [/] [Servlet.LOG]: /new.jsp: init
[07-5-9 14:10:30:312 CST] 7760993f WebGroup E SRVE0026E: [Servlet 错误]-[sun.jdbc.odbc.JdbcOdbcDriver]:java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
at com.ibm.ws.classloader.CompoundClassLoader.findClass(CompoundClassLoader.java(Compiled Code))