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:
MariaDB
's version is 10.1.13-MariaDB