JDBC with MySQL

JDBC stands for Java Database Connectivity is quite a way that helps Java Users to interact with many versatile Database. I am going to show you the connectivity for JDBC with MySQL because both are Open Source in terms of using it.

Software Requried:

  • Java JDK.
  • Xampp or any other application that let’s you interact with phpmyadmin to create a database.
  • MySQL Connector for JDBC Connectivity.

Steps for JDBC setup

  • Download and Install Xampp or Laragon.
  • Download MySQL Connector and follow below steps.
  • Extract the file.
  • Paste the mysqlconnector.jar [Your Drive]\Java\jdk[version]\jre\lib\ext
  • Right Click on mysqlconnector.jar and GOTO properties.
  • In Properties Look for Unblock Button, If not found than go for Coding.

Note: [Your Data] Square Brackets are your Environment Setup.

Source Code:

imort java.sql.*;
class JDBCEX {
    Connection con;
    Statement st;
    ResultSet rs;
    public JDBCEX() { //Constructor
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/[YourDatabase]"); 
            //There should be a database named as test in your phpmyadmin.
            System.out.println("Connected Successfully");
            st = con.createStatement();
            rs = st.executeQuery("Select * from [YourTable]"); 
            //There should be a table named as test1 in your database test1.
            while (rs.next()) {
              //Keep Some records in table to run this code.
              System.out.println("RNO : " + rs.getInt(1) + "\nNAME : " + rs.getString(2) + "\nAddress : " + rs.getString(3) + "\nEmail : " + rs.getString(4) + "\n");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    public static void main(String args[]) {
        JDBCEX test = new JDBCEX();
    }
}
JDBC MySQL Integration