I am attempting to run some queries using JDBC and keep getting this error:
Exception in thread "main" java.lang.IllegalStateException: error
at com.mycompany.app.App.writer(App.java:195)
at com.mycompany.app.App.main(App.java:19)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname
here is the relevant part of my code:
public class App {
writer();
}
public static void writer() {
String url = "jdbc:mysql://localhost:3306/dbname
String username = "root";
String password = "password";
try (Connection connection = DriverManager.getConnection(url, username, password)) {
Statement st = connection.createStatement();
ResultSet r= st.executeQuery("insert query here");
} catch (SQLException e) {
throw new IllegalStateException("error");
}
}
}
When I run it through Intellij Idea it works, but I need it to run on a server running Centos.
I have tried running it with this commands:
javac -cp "filepath/mysql-connector-java-5.1.35-bin.jar" App.java
java -cp ".filepath/mysql-connector-java-5.1.35-bin.jar" App
I have tried running it using maven, including
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
in pom.xml and still get the same error
I have looked through many articles online (and stack questions) and still can't find the solution.
The server is running CentoOS 6.6 and the database is running locally.
I am using:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
Entries on the classpath on Unix-like operating systems must be separated by :. Add a : between . (which indicates the current directory) and the path of the jar:
java -cp .:filepath/mysql-connector-java-5.1.35-bin.jar App
Alternatively, you may place your mysql-connector-java-5.1.35-bin.jar to the following directory -
JAVA_HOME/jre/lib/ext
If you place the jar in this directory then you don't have to add it to classpath by using command.
You can try loading the driver class using class.forName("qualified name of driver class"). This should resolve the issue if you're not facing the issue of class path with maven. Have a look at this where
the index tag was removed from pom and it worked.
Related
I'm trying to connect to SQL DB in my Maven project, but keep getting following exception:
"com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ...", "..Caused by: java.lang.UnsatisfiedLinkError: no mssql-jdbc_auth-8.2.1.x64 in java.library.path....".
I've tried out suggestions from no sqljdbc_auth in java.library.path and UnsatisfiedLinkError: no sqljdbc_auth in java.library.path but it didn't work for me.
I've put the path to sqljdbc_auth.dll in:
Global PATH variable Global PATH variable screenshot
C:\Program Files\Java\jdk-13.0.2\bin C:\Program Files\Java\jdk-13.0.2\bin screenshot
pom.xml (as a configuration in surefire plugin dependency)
pom.xml screenshot
And here is my code:
public class JDBC {
#Test
public void test() throws SQLException, ClassNotFoundException {
String UserName="sa";
String Password="Error911";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String DB_URL ="jdbc:sqlserver://localhost:1433;databaseName=QADB;integratedSecurity=true;";
//OR by using ip
//DB_URL ="jdbc:sqlserver://192.168.0.104;databaseName=QADB;integratedSecurity=true;";
Connection con = DriverManager.getConnection(DB_URL, UserName, Password);
}
}
And the exception in console output:
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not
configured for integrated authentication...........................
...Caused by: java.lang.UnsatisfiedLinkError: no mssql-jdbc_auth-8.2.1.x64 in java.library.path:
[C:\Users\Automation\Microsoft JDBC Driver 6.0 for SQL
Server\sqljdbc_6.0\enu\auth\x64]..
....
it seems that you don't have the mssql-jdbc_auth-8.2.1.x64 file in your classpath.
As far as I know that file is included in the Microsoft SQL JDBC driver (enu/auth/x64 folder): https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15
You can add that file to your classpath (for example copy it to: C:\Program Files\Java\jdk-13.0.2\bin) and fix the error.
I had this same problem, and it took me hours to figure out.
Make sure that you're copying the mssql-jdbc_auth-8.2.1.x64 file and not the sqljdbc_xa.dll file into the C:\Program Files\Java\jdk-13.0.2\bin folder. According to the screenshot of your bin folder, I don't think this is the issue.
Restart Eclipse and run it again. I was copying the file into the bin folder with eclipse running and I had no success. Only after I restarted eclipse did the driver begin to work correctly.
Just follow below steps and it will surely turn out to fix "no mssql-jdbc_auth-8.2.1.x64 in java.library.path" as well as "JDBC SQLServerException: “This driver is not configured for integrated authentication" issue.
Download sqljdbc_<version>_enu.zip from https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15 as per you Java version.
Unzip it, read install.txt and do as it says
Paste mssql-jdbc_auth-8.2.2.x64.dll (present inside path -> C:/Program Files/Microsoft JDBC DRIVER 8.2 for SQL Server/sqljdbc_/enu/auth/x64 to
Java/jre8/bin and to Java/jre8/lib
In order to be able to connect with the JDBC, you need to define the connection as follows:
"jdbc:sqlserver://*******;authenticationScheme=NTLM;integratedSecurity=true;domain=******;databasename=**********;encrypt=true;trustServerCertificate=true;user=*******;password=*******;"
Use the following dependency:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre8</version>
</dependency>
You should delete maven dependency:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
Then:
add dependency JDBC driver jar manually download here
This is worked for me.
I also had the same issue, and i also went through the posts that you mentioned, i resolved it by adding the mssql-jdbc_auth-8.2.2.x64.dll in \java\jre\bin\ instead of \java\bin. I was using a JDK and not a JRE.
I am trying to connect a java program to an SQL Server database.
The important is that I am working on a Windows 7 machine, my Eclipse is on a Linux machine on my company's network, and the SQL Server requests a Windows authentication.
I copied and pasted the driver sqljdbc41.jar in the location of my project (on Eclipse right click on the project, properties and see location).
I also pasted the sqljdbc_auth.dll in the same location.
I have tried to add the path to this .dll on the "Native Library location following this post : https://stackoverflow.com/a/958074/7812989
. But I still have this error :
Jul 10, 2017 10:52:35 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ClientConnectionId:39389d4c-36cd-459f-b9f8-15d3cf422b34
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2397)
at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<init>(AuthenticationJNI.java:68)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3132)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3123)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1981)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at sqlconnectiontest.SqlSelection.main(SqlSelection.java:19)
Caused by: java.lang.UnsatisfiedLinkError: no sqljdbc_auth in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1878)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1087)
at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<clinit>(AuthenticationJNI.java:41)
... 13 more
My code is the following :
package sqlconnectiontest;
import java.sql.*;
public class SqlSelection {
/**
* #param args
*/
public static void main(String[] args) {
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String userName = "myUser";
String password = "myPassword";
String url = "jdbc:sqlserver://fr0-iaascls-190.eu.company.corp:10001;databaseName=MYDATABASE;integratedSecurity=true;";
Connection con = DriverManager.getConnection(url, userName, password);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Do you have an idea that may help please ? Thank you :)
You may get this error message if the application is trying to use the incorrect architecture (x86 VS x64) version of the sqljdbc_auth.dll. Try specifying the directory path to the other architecture.
If you are on a x64 machine running x64 OS, but the JVM you are using is the x86 version, you will need to specify and use the x86 version of the sqljdbc_auth.dll.
I am trying to connect to SQL Server 2008 from JAVA code using JDBC sql server with IntegratedSecurity to use Windows Authentication.
SQL authentication worked fine with the code but when I use IntegratedSecurity for Windows Authentication, I am facing sql driver issues. I have provided the brief scenario below.
Java Code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Sqlselection
{
public static void main(String[] args)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("This programe runs on "+ System.getProperty("java.version"));
String url="jdbc:sqlserver://*****\\*****;integratedSecurity=true";
//Masked Server name & DB name
Connection con = DriverManager.getConnection(url);
Statement s1 = con.createStatement();
ResultSet rs = s1.executeQuery("SELECT count(1) myrecords FROM dbo.mytable");
String[] result = new String[20];
if(rs!=null){
while (rs.next()){
for(int i = 0; i <result.length ;i++)
{
for(int j = 0; j <result.length;j++)
{
result[j]=rs.getString(i);
System.out.println(result[j]);
}
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
Error while running the code
WARNING: Failed to load the sqljdbc_auth.dll cause : E:\POC\OMSChecker\sqljdbc_auth.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
*com.microsoft.sqlserver.jdbc.SQLServerException:* This driver is not configured for integrated authentication. ClientConnectionId:27af9d19-d144-47be-b9cf-bf646ed9bb3f
Issue root cause
sqljdbc_auth.dll is not compatible with current platform.
System properties
C:\Users>java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)
Eclipse properties
launcher: win32.x86_64_1.1.200.v20120913-144807
Used dll: E:\sqljdbc_2.0.1803.100_enu.exe\sqljdbc_2.0\enu\auth\x64\sqljdbc_auth.dll
Added External Jar: sqljdbc4.jar
Native library location: "E:\sqljdbc_2.0.1803.100_enu.exe\sqljdbc_2.0\enu\auth\x64\"
Notes
Can someone kindly help me with the fix. I am new to JAVA coding and I have tried all the solutions given for similar kind of posts.
Finally found the solution myself by re-installing Windows OS.
Used the same code above with sqljdbc4.jar & below VM argument
-Djava.library.path=E:\sqljdbc_4.0\enu\auth\x86\
I was able to connect successfully.
I've been on this class for a few days now searching the web trying to find a solution. What I am trying to do here is connect to a Access 2010 database with an extension .accdb I have been successful connecting to older databases with extensions of .mdb but not .accdb
I have tried uninstalling Office and re-installing it for x64 versions and then installing the Access x64 tools. The error I received when I use the below code is as follows:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
At this point I have no idea what could be causing this. To point out, I need to have this setup with no DSN specified because this may run on multiple machines and I do not want to have to setup and maintain a DSN on each.
String database = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Brandon\\Dropbox\\Work\\Angent Profiles\\Database1.accdb;";
Connection conn = null;
try {
conn = DriverManager.getConnection(database, "", "");
} catch (SQLException SQLE) {
System.out.println("ERROR: " + SQLE);
}
Any advice on this would be grateful.
EDIT:
C:\Windows\system32>java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
C:\Windows\system32>
EDIT:
Provider=Microsoft.ACE.OLEDB.15.0;Data Source=C:\Users\Brandon\Dropbox\Work\Angent Profiles\Database1.accdb;Persist Security Info=False
String database = "jdbc:odbc:DRIVER={Microsoft.ACE.OLEDB.15.0 (*.mdb, *.accdb)};Data Source=C:\\Users\\Brandon\\Dropbox\\Work\\Angent Profiles\\Database1.accdb;";
I believe you need to run a 32 bit JRE to connect to access databases. From my understanding there is currently no 64 bit access ODBC driver available.
I'm really looking at a mystery here. I created a Java program in Eclipse and established a JDBC connection. The code is the following:
import java.sql.*;
public class Login {
public static void main(String[] args) {
try {
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager
.getConnection("jdbc:sqlserver://localhost;databaseName=testdb; integratedSecurity=true;");
//adding port 1433 doesn't make a difference
System.out.println("Connection successful");
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from testtable");
while (rs.next()) {
line = rs.getString(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
When I'm running it within eclipse, everything's fine. The database and tables are good, library and authentication in the Java Built Path are set and I get the result I want, namely "Hello World", which is a string in the selected table.
But creating an executable JAR file and running it throws the exception "This driver is not configured for integrated authentication". It does not even connect, so even if there was an error in the database it wouldn't matter at this point.
Even more confusing is the fact, that some weeks earlier, I also created an executable JAR file and it worked!
OS: Windows Vista Home Premium x86
JDBC driver location: "C:\sqljdbc_4.0\enu\sqljdbc4.jar"
Native library location: "C:\sqljdbc_4.0\enu\auth\x86"
Java version: Java 7 Update 17 and JDK 7 Update 17
Previousely, I used Update 21, but I changed back to check if the driver could be the reason.
So, any suggestions? I'd be very grateful!
Check the runtime classpath of eclipse, then try to create a system level "CLASSPATH" variable and add the same path. Once the variable is set, execute it in new command prompt or reload the environment variables
Make sure your executable jar has the dependency in MANIFEST.MF for all the necessary jars (along with MainClass)
Finally, It is always a practice in Java to make the first alphabet in Capital ("Login" instead of "login")
Actual problem is that if you run your project from eclipse, it will automatically add run arguments that are defined in run/run config//arguments, so you run your program with properly set path to sqljdbc_auth.dll. But if you export your application to .jar file and just run it, you will get these errors cause there is no correct classpath to auth file.
So, if you run your app from command line with proper arguments, it should be working.