PART – 1


  • Create a database and table in MySQL.
  • Create an insert form in Java Swing App.
  • What is JDBC?
  • How to Connect Java Swing App To MySQL Database?

In this blog, you are going to have complete source code and theory of “How To Connect Java Swing GUI Application To MySQL Database Using JDBC Driver And using Netbeans IDE”. Stay tuned for more lectures.

What is JDBC ?

  • Java database connectivity (JDBC) is the JavaSoft specification of a standard application programming interface (API) that allows Java programs to access database management systems.
  • The JDBC API consists of a set of interfaces and classes written in the Java programming language.

What is the use of JDBC ?

  • JDBC makes it possible to do establish a connection with a data source, send queries and update statements, and process the results.
  • Simply, JDBC makes it possible to do the following things within a Java application: Establish a connection with a data source.
  • Send queries and update statements to the data source.



After Insert Data Into Database

Source Code – Insert Button Click Event Code

 private void Insert_BtnActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if(name_txt.getText().trim().isEmpty() || gender_txt.getText().trim().isEmpty())
            JOptionPane.showMessageDialog(this, "All Fields are Required.");
            try {
                String db = "jdbc:mysql://localhost:3306/swing_db";
                String username = "root";
                String password = "";
                Connection con = DriverManager.getConnection(db, username, password);
                String query = "insert into user_tbl (name, gender) values(?,?)";
                PreparedStatement pst = con.prepareStatement(query);
                pst.setString(1, name_txt.getText());
                pst.setString(2, gender_txt.getText());
                int count = pst.executeUpdate();
                if(count > 0)
                    JOptionPane.showMessageDialog(this, "Data has been Inserted Successfully");
                     JOptionPane.showMessageDialog(this, "Data insertion Failed");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(InsertForm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(InsertForm.class.getName()).log(Level.SEVERE, null, ex);

Packages Import

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

Database Table Structure

Database Table Data

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *