作者:龙叔君_541 | 来源:互联网 | 2023-08-10 12:36
ImworkingonawebapplicationusingnetbeansandMSAccesasmydatabase,inmyconnectionclass
I'm working on a web application using netbeans and MS Acces as my database, in my connection class I tried to get the path of my acces file (located inside my project) via the following command:
我正在使用netbeans和MS Acces作为我的数据库的Web应用程序,在我的连接类中,我试图通过以下命令获取我的acces文件的路径(位于我的项目中):
File f = new File("softTech.accdb");
String path = f.getAbsolutePath();
The problem is, as soon as I run the project and it tries to connect, when trying to get the path, the system gives me a path inside the tomcat's paste
问题是,一旦我运行项目并尝试连接,当尝试获取路径时,系统会在tomcat的粘贴内部给出一条路径
I need your help, don't know what to do Thanks in advance
我需要你的帮助,不知道该怎么做在此先感谢
2 个解决方案
1
There are two points:
有两点:
First: MS Access is really the worst choice as a database for a Java web application.
第一:作为Java Web应用程序的数据库,MS Access确实是最糟糕的选择。
MS Access is a desktop database not made for using it at the server side. The JDBC-ODBC bridge was never meant for production use and was removed in Java 8. The alternate driver Ucanaccess is nice for data exchange scenarios with a MS Access database but it uses a pure Java database (HSQLDB) as a buffer and an emulation layer to avoid the use of the original Jet Engine. This alone is a performance nightmare.
MS Access是一个桌面数据库,不是在服务器端使用它。 JDBC-ODBC桥从未用于生产用途,并且已在Java 8中删除。备用驱动程序Ucanaccess适用于具有MS Access数据库的数据交换方案,但它使用纯Java数据库(HSQLDB)作为缓冲区和仿真层避免使用原装Jet Engine。仅这一点就是表演的噩梦。
So you should consider to use another database for your web application. There are plenty of alternatives like SQLite, Apache Derby or H2 as embedded database engines or MySQL, PostgreSQL as client-server database systems. All with dedicated JDBC driver support.
因此,您应该考虑为Web应用程序使用另一个数据库。有许多替代方案,如SQLite,Apache Derby或H2作为嵌入式数据库引擎或MySQL,PostgreSQL作为客户端 - 服务器数据库系统。全部具有专用的JDBC驱动程序支持
Second: The database path or the connection shouldn't be hardcoded inside your web application. You should configure a named datasource in your application server (e.g. Tomcat). And inside your web application you can access the datasource via JNDI.
第二:不应在Web应用程序中硬编码数据库路径或连接。您应该在应用程序服务器(例如Tomcat)中配置命名数据源。在Web应用程序中,您可以通过JNDI访问数据源。
Example: Configure the database connection factory in Tomcat:
示例:在Tomcat中配置数据库连接工厂:
...
...
Example: Access the datasource via JNDI:
示例:通过JNDI访问数据源:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
Connection cOnn= ds.getConnection();
... use this connection to access the database ...
conn.close();
The examples are part of the Tomcat documentation.
这些示例是Tomcat文档的一部分。
The benifit is, the web application only knows the logical database name. You can have multiple Tomcat installation, e.g. one for testing with a test database and one production. You can use the same WAR file for both installations. Each Tomcat has to be configured only once.
好处是,Web应用程序只知道逻辑数据库名称。您可以安装多个Tomcat,例如一个用于测试数据库和一个生产。您可以对两个安装使用相同的WAR文件。每个Tomcat只需配置一次。