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

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.

Related

Can't connect to MySQL database: No suitable driver found

I'm having some troubles with java and jdbc.
In particular, while my code perfectly works in a NetBeans project, when i try to execute it on a terminal or on my ubuntu vps (which is where i need it to work) i always get this exception:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/quakes
First thing first: yes, i'm adding the jdbc .jar to the execute command and the compile command; Yes, i've even tried to add
Class.forName("com.mysql.jdbc.Driver");,
but I always get a ClassNotFoundException: com.mysql.jdbc.Driver exception
The .jar i'm using is the exact same that i use in the NetBeans project, so i know i have the right thing, and even downloading it again from the official site won't change a thing.
Yes, the database exists, and the result doesn't change if i try to connect to another db.
I also tried switching to postgresql (yes, i didn't forget to change the url), but to no avail, it still can't find the driver.
With this, i'm guessing that the actual error is in the compile/execute commands, but even them should be ok:
javac *.java <-cp mysql-connector-java-5.1.41-bin.jar > (the <> parenthesis means that i tried compiling with and without specifying the classpath);
java TAW -cp mysql-connector-java-5.1.41-bin.jar,
In case you want to see it, here's the method that tries to connect to the database:
public Connection getConnection() throws SQLException {
if (conn == null) {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+
this.dbname,this.user,this.pass);
}
return conn;
}
Does anyone have any idea on why this happens?
You need to put the classpath option for java before the name of your main class, otherwise it is regarded as program arguments:
java -cp mysql-connector-java-5.1.41-bin.jar;. TAW
You can use this method step by step to create connetion.
it is an example connectivity:
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");

No suitable driver found for jdbc:mariadb (Netbeans)

I'm using the mariadb-java-client-1.5.7.jar connector for MariaDB, and it does not work.
Here's the connection code:
public DataAccess() throws SQLException, ClassNotFoundException {
this.driver = "org.mariadb.jdbc.Driver";
this.host = "jdbc:mariadb://localhost/bluebank";
this.user = "root";
this.password = "";
Class.forName(this.driver);
this.conn = DriverManager.getConnection(this.host, this.user, this.password);
}
I get:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost/bluebank
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DAO.DataAccess.<init>(DataAccess.java:31)
Apart from adding as an external jar to the libraries, I've added it as a driver to the databases in (Services) in Netbeans. Also, if I remove the Class.forName(), it doesn't work as well.
Had something similar today. Worked in Eclipse, did not with pure Java.
For me it was important to have
Class.forName ("org.mariadb.jdbc.Driver");
to make it work everywhere.
You forgot the port number of your database :
this.host = "jdbc:mariadb://localhost:port_number/bluebank";
Make sure that your db connector jar, exist in the your jar libraries: https://mariadb.com/kb/en/mariadb/about-mariadb-connector-j/
You can learn more here :
Connect to MariaDB from Java application in NetBeans on Linux (Mageia)
Hope this can help you
replace mariadb in the url with mysql:
I had this Problem myself: the solution was quite simple... MariaDB is basically still MySQL. In the Url you are using to connect to the Database (jdbc:mariadb://localhost:3306) you can therefore just use jdbc:mysql://localhost:3306 <- i just replaced mariadb with mysql. It is still running on a mariadb server but it works so dont change it ;)
Still i dont know why none of the other solutions have worked but at least it is a solution

Unable to connect to SQL Server via JDBC. No suitable driver found for jdbc:sqlserver://

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

MySQL connection JDBC driver not found

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"

MySQL Non Registering Driver

This is my first post so if I get anything wrong I apologise!
I am using Java in Eclipse to create tables in a MySQL Database. Everything has been working fine until it inexplicably stopped working the other day. My code runs but nothing happens to the database due to an error when I try to initialise the connection. The code actually runs to the end, the error is only viewable when I step through the code.
This is the constructor of my TableCreator class:
public TableCreator() throws SQLException {
host = "jdbc:mysql://localhost:3306/test";
dbName = "root";
dbPass = "pass";
conn = DriverManager.getConnection(host, dbName, dbPass);
query = conn.createStatement();
initialise();
System.out.println("Success.");
}
The error occurs on the line 'conn = DriverManager.getConnection(host, dbName, dbPass);'.
The stack looks like this just after I have attempted to execute this line:
TableCreator (1) [Java Application]
biz.cogitare.gpsperformancetool.TableCreator at localhost:53389
Thread [main] (Suspended)
Driver(NonRegisteringDriver).connect(String, Properties) line: 306
DriverManager.getConnection(String, Properties, Class<?>) line: not available
DriverManager.getConnection(String, String, String) line: not available
TableCreator.<init>() line: 23
TableCreator.main(String[]) line: 49
Daemon Thread [Abandoned connection cleanup thread] (Running)
C:\Program Files\Java\jre1.8.0_45\bin\javaw.exe (9 Jul 2015 12:27:27)
I have spent many hours searching the internet for help on how to fix this but so far I have been unsuccessful.
Any help would be greatly appreciated.
Thanks!
Several things can cause such behaviour.
Check the driver is loaded.
If your JDBC driver is earlier than 4.0 you need to ensure the driver is loaded with
Class.forName("com.mysql.jdbc.Driver")
Or a similar string depending on the particular driver you are using.
Maybe you were doing another operation which loaded the driver before executing this code and that is why it did work. And maybe you are not doing that operation and that is why this code fails despite being the same code.
Check the driver file is in the classpath.
If you get ClassNotFoundException it means the JVM has not been able to locate your class. If you get LinkageError it means there is a version conflict. Get the most recent driver and reinstall it.
Check the driver is properly imported in eclipse.
Follow this instructions to import it : How to import a jar in Eclipse
If everything fails, reinstall.
Uninstall java, remove eclipse, re-install everything.
Insert the driver in the correct fashion and started a new project and copy your classes over.
If you are exporting the project as runnable jar,please make sure that you export mysql driver.jar
and as all above answers if you need to add
Class.forName("com.mysql.jdbc.Driver"); before conn = DriverManager.getConnection(host, dbName, dbPass);
If it not working, please share with us the stacktrace

Categories