我目前正在为我的大学开设一个班级项目。我正在学习连接和操作数据库,我们正在使用Microsoft .accdb文件。
这是我到目前为止所拥有的。
/* 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();
}
在寻找“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$9.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.access$100(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)
看起来很简单。看来我错过了司机或其他类似的东西。但是我很难找到该做些什么。驱动程序是否随JDK一起提供?我必须单独下载一些东西?它取决于我的操作系统吗? (Mac OS X 10.9)。或者我只是在代码中做错了什么?
我很感激帮助!谢谢!
其他信息:
[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: ~]$
答案 0 :(得分:17)
正如对该问题的评论中所提到的,JDBC-ODBC Bridge - 正如其名称所示 - 只是JDBC层与ODBC层“对话”的机制。即使你的Mac上有一个JDBC-ODBC Bridge,你也需要
因此,对于大多数人来说,使用JDBC-ODBC Bridge技术来操作ACE / Jet(“Access”)数据库实际上只是在Windows下才是实用选项。同样重要的是要注意在Java 8中删除了JDBC-ODBC Bridge (参考:here)。
还有其他方法可以从Java操作ACE / Jet数据库,例如UCanAccess和Jackcess。这两个都是纯Java实现,因此它们可以在非Windows平台上运行。有关如何使用UCanAccess的详细信息,请参阅
答案 1 :(得分:0)
我也没有使用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());
}
}
}