Well, this is a little odd, I installed succesfully the driver, and in the services tab, my database tables show correctly, but when I call the driver from code I get:
java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:\Users\sample.mdb
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at sampleProject.TestDBConnection.main(TestDBConnection.java:16)
Here's my code:
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestDBConnection {
public static void main(String[] args) {
try
{
// Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("sample.mdb");
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\sample.mdb");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM tableSample");
while(rs.next())
System.out.println(rs.getString("RunnersSample") );
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
Anyone knows why I cant acces from code? Thanks in advance!
For some reason you're probably using a different ucanaccess.jar (likely built from the sources) from that in the current distribution: in this jar there must be the META-INF/services/java.sql.Driver file. Also, the JDK must be a JDK at least Java 6.
EDIT:
The exception is unambiguous: the driver isn't registered.
One time, before java 6, you had to invoke Class.forName in order to register the driver, so that a static block initializer of the driver class was invoked in turn, registering the driver.
Starting from java 6, you need no more to do that, because someone else does it for you. But the driver has to specify by contract the name of the driver class in the content of the file META-INF/services/java.sql.Driver (unizip the ucanaccess.jar to ensure that this file exists and its content is correct). And in the NetBeans services, if a driver weren't compliant, NetBeans would likely call Class.forName for compatibility with old drivers. Notice that the META-INF/services/java.sql.Driver had been already put inside ucanaccess 2.0.4.1(but this is such a old version!) so maybe you're using a different jar from that in the distribution.
In particular, if there isn't a META-INF/services/java.sql.Driver file, it means your ucanaccess.jar isn't that of the sourceforge distribution.
EDIT 2: Obviously, YOU ALWAYS MUST IMPORT the ucanaccess.jar and the four dependencies jars(hsqldb, jackcess, commons-lang, and commons-logging) into your NetBeans project libraries, even if you configured ucanaccess as service. This was an assumption of my considerations above.