I am trying to find out why I cannot connect to mariadb on my laptop. MariaDB installs with several databases, and I can use HeidiSQL without any problems.
I try to connect a Java application to the database, but I get:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
I downloaded "mariadb-java-client-1.2.2.jar" and added this to the project.
My URI of my database:
jdbc:mysql://localhost:3306/mysql
I tried changing the usage:
jdbc:mariadb://localhost:3306/mysql
With the same results. It works for me earlier on another PC, but I do not know why it does not work on a laptop? The username and password are correct and the same as those used to connect to HeidiSQL.
I tried with both:
Class.forName("com.mysql.jdbc.Driver");
and
Class.forName("org.mariadb.jdbc.Driver");
to register the library, and then I read that it is not required .... what am I missing?
code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class clsDB {
private static final String TAG = clsDB.class.toString();
private static final String SERVER_ADDR = "localhost";
private static final String DB_URL = "jdbc:mariadb://" + SERVER_ADDR + ":3306/mysql";
private static final String USER_NAME = "root";
private static final String PASSWORD = "RRCmcs2014";
private Connection m_con = null;
public clsDB() throws Exception {
connect();
}
private void errorMsg(String strMethod, String strMsg) {
System.out.println(TAG + "." + strMethod + ": " + strMsg);
}
protected void finalize() throws Throwable {
close();
}
public void close() throws SQLException {
if ( m_con != null && m_con.isClosed() == false ) {
m_con.close();
}
}
public void commit() throws SQLException {
if ( m_con != null && m_con.isClosed() == false ) {
m_con.commit();
}
}
private void connect() throws Exception {
m_con = (Connection)DriverManager.getConnection(DB_URL,
USER_NAME,
PASSWORD);
if ( m_con == null ) {
throw new Exception( "Cannot connect to database!" );
}
m_con.setAutoCommit(false);
}
public long execute(String strSQL) throws SQLException {
Statement st = null;
long lngRC = 0;
try{
if ( m_con != null ) {
if ( m_con.isClosed() == true ) {
try{
connect();
} catch( Exception ex ) {
errorMsg("query", ex.getMessage());
}
}
st = (Statement)m_con.createStatement();
if ( (lngRC = (int)st.executeUpdate(strSQL, Statement.RETURN_GENERATED_KEYS)) > 0 ) {
if ( strSQL.toUpperCase().startsWith("INSERT") == true ) {
ResultSet keys = st.getGeneratedKeys();
if ( keys != null ) {
keys.next();
lngRC = keys.getLong(1);
}
}
m_con.commit();
}
}
} catch( SQLException ex ) {
errorMsg("execute", ex.getMessage());
} finally {
if ( st != null ) {
st.close();
}
}
return lngRC;
}
public Connection getConnection() {
return m_con;
}
public ResultSet query(String strSQL) throws SQLException {
Statement st = null;
ResultSet rs = null;
try{
if ( m_con != null ) {
if ( m_con.isClosed() == true ) {
try{
connect();
} catch( Exception ex ) {
errorMsg("query", ex.getMessage());
}
}
st = (Statement)m_con.createStatement();
rs = st.executeQuery(strSQL);
}
} catch( SQLException ex ) {
errorMsg("query", ex.getMessage());
}
return rs;
}
}