How to connect to Access .mdb database from 64-bit Java? - java

Hi I have the below code to connect to MS Access database on Windows 7 OS. I have changed the Data Source short cut to point to 64bit odbc then 32 bit. But still getting the error as
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at TestDBConnection.main(TestDBConnection.java:21)
And my code is :
import java.sql.Connection;
import java.sql.DriverManager;
public class TestDBConnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
System.out.println("filename");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database =
"jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Test\\Tests.mdb";
Connection conn = DriverManager.getConnection(database, "", "");
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
How ever I have SQL Workbench tool through which I can connect to it but not through java code.
Please need help badly as I am struggling with this from past 3 hours searching on Google.

If your Java app is running in a 64-bit Java Virtual Machine (JVM) then DRIVER={Microsoft Access Driver (*.mdb)} is not going to work because there is no 64-bit version of the Jet database engine. You can...
Download and install the 64-bit version of the Microsoft Access Database Engine from here, and then use DRIVER={Microsoft Access Driver (*.mdb, *.accdb)} in your code.
... or ...
Run your Java app in a 32-bit JVM and continue to use the existing DRIVER= string. The related answer here might prove helpful if you choose this option.
... or ...
Use the UCanAccess JDBC driver for Access databases. It is a free, open-source, pure Java implementation so it works on both 32-bit and 64-bit systems, both Windows and non-Windows. It also works with Java 8 (which has dropped the JDBC-ODBC Bridge). For more details, see:
Manipulating an Access database from Java without ODBC

You can install the 64 ODBC drivers for Access available from Microsoft
http://www.microsoft.com/en-us/download/details.aspx?id=13255

1) you will have to configure System dsn (Driver Microsoft Access Driver(.mdb,.accdb))
2) link .mdb database in above configuration
and write below code.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:systemdsnname";
Connection conn = DriverManager.getConnection(database, "", "");

Related

Connect To SQL Server With Windows Authentication From A Linux Machine Through JDBC

I want to be able to connect to a SQL Server using jdbc and windows authentication.
I saw some answers on the internet saying i should add the following property to the connection string:
integratedSecurity=true;
And also add
sqljdbc_auth.dll
To the java path.
But this, as far as i understand applies only when i'm connecting from a Windows machine.
When i try this on a Linux machine i get:
java.sql.SQLException: This driver is not configured for integrated authentication
My question is how do I do it from a Linux machine.
Thanks
Well, eventually I answer my own question:
This is not possible to use Windows authentication from a linux machine using the Microsoft JDBC driver.
This is possible using the jTDS JDBC driver using the following connection string:
jdbc:jtds:sqlserver://host:port;databaseName=dbname;domain=domainName;useNTLMv2=true;
Thank you all for all the comments
TL;DR
It is not possible to use native Windows Authentication for JDBC connections to MSSQL from a JVM running on Linux.
This MSDN article explains the authentiation methods with JDBC on Linux, potential errors, and available options:
https://blogs.msdn.microsoft.com/psssql/2015/01/09/jdbc-this-driver-is-not-configured-for-integrated-authentication/
...in the JDBC 4.0 driver, you can use the authenticationScheme
connection property to indicate how you want to use Kerberos to
connect to SQL. There are two settings here.
NativeAuthentication (default) – This uses the sqljdbc_auth.dll and is specific to the Windows platform. This was the only option
prior to the JDBC 4.0 driver.
JavaKerberos – Makes use of the Java API’s to invoke kerberos and does not rely on the Windows Platform. This is java specific and not
bound to the underlying operating system, so this can be used on both
Windows and Linux platforms.
...
The following document outlines how to use Kerberos with the JDBC
Driver and walks through what is needed to get JavaKerberos working
properly.
Using Kerberos Integrated Authentication to Connect to SQL Server
http://msdn.microsoft.com/en-us/library/gg558122%28v=sql.110%29.aspx
For those who are using DBeaver the way to connect to the SQL Server Database is:
In order to connect to the SQL Server from Linux Debian using DBeaver
1.- Select SQL Server jTDS driver
2.- Enter the connection information
3.- Go to Driver Properties tab and add the domain, user, password
Just as a note, in some post I found that they needed to change the property USENTLMV2 to TRUE but it worked for me either by putting the USERTLNMV2 in true or false.
A problem that I found was that when I was trying to connect to the database using my user and password the next error was thrown:
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
This error was thrown because of my user was about to expire. I tried with another AD user and it could connect.
I know this is kind of an older topic but in case Google sends people here:
There are two main JDBC drivers for SQL Server. One is from Microsoft and the other from jTDS. jTDS can, amazingly, connect using Windows auth (NTLM) from other platforms, including Linux, as described here: http://jtds.sourceforge.net/faq.html#windowsAuth. It can, of course, also use SQL-authenticated logins. SQL-authenticated logins are no harder to use from any OS than any other, so don't forget about those an option.
The version provided by Microsoft is the one from which #mjn provided a quote from the documentation. It is able to connect using Windows authentication by specifying integratedSecurity=true, authenticationScheme=javaKerberos, and authentication=NotSpecified.
It is tricky to get this working even if you don't go out of your way to find more confusion, so always keep in mind which driver you are using - and tell us in these posts so that you can get more specific help.
This JDBC URL is validated to work with latest Microsoft SQL Server JDBC driver:
jdbc:sqlserver://[server]:[port];database=[db\;trustServerCertificate=true;integratedSecurity=true;user=[user without domain];password=[pw];authenticationScheme=NTLM;domain=[domain];authentication=NotSpecified
Example:
jdbc:sqlserver://mysql.myorg.com:1433;database=mydb;trustServerCertificate=true;integratedSecurity=true;user=myuser;password=mypwd;authenticationScheme=NTLM;domain=ad.myorg.com;authentication=NotSpecified
I was able to connect to a SQL Server 2016 Data Mart and JDBC connection Microsoft JDBC Driver using Windows Authentication using the following script on a Ubuntu Linux Docker Image running on Windows 10.
# initializes spark session
from pyspark.sql import SparkSession
spark = SparkSession\
.builder\
.master('local[*]')\
.appName('FDM')\
.config("spark.driver.extraClassPath","pyspark_jars/*")\
.config('spark.executor.memory', '4g')\
.config('spark.driver.memory', '16g')\
.config('spark.executor.cores', '4')\
.getOrCreate()
jdbc_url = '''jdbc:sqlserver://SERVER;databaseName=DBNAME;trustServerCertificate=true;integratedSecurity=true;user=USERID;password=PASSWORD;authenticationScheme=NTLM;domain=US;authentication=NotSpecified'''
spark_df = spark.read\
.format("jdbc")\
.option("url", jdbc_url)\
.option("driver","com.microsoft.sqlserver.jdbc.SQLServerDriver")\
.option("query", 'select top(1000) * from SCHEMA.TABLE')\
.option("fetchsize", 100000)\
.load()
spark_df.write.csv('TEST.csv', mode = "overwrite", header=True)

JDBC-ODBC Connection

I am trying to connect MS Access with Java
Below is the error message
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Code-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb,*.accdb)};DSN=C:\\ST\\BillingTrackerDB.mdb";
Connection conn=DriverManager.getConnection(database, "", "");
DRIVER={Microsoft Access Driver (*.mdb,*.accdb)}
is an invalid ODBC driver name for Access; it is missing a space. The correct name is
DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}
What version of Java are you using? In Java 8, I was unable to use the JDBC-ODBC bridge as it has been removed in JDK 8. What I have found to use in place of the JDBC-ODBC bridge for connecting to Access Databases is a pure Java solution called UCanAccess. It has done everything I needed, so far.

ClassNotFoundException when trying to connect to .accdb file via JDBC-ODBC in Java 8

I'm working in a project in Java in Eclipse EE IDE where I have to query a .accdb file.
The problem is when I try to load the driver and then connect to the database it gives me an exception error.
My code:
try{
String filePath = "//myfilepathtomydb/BLABLA/example.accdb"
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + filePath;
Connection database = DriverManager.getConnection(url);
System.out.println("Connection sucessful");
} catch (ClassNotFoundException e){
System.err.println("Got an exception");
System.err.println(e.getMessage());
e.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
// TODO: handle exception
}
The exception:
Got an exception
sun.jdbc.odbc.JdbcOdbcDriver
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at project.Main.main(Main.java:15)
I'm using 32-bit Eclipse in a 64-bit Windows and from what i've read this way of connecting to the database is not supported by a 64-bit JRE, so I'm using a chosen 32-bit JRE (jdk1.8.0_05) and in my run configurations I used the '-d32' argument in VM.
Apparently the JdbcOdbcDriver should be inside the rt.jar, but when i look for it i can't find the following package: sun.jdbc.odbc.JdbcOdbcDriver.
Would be appreciated if someone could shed light to my problem, any mistakes or stupid things i said fell free to correct me also.
According to this post the JDBC-ODBC Bridge was removed in Java8. You can use a JDBC Driver specifically for Access. I see a lot of people mentioning UCanAccess to connect to Access although I have not used it myself.
According to Oracle the JDBC-ODBC Bridge will no longer be included with JDK as of Java SE 8 and that
The ideal is "Pure Java": no native code, no platform dependent features.
that means looking for at least a JDBC type 3 or 4.
Lance Andersen wrote:
The JDBC-ODBC Bridge has always been considered transitional and a non-supported product that was only provided with select JDK bundles and not included with the JRE.
The JDBC-ODBC bridge provides limited support for JDBC 2.0 and does not support more recent versions of the JDBC specification.
You should either use any of third-part database drivers (as Microsoft doesn't provide for any) or use a previous version of java.
Anyhow I suggest using a specific Driver, instead of the JDBC-ODBC one.
For that you could look at any of the following:
EasySoft drivers
UCanAccess
Oracle's (and Sun's) official position has long (since JDBC 1.0!) been that --
the [JVM-bundled] JDBC-ODBC Bridge should be considered a transitional solution [...] Oracle [was Sun] does not support the JDBC-ODBC Bridge.
However, my employer, OpenLink Software, has produced enterprise-grade commercial Type 1 Bridges between JDBC and ODBC since JVM 1.0, and current versions are fully compatible with the current JVM 1.8. You can learn more here --
Single-Tier JDBC-ODBC Bridge Driver -- a JDBC driver for ODBC data sources
Single-Tier ODBC-JDBC Bridge Driver -- an ODBC driver for JDBC data sources
ODBC Bridge might not exist in JRE rather than SUN's. Check your JRE.

How to find MS Access version number in java code

Is there a way to find version number of MS Access using java code. I am using jdbc-odbc bridge to connect with MS Access.
My usecase is to load mdb and accdb driver using JDBC
DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver(.mdb,.accdb);}DBQ=filename")
If i use access 2003 or previous version, accdb driver will not be installed. So, while loading it throws exception. What is the way to resolve it.
Thanks in advance.
Regards,
Ganesan
From the java.sql.Connection you have getMetaData() to get the DatabaseMetaData. Several methods are available here (eg. getDatabaseMajorVersion()) to get version information, if the Driver implement this info.
To use the driver, it must be installed and registered. That you can check:
Enumeration<Driver> driverlist = DriverManager.getDrivers();
while(driverlist.hasMoreElements()) {
System.out.println(driverlist.nextElement().getClass());
}
https://www.tutorialspoint.com/how-to-get-the-list-of-all-drivers-registered-with-the-drivermanager-using-jdbc

Cannot connect to *.dbf file through JDBC drivers

i'm trying to connect to *.dbf (dBase III) file on my Java application, running on a Windows Server 2003 system.
I'm encountering this error and I cannot really understand the meaning (sources for OdbcJdbc.java seems to be unavailable):
[Microsoft][ODBC dBase driver] '(unknown)' is not a valid path error
This is the code I run on my application:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:DRIVER={Microsoft dBase Driver(*.dbf)};DBQ=D:\\dbNeri\\CARISTAT;";
Connection conn = DriverManager.getConnection(database);
Statement s = conn.createStatement();
String selTable = "SELECT * FROM CARISTAT";
Does it exists a JDBC driver able to connect to dBase files or do I have to import external libraries to do the magic?
Thanks in advance for your help!
Ok guys, I finally found the answer to all my problems.
Without any need to configure a ODBC data source, the mistake in that code was I've targeted directly in my connection string the file name I would like to access.
(In the code up here, I removed "\CARISTAT").
Thus the application run easily and with no more JDBC driver error!!
Thanks anyway for your support!
Are you able to set up a DSN for that database (using ODBC Administrator)? Maybe you just don't have the dBase ODBC drivers installed on that server?
Dbase III is a 16-bit product and Windows Server is a 64-bit environment, which is what causes the compatibility issue.
You will have to use dBDOS to use your DOS based dBase to run our dBase applications on 64-bit platforms.
These sites have more information:
http://pmcgee#dbasellc.com
http://www.dbase.com

Categories