0

I have created a database named vidya with a table named students using PhPMyAdmin. I want to insert values to the database using a Java code. Here's the code but currently has problem with MariaDB JDBC connector plus I am not sure if my DB_URL is correct anyways if it is running on the localhost.

/**
 * Created by jalal on 7/6/2016.
 */
import java.sql.*;
import java.util.Calendar;

public class TestPhpMyAdmin
{

    public static void main(String[] args)
    {
        try
        {
            //static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
            // create a mysql database connection
            String myDriver = "org.mariadb.jdbc";

            String DB_URL = "jdbc:mysql://localhost/vidya";
            Class.forName(myDriver);

            //  Database credentials
            final String USER = "root";
            final String PASS = "newpass";

            Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // create a sql date object so we can use it in our INSERT statement
            Calendar calendar = Calendar.getInstance();
            java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());

            // the mysql insert statement
            String query = " insert into students (ID, last_name, first_name, birthday, hometown)"
                    + " values (?, ?, ?, ?, ?)";

            // create the mysql insert preparedstatement
            PreparedStatement preparedStatement = conn.prepareStatement(query);
            preparedStatement.setInt(1, 808027);
            preparedStatement.setString(2, "Davis");
            preparedStatement.setString(3, "Felicita");
            preparedStatement.setDate(4, startDate);
            preparedStatement.setString(5, "Venice");

            // execute the preparedstatement
            preparedStatement.execute();

            conn.close();
        }
        catch (Exception e)
        {
            System.err.println(e.getMessage());
        }
    }
}

I get this error:

org.mariadb.jdbc

Here's the directory structure: enter image description here

MariaDB's version is 10.1.13-MariaDB

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • You get that error because `org.mariadb.jdbc` is not the name of a class to load, but of a package, which you can't load with `Class.forName`. You should have used `org.mariadb.jdbc.Driver`. But as you found out, loading the driver yourself is not actually necessary anymore. – Mark Rotteveel Jul 07 '16 at 08:48
  • The `Class.forName(myDriver)` line has not been required since 2007. Remove it. – user207421 Jul 08 '16 at 04:02

3 Answers3

1

JDBC drivers register themself with DriverManager.registerDriver.

Using DriverManager.getConnection(...), DriverManager will asked each driver if url like 'jdbc:mariadb:..." is handled. If a driver answer is yes, Drivermanager will create a connection using this Driver.

(That will permit to have for example a H2 database for developpement environnement, and a MariaDB database for production just changing the url)

Driver for MariaDB is "org.mariadb.jdbc.Driver", not "org.mariadb.jdbc" that is the driver package, but in fact isn't usefull in this case. This portion of code that is causing the error can be suppressed :

    String myDriver = "org.mariadb.jdbc";
    Class.forName(myDriver);
0

According to ojacobson in Java IRC channel there's no need for Class.forName(myDriver); hence I commented it and the value was inserted onto the database table:

Here's the correct code for the reference:

/**
 * Created by jalal on 7/6/2016.
 */
import java.sql.*;
import java.util.Calendar;

public class TestPhpMyAdmin
{

    public static void main(String[] args)
    {
        try
        {
            // create a mysql database connection
            String DB_URL = "jdbc:mysql://localhost/vidya";

            //  Database credentials
            final String USER = "root";
            final String PASS = "newpass";

            Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // create a sql date object so we can use it in our INSERT statement
            Calendar calendar = Calendar.getInstance();
            java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());

            // the mysql insert statement
            String query = " insert into students (ID, last_name, first_name, birthday, hometown)"
                    + " values (?, ?, ?, ?, ?)";

            // create the mysql insert preparedstatement
            PreparedStatement preparedStatement = conn.prepareStatement(query);
            preparedStatement.setInt(1, 808027);
            preparedStatement.setString(2, "Davis");
            preparedStatement.setString(3, "Felicita");
            preparedStatement.setDate(4, startDate);
            preparedStatement.setString(5, "Venice");

            // execute the preparedstatement
            preparedStatement.execute();

            conn.close();
        }
        catch (Exception e)
        {
            System.err.println(e.getMessage());
        }
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • [a Comment](http://stackoverflow.com/questions/32106052/class-forname-are-there-other-ways-to-use-it#comment52106990_32106052) and [an answer](http://stackoverflow.com/a/8053125) , and somehow with your setup, it interfered. – Drew Jul 07 '16 at 01:26
0

For mariadb is: jdbc:mariadb://localhost/ and don't need classforname if your java version above 1.6 ClassForName is for java below 1.6.

Change this line:

import java.sql.*;

import java.sql.Date;

And java.sql.Date make change for Date only.

Don't use import java.sql.*;

Tiago NET
  • 153
  • 9