How to find MS Access version number in java code - java

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

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.

How to connect to Access .mdb database from 64-bit 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, "", "");

How is driver class located in JDBC4

One of the great additions in version 4 of JDBC You don't have to explicitly load the
driver by calling Class.forName anymore. When your application attempts to connect the database for the first time, DriverManager automatically loads the driver found in
the application CLASSPATH.
My question is how? What if there are multiple drivers in the classpath?
One thing I can guess is that on parsing the connection URL whether driver needed is of JDBC or ODBC can be figured out but how can one say out of multiple jdbc compliant drivers which one is to be selected for the database I am using? (lets say I am using MySql and I need MySql-Connector driver). Is there any static mapping of such database drivers in JVM?
Every JDBC 4 compliant driver has a file in its jar called META-INF/services/java.sql.Driver, in that file it will list its implementation(s) of java.sql.Driver. When you request a connection, DriverManager will use the ServiceLoader to find all(!) copies of META-INF/services/java.sql.Driver in the classpath and will then load all classes listed. When a java.sql.Driver class is loaded, it has to register itself with the DriverManager, so the DriverManager loads all classes using the service loader, and each Driver implementation registers itself.
When you request a connection from DriverManager, the DriverManager will iterate over all registered drivers asking them for a Connection. The driver will use the JDBC url to check if it's a protocol it supports (eg Jaybird/Firebird JDBC checks if the url starts with "jdbc:firebirdsql:" or "jdbc:firebird:"). If the driver does not support the protocol, it will return null, if it does support the protocol it will either return an established connection, or it will throw an SQLException (eg if you made an error in the URL, or it couldn't connect). If all drivers return null (none support the protocol), then DriverManager will throw an SQLException with error "No suitable driver found for <url>"
So, having multiple drivers on the classpath does not matter as long as they support different protocols, however if there are multiple drivers for the same database (or at least: same protocol prefixes), it will use the first in the list of drivers. Depending on the Java version, if that driver fails with an SQLException, it will continue with the next driver (at least Java 5 and later), or stop trying and throw the exception (I believe this was in Java 1.4 or maybe even earlier).
Some information about JDBC4 driver loading taken from : http://www.onjava.com/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html
When the method getConnection is called, the DriverManager will
attempt to locate a suitable driver from among the JDBC drivers that
were loaded at initialization and those loaded explicitly using the
same class loader as the current application.
The DriverManager methods getConnection and getDrivers have been
enhanced to support the Java SE Service Provider mechanism (SPM).
According to SPM, a service is defined as a well-known set of
interfaces and abstract classes, and a service provider is a specific
implementation of a service. It also specifies that the service
provider configuration files are stored in the META-INF/services
directory. JDBC 4.0 drivers must include the file
META-INF/services/java.sql.Driver. This file contains the name of the
JDBC driver's implementation of java.sql.Driver. For example, to load
the JDBC driver to connect to a Apache Derby database, the
META-INF/services/java.sql.Driver file would contain the following
entry:
org.apache.derby.jdbc.EmbeddedDriver
Now coming to your question.
My question is how? What if there are multiple drivers in the
classpath?
As a class loader rule, any class found first will be loaded and if it is already loaded then will not be reloaded by the class loader.
Answer for your question will get it from java.sql.DriverManager java class ,method loadInitialDrivers.
drivers = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty("jdbc.drivers");
}
});
If program found multiple jdbc driver,it will use below logic to take exact driver
String[] driversList = drivers.split(":");
println("number of Drivers:" + driversList.length);
for (String aDriver : driversList) {
try {
println("DriverManager.Initialize: loading " + aDriver);
Class.forName(aDriver, true,
ClassLoader.getSystemClassLoader());
} catch (Exception ex) {
println("DriverManager.Initialize: load failed: " + ex);
}

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