Java JDBC ODBC 驱动程序连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19709843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JDBC ODBC Driver Connection
提问by James Manes
I'm currently working on a project for a class at my University. I am learning about connecting and manipulating databases and we are using a Microsoft .accdb file.
我目前正在为我大学的一个班级做一个项目。我正在学习连接和操作数据库,我们正在使用 Microsoft .accdb 文件。
Here is what I have so far.
这是我到目前为止所拥有的。
/* Perform database operations */
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
connection = DriverManager.getConnection("jdbc:odbc:Lab3.accdb");
System.out.println("Database connected");
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("");
while (resultSet.next()) {
output.write("<p>" + resultSet.getString(1) + "\t" +
resultSet.getString(2) + "\t" + resultSet.getString(3) + "</p>");
}
connection.close();
} catch (ClassNotFoundException|SQLException e) {
System.out.println("Database Access Error.");
e.printStackTrace();
}
When looking for "sun.jdbc.odbc.JdbcOdbcDriver" I am getting the following output.
在寻找“sun.jdbc.odbc.JdbcOdbcDriver”时,我得到以下输出。
SEVERE: java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1761)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at changepass.ChangePasswordServlet.processRequest(ChangePasswordServlet.java:47)
at changepass.ChangePasswordServlet.doGet(ChangePasswordServlet.java:83)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
It seems simple enough to work through. It seems I am missing the driver or something along those lines. However I am having a hard time finding out what to do about this. Is the driver included with the JDK anywhere? Must I download something separate? Does it depend on my Operating System? (Mac OS X 10.9). Or am I simply doing something wrong in code?
这似乎很容易解决。看来我错过了驱动程序或类似的东西。但是,我很难找到对此做些什么。JDK 中是否随附了驱动程序?我必须单独下载一些东西吗?它取决于我的操作系统吗?(Mac OS X 10.9)。或者我只是在代码中做错了什么?
I would appreciate the help! Thanks!
我将不胜感激!谢谢!
Additional Info:
附加信息:
[James@toodarkpark: ~]$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
[James@toodarkpark: ~]$
采纳答案by Gord Thompson
As mentioned in the comments to the question, the JDBC-ODBC Bridge is - as the name indicates - only a mechanism for the JDBC layer to "talk to" the ODBC layer. Even if you had a JDBC-ODBC Bridge on your Mac you would also need to have
正如对问题的评论中所提到的,JDBC-ODBC 桥(顾名思义)只是 JDBC 层与 ODBC 层“对话”的一种机制。即使您的 Mac 上有 JDBC-ODBC Bridge,您也需要有
- an implementation of ODBC itself, and
- an appropriate ODBC driver for the target database (ACE/Jet, a.k.a. "Access")
- ODBC 本身的实现,以及
- 目标数据库的适当 ODBC 驱动程序(ACE/Jet,又名“Access”)
So, for most people, using JDBC-ODBC Bridge technology to manipulate ACE/Jet ("Access") databases is really a practical option only under Windows. It is also important to note that the JDBC-ODBC Bridge will behas been removed in Java 8 (ref: here).
所以,对于大多数人来说,使用 JDBC-ODBC Bridge 技术来操作 ACE/Jet(“Access”)数据库确实是只有在 Windows 下才有的实际选择。同样重要的是要注意 JDBC-ODBC Bridge将在 Java 8 中被删除(参考:这里)。
There are other ways of manipulating ACE/Jet databases from Java, such as UCanAccessand Hymancess. Both of these are pure Java implementations so they work on non-Windows platforms. For details on how to use UCanAccess see
还有其他方法可以从 Java 操作 ACE/Jet 数据库,例如UCanAccess和Hymancess。这两个都是纯 Java 实现,因此它们可以在非 Windows 平台上运行。有关如何使用 UCanAccess 的详细信息,请参阅
回答by Lokesh Gajbhiye
Didn't work with ODBC-Bridge for me too. I got the way around to initialize ODBC connection using ODBC driver.
也不适用于我的 ODBC-Bridge。我找到了使用 ODBC 驱动程序初始化 ODBC 连接的方法。
import java.sql.*;
public class UserLogin
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// C:\databaseFileName.accdb" - location of your database
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\emp.accdb";
// specify url, username, pasword - make sure these are valid
Connection conn = DriverManager.getConnection(url, "username", "password");
System.out.println("Connection Succesfull");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}