Connecting MariaDB using JDBC

Assumption :
     You have MariaDB and Client installed.

Steps:
     Run client and create database using command "create database".

     create database test;

     Now, connect to that database using "USE" command

     Use test;

     Create new table USER with following command

     CREATE TABLE USER (
       ID int NOT NULL,
       NAME varchar(100) NOT NULL,
       EMAIL_ADDRESS varchar(100),
       PRIMARY KEY (ID)
     )

     Now, download the driver from mysql site. I'm using "mysql-connector-java-5.1.46". MySql and MariaDB are similar so for basic connectivity mysql drivers works.

  Class.forName("com.mysql.jdbc.Driver");

  java.sql.Connection con =   DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");



  PreparedStatement prepareStatement= null;

  try {

   prepareStatement = connection.prepareStatement("insert into USER values(?, ?, ?)");

   prepareStatement.setInt(1, user.getId());

   prepareStatement.setString(2, user.getName());

   prepareStatement.setString(3, user.getEmailAddress());

   rows = prepareStatement.executeUpdate();

  } catch (SQLException e) {

   Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, e);

  }

Comments

Popular posts from this blog

How to do AJAX using XMLHttpRequest for large data

Caching mechanisms in modern world browser

Polymorphism in Java OOP