当前位置: 代码迷 >> Java Web开发 >> [求助]JDBC连接ACCESS的问题
  详细解决方案

[求助]JDBC连接ACCESS的问题

热度:399   发布时间:2006-09-18 16:21:49.0
[求助]JDBC连接ACCESS的问题
String url = "sun.jdbc.odbc.JdbcOdbcDriver";
String driver = "dbc:odbc:Driver={MicroSoft Access Driver(*.mdb)};DBQ=E:\\javatest\\Test.mdb";

Class.forName(driver).newInstance();
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();

我用上面的程序来连接,却出现以下的报错:
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

这个错误应该是说找不到驱动,
这个连接需要驱动吗?
这个驱动哪里有下载?

有没有其它更好的连接方式啊?
谢谢!

搜索更多相关主题的帖子: JDBC  ACCESS  

----------------解决方案--------------------------------------------------------

不好意思,写反了
String url = "dbc:odbc:Driver={MicroSoft Access Driver(*.mdb)};DBQ=E:\\javatest\\Test.mdb";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

现在的报错是这样的:
java.sql.SQLException: No suitable driver

这个要怎么解决啊?


----------------解决方案--------------------------------------------------------
可以用ODBC数据源
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String dbUrl = "jdbc:odbc:aaa";//aaa是数据源的名字
Class.forName(driver).newInstance();
Connection conn= DriverManager.getConnection(dbUrl,"","");
Statement stmt=conn.createStatement();
这样应该可以
你试试

----------------解决方案--------------------------------------------------------
第3行没有用  去掉
----------------解决方案--------------------------------------------------------

3楼的方法不行,错误还是一样
java.sql.SQLException: No suitable driver

4楼的把第三行拿掉,是这行吗?
Class.forName(driver).newInstance();

拿掉后有了新的错误,这个错误又不知是什么意思

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: Unable to compile class for JSP

An error occurred at line: 47 in the jsp file: /TestDbConnect.jsp
Generated servlet error:
Unreachable catch block for ClassNotFoundException. This exception is never thrown from the try statement body


org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
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

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 47 in the jsp file: /TestDbConnect.jsp
Generated servlet error:
Unreachable catch block for ClassNotFoundException. This exception is never thrown from the try statement body


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:303)
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.17 logs.


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

Apache Tomcat/5.5.17




程序是这样的


<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,javax.sql.*"%>
<%@ page import="java.util.*"%>
<html>
<body>
<%

String url = "dbc:odbc:Driver={MicroSoft Access Driver(*.mdb)};DBQ=E:\\javatest\\Test.mdb";
//String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

String user = "";
String password = "";
try {
//Class.forName(driver).newInstance();
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
int Msg = 0;
while(rs.next()) {%>
<%=rs.getString(1)%>
<%=rs.getString(2)%>
<%=rs.getString(3)%>
<%=rs.getString(4)%>
<%=rs.getString(5)%>
<%=rs.getString(6)%>
<%=rs.getString(7)%>
<%=rs.getString(8)%>


<% Msg = 1;%>
<p>
<%}%>
<%if (Msg == 0) out.print("No Data!<br>");%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
}
catch (ClassNotFoundException s) {
out.println(s);
}
catch (SQLException e) {
out.println(e);
}
%>
</body>
</html>


----------------解决方案--------------------------------------------------------

你用ODBC数据源
试试这段程序能不能运行

import java.sql.*;

public class sql{
public static void main( String[] args )throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String dbUrl = "jdbc:odbc:aaa";//aaa是数据源的名字
Connection conn= DriverManager.getConnection(dbUrl,"","");
Statement stmt=conn.createStatement();
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
String a=rs.getString(1);
System.out.print(a);
}
}
}


----------------解决方案--------------------------------------------------------

这跟3楼的有什么区别啊?
不行啊,
试过了

还有个问题,
如果这个用数据源的话,那不是要在服务器上配置数据源啊?服务器是别人的,怎么配置啊?


----------------解决方案--------------------------------------------------------

那只能找驱动了
我是没用过access


----------------解决方案--------------------------------------------------------
以下是引用xqiang在2006-9-20 9:21:06的发言:

这跟3楼的有什么区别啊?
不行啊,
试过了

还有个问题,
如果这个用数据源的话,那不是要在服务器上配置数据源啊?服务器是别人的,怎么配置啊?

那就配不了啦,就要驱动了
----------------解决方案--------------------------------------------------------

那有谁知道哪里有这个驱动的?
谢谢啊


----------------解决方案--------------------------------------------------------
  相关解决方案