Connecting Java with mySQL Database

Hello everyone,

This is me saif, busy in SuperComp’17 management these days and bringing the FYP to the finish line.

SuperComp’17 is an IT centered cluster of events and competitions organized by Bahria University Karachi, going to be held at 3rd of May.

Coming back to the tutorial, this article is for beginners who want to develop a GUI based Java application that requires a Database to store application’s data.

This article will guide step by step how to create a database in mySQL using XAMPP and connect it with Java Application. Let’s Begin.

If you have no prior knowledge of Java and NetBeans read this article once to have a flavor.

First of all you have to download and install XAMPP from here. XAMPP is an open source, cross platform web server. Also download and extract jConnector; an open source driver package for mySQL.

Getting Started with XAMPP

  1. Open Xampp and start Apache and MySQL.1
  2. Click on admin and you will be taken to the control panel.2
  3. Click New and create a new database with any name of your choice. In this example it is “testdb”.3
  4. Now click on create table to create a new table for your database by providing table name and number of columns.4
  5. Set the data types and names of the columns you want to include.5
  6. Click on insert tab and insert a few values in your database.7
  7. You are now done with creating MySQL database.

Connecting Java with MySQL

    1. Create a GUI based Java Application as mentioned in the figure below.11
    2. Used a panel, a textBox, a password field, a button and two labels.
    3. Name the id text-field as ‘id’ and password-field as ‘pass’
    4. Now, add  jConnector by right clicking on libraries and then add jar file.17
    5. Browse to the folder where you extracted jConnector to set the path.18
    6. Now create a connection (CRUD) class using the code below.12
    7. import java.sql.Connection;
      import java.sql.DriverManager;
      import javax.swing.JOptionPane;
      public class CRUD {
      final String DRIVER = "com.mysql.jdbc.Driver";
       final String URL = "jdbc:mysql://localhost/testdb";
       final String USER = "root";
       final String PASS = "";
      private Connection connectDB = null;
      public CRUD() {
      try {
      this.connectDB = (Connection)DriverManager.getConnection(URL, USER, PASS);
      } catch (Exception se) {
      public Connection conDB() {
      return connectDB;
    8. Double click the submit button and paste the code given below.
    9. CRUD login = new CRUD();
              try {
                  Statement get = login.conDB().createStatement();
                  String sql = "SELECT * FROM student where ID = '"+ id.getText()+ "' and password = '"+pass.getText()+"'";
                  ResultSet res = get.executeQuery(sql);
                  if ({
                       JOptionPane.showMessageDialog(this, "Access Guaranteed! Welcome " +res.getString("Name"));
                       JOptionPane.showMessageDialog(this, "Invalid ID or Password");
              } catch (Exception se) {
                  JOptionPane.showMessageDialog(this, se.getMessage());
    10. The given code will provide the functionality of a login panel. It will check whether a student with the provided ID and Password exists in the database?
    11. It will display the messages in either of the cases.1516

Congratulations 🙂
We are done with connecting Java with mySQL database.

Stay with Protorials for more tutorials.
Have a nice day!


1 thought on “Connecting Java with mySQL Database”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s