Tomcat 7:连接到 Microsoft Access 时如何指定数据库资源
我知道如何将特定数据源连接池添加到 Tomcat 7 中的 mysql 服务器,添加
<Resource type="javax.sql.DataSource"
name="jdbc/TestDB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysql"
username="mysql_user"
password="mypassword123"
/>
到我的 META-INF/context.xml
中,
- 任何人都可以告诉我如何对
Microsoft Access 执行此操作
代码>? - 我是否需要 JDBC - ODBC 桥来从我的 Java Web 应用程序连接到 Microsoft Access?
更新:这是我的尝试,但我遇到了SQLException
<Resource type="javax.sql.DataSource"
name="jdbc-odbc/qtl"
maxActive="100"
maxIdle="30"
maxWait="10000"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="sun.jdbc.odbc.JdbcOdbcDriver"
url="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=/Users/KingdomHeart/resources/db.mdb"
/>
当我这样做时,我得到警告:意外的异常解析参考 java.sql.SQLException:sun.jdbc.odbc.JdbcOdbcDriver
更新2::我尝试编写一个单独的程序来连接到Microsoft Access db。我得到 java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
这是我的代码。肯定是我的类路径中需要一个 jar
驱动程序,不是吗?
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String fileName = "/Users/KingdomHeart/resources/MyTable.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database += fileName + ";DriverID=22;READONLY=true";
Connection con = DriverManager.getConnection(database, "", "");
System.out.println("here");
} catch (SQLException ex) {
Logger.getLogger(TestJdbcOdbc.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(TestJdbcOdbc.class.getName()).log(Level.SEVERE, null, ex);
}
}
I know how to specific datasource connection pooling to a mysql server in Tomcat 7, add
<Resource type="javax.sql.DataSource"
name="jdbc/TestDB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysql"
username="mysql_user"
password="mypassword123"
/>
to my META-INF/context.xml
,
- Can anyone show me how to do this to
Microsoft Access
? - Do I need a JDBC - ODBC bridge to make connection from my Java web app to Microsoft Access?
UPDATE: This is my attempt, but I ran into SQLException
<Resource type="javax.sql.DataSource"
name="jdbc-odbc/qtl"
maxActive="100"
maxIdle="30"
maxWait="10000"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="sun.jdbc.odbc.JdbcOdbcDriver"
url="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=/Users/KingdomHeart/resources/db.mdb"
/>
When I do this, I got WARNING: Unexpected exception resolving reference
java.sql.SQLException: sun.jdbc.odbc.JdbcOdbcDriver
UPDATE 2:: I tried to write a separate program in effort to make connection to Microsoft Access db. I got java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Here is my code. It must be that I need a jar
driver in my class path, do I not?
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String fileName = "/Users/KingdomHeart/resources/MyTable.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database += fileName + ";DriverID=22;READONLY=true";
Connection con = DriverManager.getConnection(database, "", "");
System.out.println("here");
} catch (SQLException ex) {
Logger.getLogger(TestJdbcOdbc.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(TestJdbcOdbc.class.getName()).log(Level.SEVERE, null, ex);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会使用这样的 URL:
JDBC 驱动程序类是
sun.jdbc.odbc.JdbcOdbcDriver
(至少是我上次使用它时;那是在 2004 年)。我更喜欢这种风格,因为它消除了创建 ODBC 数据源的步骤。它保留了 Java 部署中文件的精确位置。
是的,您需要 JDBC-ODBC 桥(除非您购买了商业 JDBC 驱动程序)。
您应该了解在多用户环境中使用 Access 的陷阱:
http://msdn.microsoft.com/en-us/library/aa167840(v=office.11).aspx
我个人的建议是使用 MySQL 或PostgreSQL 而不是 Access。
您可能还想阅读以下有关在 Tomcat 上设置 JNDI 数据源的其他内容:
http://craicpropagation.blogspot.com/2009/02/how-to-use-same-jndi-resource-name-on.html
I'd use a URL like this:
The JDBC driver class is
sun.jdbc.odbc.JdbcOdbcDriver
(at least it was when I last used it; that was back in 2004).I prefer this style because it eliminates the step of creating an ODBC data source. It keeps the precise location of the file inside the Java deployment.
Yes, you need the JDBC-ODBC bridge (unless you bought a commercial JDBC driver).
You should be aware of the pitfalls of using Access in a multi-user environment:
http://msdn.microsoft.com/en-us/library/aa167840(v=office.11).aspx
My personal recommendation would be to use MySQL or PostgreSQL instead of Access.
Here's something else you might want to read about setting up a JNDI data source on Tomcat:
http://craicpropagation.blogspot.com/2009/02/how-to-use-same-jndi-resource-name-on.html