Unable to connect to a database on a shared drive

2019-02-26 01:23发布

问题:

I've setup my application to connect to a Access database through UCanAccess however I'm unable to connect to a database that is located on the shared drive. See below for examples.

String databasePath = "jdbc:ucanaccess://C:/Desktop/MyDB.accdb"  \\\\ this works
String databasePath = "jdbc:ucanaccess://servername/etc/MyDB.accdb" \\\\ does not work and throws no suitable driver found exception

Interesting enough when I map my folder on the shared drive to some letter e.g. P: - it successfully connects.

String databasePath = "jdbc:ucanaccess://P:/servername/etc/MyDB.accdb"  \\\\ this works

Is it possible to go around this? My users have r/w permissions to the folder but I don't want to map drives on each machine. Thanks

回答1:

You simply don't have enough forward slashes in your connection URL to represent a UNC path. As you have noted, for a local connection to C:\Desktop\MyDB.accdb you can use

String databasePath = "jdbc:ucanaccess://C:/Desktop/MyDB.accdb";

Similarly, for a UNC connection to \\servername\etc\MyDB.accdb you can use

String databasePath = "jdbc:ucanaccess:////servername/etc/MyDB.accdb";


回答2:

The database driver can only handle his specified protocolls. Most of jdbc drivers can only handle the network protocol for his specific database server.

You are using the access jdbc driver which can only handle file access to a given accdb file.

When you try an url like:

jdbc:ucanaccess://servername/etc/MyDB.accdb

The driver makes a local file access to this path which does not exist localy. What you expect is, that the driver would make up a SMB/CIFS connection to your server, which he can't!

You have to map the SMB/CIFS share over your OS on to a local drive letter. Because then the OS is handling the network SMB/CIFS stuff for you. And the JDBC driver can access the file as it would access a local file.