Masked Man Masked Man - 1 month ago 6
Java Question

I can't establish connection to Mariadb using Java?

I try running this code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class fun {
public static void main(String args[]) {
try {
Connection connection = DriverManager.getConnection(
"jdbc:mariadb://localhost:3306/", "root", "bingo");

}
catch(SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class fun {
public static void main(String args[]) {
try {
Connection connection = DriverManager.getConnection(
"jdbc:mariadb://localhost:3306/", "root", "bingo");

}
catch(SQLException e) {
e.printStackTrace();
}
}
}


But I get the following error:

java.sql.SQLException: No suitable driver found for jdbc:mariadb://ocalhost:3306/
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at fun.main(fun.java:8)


I downloaded mariadb-java-client-1.4.6-sources.jar from mariadb website and put it into my
jdkdirectory/jre/lib/ext/
directory
and this is how my
CLASSPATH
variable holds:

C:\Program Files\Java\jdk1.8.0_91\bin;
C:\Program Files\Java\jdk1.8.0_91\jre\lib;
C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext;.;
C:\ProgramFiles\Java\jdk1.8.0_91\jre\lib\ext\mariadb-java-client-1.4.6-sources.jar

Answer

Jens hit it on the head in the comments above: You don't want the sources JAR if it contains .java files. You want byte code; I'd see if there's a JAR with the driver .class file in it available.

You should never put anything in your jre/lib/ext folder. Better to learn how to deal properly with CLASSPATH when you compile and run.

Most Java developers will set up a project in an IDE like IntelliJ using some standard directory format (e.g. Maven). Using Maven certainly makes it easier, but if you're not I'd expect to see a /lib directory with all 3rd party JARs like your JDBC driver in it. All those JARs would be added to CLASSPATH at compile and run time.

A professional developer will store their project in a source code management system like Subversion, Mercurial, or Git. Anyone checking your code out should be able to see what all the dependencies are.

"No suitable driver" might also mean that the JDBC driver class was loaded, but the URL syntax was incorrect.

Comments