MySQL connection JDBC driver not found - java

I am trying to open a MySQL connection but run into this error.
java.lang.ClassNotFoundException: com.mysql.jdbc.driver
There are plenty of questions online about this but I cannot seem to make it work.
I currently using gradle to compile 'com.oracle:ojdbc14:10.2.0.4.0' and I have added the connector library using the build in maven importer.
My code looks like this:
private static final String DRIVER = "com.mysql.jdbc.driver";
...
try {
try {
Class.forName(DRIVER).newInstance();
} catch (Exception e)
{
e.printStackTrace();
}
...
}
I tried to add ojdbc-14.jar to libs and reference that in gradle without success. I also use some groovy code in the build.settings but without success, I'm not sure how to work with that either.
A pic of my IDE:

The error is in your DRIVER string. It should be
DRIVER = "com.mysql.jdbc.Driver"

Related

No Suitable Driver error after setting CLASSPATH, Maven, and pom.xml

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.

intellij com.mysql.jdbc.Driver issues, tried multiple methods

I've been searching for more than a day now on this simple problem. This is my first time using Intellij/Android Studio as I come from using Eclipse. The issue I'm having is that the project can't find my driver. I've done this multiple times, usually you would just have to make sure that the mysql-connector.jar is referenced/added to the project. However, Intellij is not so simple in this regard. First, I am using the free version and not the ultimate, so I do not have the database tools option but luckily, there is a database navigator plugin. I add it and set my connections. I use the built-in driver as shown
and my code is the following:
private static Connection connection = null;
private static String driver = "com.mysql.jdbc.Driver";
private static String url, user, password = "";
private static String server = "localhost";
public LoginDataSource() {
System.out.println("Connecting to database...");
setDatabase(server);
try {
Class.forName(driver); // <-- fails here
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
Error due to
W/System.err: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
W/System.err: at java.lang.Class.classForName(Native Method)
I keep looking and some say to add the connector jar as a maven dependency. I do that as shown:
and I add it here too
I run the application again and still same error. So next I go back to database navigator and edit the driver to external and select the regular driver as shown:
I run the application again and still the same error... I am at a lost. I don't know what to do, and it would be really great if someone could tell me.
This is my external libraries if it helps, the mysql connector is not shown here
I tried multiple ways and I know there are VERY similar threads on this but believe me, I have tried and I still can't figure it out on my own. Any help would be greatly appreciated. Thank you.
I will try to help. First, remove your previous MySQL driver dependencies, to start from scratch. Then, you need to declare a dependency in your "build.gradle" file:
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.20'
After you add this line to the script, refresh the gradle dependencies.
Then, try to load the driver again, but note that com.mysql.jdbc.Driver is deprecated now, use com.mysql.cj.jdbc.Driver.
I will answer your questions if this small instruction fails.

Trying to connect to a SQLite database, keep getting no Suitable Driver Found

When I call The staement
c = DriverManager.getConnection("jdbc:sqlite:sample.db");
I get a exception saying
no Suitable Driver Found for jdbc:sqlite:sample.db
The project is running in eclipse on a windows platform.
This is what I did:
WENT TO properties-> java build path -> Add external jar
added the file sqllite-3-16-1.jar
The code
try {
// exception goes off here
c = DriverManager.getConnection("jdbc:sqlite:sample.db");
} catch(Exception e){
ted=ted+1;
} //
You need to load the driver class before trying to acquire the connection. Can you try the following:
Class.forName("org.sqlite.JDBC");
This will throw NoClassDefFoundError if the driver jar is not on the classpath.

Class Not Found Exception, even after putting the jar file and setting the classpath in Mac OS X

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)

java.sql.SQLException: No suitable driver found for jdbc:sqlserver tried classpath already

The sqljdbc4.jar isn't loaded or recognized even after following the steps from:
java.sql.SQLException: No suitable driver found for jdbc:derby:
With this command the application does not start outside netbeans. Starting it only with: java -jar FXProductWatcher.jar # does not load the driver .jar
PS C:\Documents and Settings\User1\Meus
documentos\x\workspace\FXProductWatcher\dist> java -classpath
'C:\Documents and Settings\User1\Meus
documentos\x\workspace\FXProductWatcher\dist\lib\sqljdbc4.jar;FXProductWatcher.jar'
FXProductWatcher
Results in> Error not able to locate or load main class FXProductWatcher
I'm using netbeans the manifest file is being generated automatically. I tried placing Class-Path: lib/sqljdbc4.jar but with no positive result.
The manifest from netbeans has:
JavaFX-Application-Class: fxproductwatcher.FXProductWatcher
JavaFX-Class-Path: lib/sqljdbc4.jar
Created-By: JavaFX Packager
Main-Class: com/javafx/main/Main
My connection code
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
String sDbUrl = "jdbc:sqlserver://remoteIP;databaseName=test";
conn = DriverManager.getConnection(sDbUrl, username, password);
return conn;
Thanks for any help or sugestion.
Brother the url which you are using is :: jdbc:sqlserver://remoteIP;databaseName=test
the ":"after sqlserver is not valid.
try this: jdbc:sqlserver//remoteIP;databaseName=test
this may work.
good luck.

Categories