当前位置:   article > 正文

jsp连接access数据库

jsp连接access

我已经建立了用access建立了图书管理系统的数据库,并建立了图书明细表.也弄了数据源.执行别的jsp的文件都成功,但唯独连接数据库的都没用。请各位帮帮我
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>JSP连接Access数据库(一)显示记录</title>
</head>
<body>
<div align="center" >JSP连接Access数据库</div>
<div align="center" >
<br/><hr>
<p>
<%
//载入驱动程序类别
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//建立数据库连接,jspdata为ODBC数据源名称
Connection con = DriverManager.getConnection("jdbc:odbc:jspdata");
//建立Statement对象
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
//建立ResultSet(结果集)对象,并执行SQL语句
ResultSet rs = stmt.executeQuery("select * from 图书明细表");
%>
</p>
<p align="center">图书管理系统图书明细表中记录如下</p>
<table width="640" border="1" align="center" bordercolor="#7188e0">
<tr bgcolor="d1d1ff">
<Td width="80">图书编号</th>
<th width="300">图书名称</th>
<th width="200">出版社</th>
<th width="200">定价</th>
<th width="200">日期</th>
</tr><%
while(rs.next())
{%>
<tr bgcolor="#f8f8f8">
<td><%= rs.getString(1) %></td>
<td><%= rs.getString(2) %></td>
<td><%= rs.getString(3) %></td>
<td bgcolor="#f6f6f8"><%= rs.getString(4) %></td>
<td><%= rs.getString(5) %></td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
<p align="center"><br/>
如果能看到表格中的数据,说明连接数据库成功!</p>
</body>
</html>


执行后显示
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /jspdata.jsp:15

12: //载入驱动程序类别
13: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
14: //建立数据库连接,jspdata为ODBC数据源名称
15: Connection con = DriverManager.getConnection("jdbc:odbc:jspdata");
16: //建立Statement对象
17: Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
18: ResultSet.CONCUR_READ_ONLY);


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

javax.servlet.ServletException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:843)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:776)
org.apache.jsp.jspdata_jsp._jspService(jspdata_jsp.java:115)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.jspdata_jsp._jspService(jspdata_jsp.java:58)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.20

 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            String s = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=d:/jsp/oa/database/oa_database.mdb";

 

try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(ClassNotFoundException e){
      System.out.print(e);
    }
    try{
      String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C://Inetpub//wwwroot//Yeyinews//news.mdb";
      Connection conn = DriverManager.getConnection(url);
      Statement stmt = conn.createStatement();
      .........
}
catch(Xeception e){
}

上面的代码实际使用过,应该没有什么问题

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/559218
推荐阅读
相关标签
  

闽ICP备14008679号