Since yesterday I have been trying to figure out how to fix this SQLException, and at this point I'm empty. I have checked dozens of sites, the whole documentation for Microsoft and Azure on how to connect with databases, and I keep getting the same error.
I've added a CLASSPATH like CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 8.2 for SQL Server\sqljdbc_8.2\enu\mssql-jdbc-8.2.2.jre11.jar as informed on Using the JDBC Driver documentation. I've downloaded the JDBC driver from Microsoft themselves and dumped the files on a folder in C:\Program Files\Microsoft JDBC DRIVER 8.4 for SQL Server as their documentation suggests. My maven and java are working fine, and I have an example class that I've got from SQLExamples by Microsoft themselves, this one:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// As informed on:
// https://www.microsoft.com/en-us/sql-server/developer-get-started/java/windows/step/2.html
public class App {
public static void main(String[] args) {
String connectionUrl = "jdbc:sqlserver:[AzureAddressHere];"
+ "database=[DatabaseHere];"
+ "user=[UserHere];"
+ "password=[PasswordHere]";
try {
// Load SQL Server JDBC driver and establish connection.
System.out.print("Connecting to SQL Server ... ");
try (Connection connection = DriverManager.getConnection(connectionUrl)) {
System.out.println("Done.");
}
} catch (SQLException e) {
System.out.println();
e.printStackTrace();
}
}
}
I've also added the references to the jar file to the pom.xml dependencies list:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre11</version>
</dependency>
I've also found a mention in a website that the jar file sits in Maven's library folder, so I've copied the jar file to the lib folder as well.
Yet, whenever I try to run the app, I get the java.sql.SQLException: No suitable driver found for [AzureInformation].
Any ideas on what I could be missing here?
Turns out that the issue was the lack of two // in the address. facepalm
So, I just set this line to: String connectionUrl = "jdbc:sqlserver://[AzureAddressHere];" and it finally connected.
To be fair to myself here, this error is so completely unclear and unhelpful.
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 simply want to use SQL Server database in my HTTP Servlet program but my program can't seem to connect to the database. It gives me the following error:
No suitable driver found for
jdbc:sqlserver://localhost:1433;databaseName=Bookyard;integratedSecurity=true;
This is my connection method.
package practice.bookyard.server.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
public static Connection getConnection() {
String url = "jdbc:sqlserver://(LocalDb)\\MSSQLLocalDB:1433;databaseName=Bookyard;integratedSecurity=true;";
Connection connection = null;
try {
connection = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
I had the sever name as localhost:1433 earlier but I changed it to the SQL Server instance name (LocalDb)\\MSSQLLocalDB:1433 but it still seems to pick up the old name.
Also, I am not sure how to provide the right connection string when connecting to SQL Server localdb.
I am using Eclipse for Java EE, Mars 2, and I downloaded Microsoft JDBC drivers for SQL 6.0 from this website.
I ran the installation, unzipped the contents of the resulting folder. Then, I added the sqljdbc42.jar file to the build path as I am targeting JDK 1.8.
UPDATE
Upon Scary Wombat's suggestion, I have also added the path to the sqljdbc42.jar file to my classpath.
However, I still get the same error.
I am pretty confident this is a reflection issue, in that the type loader isn't able to resolve the driver type from my connection string. Which means, the connection string syntax I am using is wrong.
I changed my connection string to read as follows:
String url = "jdbc:sqlserver://localhost:1433;
instance=(LocalDb)\\MSSQLLocalDB;
databaseName=Bookyard;integratedSecurity=true;";
However, I still not only get the same error but the exception message I receive still has my old connection string. So, clearly, there's also some caching going on, I just don't know where. Who is caching my connection string and how do I refresh / clear that cache?
Could you please tell me how to provide a SQL Server instance name if I am connecting to localdb and not on the main SQL Server instance?
As #ScaryWombat and #JozefChocholacek had hinted, it turned out to be a class path issue. Apparently, you have to copy and paste just the sqljdbc42.jar file, and this file only directly into the WEB-INF\lib folder and not within any sub-folder.
I did that it still gave me that error.
That was because the WEB-INF folder structure, when I viewed it in the Project Explorer, still had the old folder structure. So, I right-clicked on the WEB-INF folder in the Project Explorer and selected the Refresh command.
I also updated my environmental variable CLASSPATH to point it to the WEB-INF folder and that error went away.
you can do like below
String url = "jdbc:sqlserver://YOUR PC NAME;instanceName=SQLEXPRESS;DatabaseName=dbname;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myCon = DriverManager.getConnection(url);
note :however its not necceaary for new version of jdbc driver to load driver class manually using class.forname
I am trying to make a java application which would use MySQL as database. I have successfully done this number of times using Windows but I am facing problems with Mac OS. Even after placing the "my sql connector" jar file in lib folder and setting proper classpath, I am getting an exception of
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Screenshot of code along with exception.
Please Help.
As you want to connect to MySQL, you need to use the right Driver class - see MySQL Documentation on MySQL Connector/J
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}
Please note, your JDBC url is also wrong, or do you try to connect via odbc? (MySQL JDBC URL for example: jdbc:mysql://localhost:3306/mydatabase , driver class: com.mysql.jdbc.Driver)
I've searched a lot and spent many time trying register JDBC driver.
First, I copied my ojdbc7.jar file (downloaded from Oracle) into directory shown below:
Driver File(s): /Users/Kamil/glassfish4/jdk7/jre/lib/ext/ojdbc7.jar
Driver Class: oracle.jdbc.OracleDriver
// this is copied from Services/Databases/Drivers/ojdbc
Then, I tried following code:
try {
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(myDriver);
} catch (ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
... and this one:
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
... and with this line instead:
Class.forName("oracle.jdbc.driver.OracleDriver");
I always get ClassNotFoundException :(
Here is code I try to run:
Connection DBconn;
String USER = "root";
String PASS = "root";
System.out.println("Connecting to database...");
DBconn = DriverManager.getConnection("mysql://localhost:3306/RestToolDatabase", USER, PASS);
System.out.println("Creating statement...");
Statement stmt = DBconn.createStatement();
String sql;
sql = "select surname, id, age\n"
+ "from customers \n"
+ "where name = \"maria\" \n"
+ "order by id;";
ResultSet rs = stmt.executeQuery(sql);
I've also read about setting the classpath as described here:
Right-click your Project.
Select Properties.
On the left-hand side click Libraries.
Under Compile tab - click Add Jar/Folder button.
but there is no "Properties/Libraries" option in NetBeans...
I use Maven and there is following dependency added by some library:
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>14</version>
<type>pom</type>
</dependency>
Maybe there is some workaround, or other way to add it automatically? It SHOULD be simple but I'm unexperienced and wasted many time on this. Please help.
EDIT: Thank you for replies, yes, I use MySQL Server at localhost:3306 [root]. I have MySQL JDBC connector installed here:
/Applications/NetBeans/NetBeans 8.0.app/Contents/Resources/NetBeans/ide/modules/ext/mysql-connector-java-5.1.23-bin.jar
When I go to "Services" --> "Drivers" --> "MySQL (Connector/J driver)" there is Driver Class path exactly as you suggested, so I use Class.forName("com.mysql.jdbc.Driver") now.
I right-clicked on "MySQL (Connector/J driver)" driver and went to "Connect Using..." --> "localhost, port 3306, user, password". And it is connected now, I see new connection. But still I get ClassNotFoundException.
EDIT 2 - this solution worked for me:
I added following to dependencies in pom.xml:
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
</dependency>
... and builded application; the driver was downloaded and installed. As simple as that... I spent many time on this... It works - yeah! :)
You are using an Oracle JDBC driver on a MySQL database, you should use
Class.forName("com.mysql.jdbc.Driver");
Edit: Thanks to the comments from #duffymo and #Mark-Rotteveel who noticed that the URL of the connection is also wrong, the correct connection is:
Connection DBconn;
String USER = "root";
String PASS = "root";
DBconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/RestToolDatabase?" + "user="+USER+"&password="+PASS);
Setting the classpath :
Download the JAR JDBC Connector/Driver
Copy file in subfolder of project (ex: libs) (not 'src')
Project->Properties->Libraries->Add Jar/Folder->Select file
And packaging dependent librairies with relative path in 'dist' :
Project->Build->Packaging->Check "Copy Dependent Librairies"
I don't think the JDBC JAR belongs in that directory. The CNF exception backs me up.
I would recommend putting that JAR in your project WEB-INF/lib, repackaging the WAR, and trying again.
This line is correct:
Class.forName("oracle.jdbc.driver.OracleDriver");
The full class name has to match that in the JAR. This is the only one that does.
You have to use the driver that matches your database: Oracle for Oracle, MySQL for MySQL. What database are you using?
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
This worked for me. Since there's no Property and Library in Netbeans apache. Good luck.
I was trying to connect to MySQL using JDBC API. I have downloaded the MySQL driver which is the "mysql-connector-java-5.1.28-bin jar" file. My OS is Windows 7 and I have set the Classpath of Java to following path:
"E:\Myclass"
I have copied the above jar file to this folder.
Then I have written the following code to test if I can load the driver.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
System.out.println("Unable to load Driver Class");
return;
}
}
}
I expect everything should be working fine but I always get the "Unable to load Driver Class". Can anyone point out where was wrong? Thanks
Note: Thanks for all your answers. I have solved the problem. Since I am using Eclipse, I have add the JAR file to the classpath of the Eclipse.
You have to include the JAR in your classpath:
java -jar yourdriver.jar LoadDriver
JARs are filesystems. They should be added to your classpath the same way you add directories. Only classes will be loaded from the classpath you specified.
Use the below cmd to run it
java -cp E:\Myclass\mysql-connector-java-5.1.28-bin.jar; LoadDriver
As mentioned the mysql jar exist # E:\Myclass\mysql-connector-java-5.1.28-bin.jar , just set in the classpath and run it