Java Database Connectivity with MySQL | eclipse IDE | phpmyadmin | Advanced Java Programming

To connect Java application with the MySQL database, we need to follow below following steps.

In this example we are using phpmyadmin as the database. So we need to know following informations for the mysql database:

1) Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.

2) Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/GSSSKHOKHAR where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number and GSSSKHOKHAR is the database name. 

3) Username: The default username for the mysql database is root.

4) Password: It is the password given by the user at the time of installing the mysql database. In this example, we are going to use root as the password.

Let's first create a table in the mysql database, Table name : Students




LoginSystem.java



To connect java application with the mysql database, mysqlconnector.jar file is required to be loaded.

download the jar file mysql-connector.jar


Two ways to load the jar file:
1) Paste the mysqlconnector.jar file in jre/lib/ext folder
2) Set classpath










Output : Login Screen


Output : Wrong Authentication - Login Screen


Output : Successfully - Login Screen

Delete records from the Access/SQL table-using Swing Components

Home  »  Java  »  Advance Java  »  Java Database Connectivity  »  Delete records-GUI


This example delete record from the Access table 'SchoolClasses' on the hard disk, using classes of java.sql and java.awt and more packages with Grapchical User Interface.

  •  A JDBC-ODBC bridge driver is loaded
  •  A Connection to the database is made, using getConnection method()
  •  Using the Connection object, SQL's 'Delete' statement is sent to the database
  •  A message is printed on the command line indicating the command was successful
  •  Minimal exception handling is done


To build these concepts and skill, a 'SchoolClasses' table will be used as a working model. This table will be created in an Access database i.e. the SchoolDemo.mdb database will hold a 'SchoolClasses' table. The structure of the table will be as described below:

Database NameSchoolDemo.mdb
Table NameSchoolClasses
Primary KeyAdmission_No


The Following block of code creates the UI described using Java Swing Components.




The Output of DeleteRecords table is as shown below:




Diagram 22.34 output of DeleteRecords.class


After running DeleteRecords.class file you will see above screen just enter Reg.No/AdmissionNo into Reg.No textbox and click on 'Search' Button this will show the next screen as shown in diagram 22.35, if reg.no is exists then it will show records otherwise it will give error message that. "Registration No. ...... is Not Exist!!!"

Diagram 22.35 entering Reg.No into Reg.No JTextBox



If Reg.No 603 is Exist then it will shown below diagram 22.28. You can use Keyboard short cut to search records like 'Alt + S'. Same for Delete Record 'Alt + D'.


Diagram 22.36 Reg.No is Exist


Diagram 22.37 Warring Message "Do You Really Want To Delete The Record ? 603"


Diagram 22.38 Record Deleted Successfully




JDBC - Working With User Interfaces - Using Swing Compoents

Inserts records into the Access/SQL table using Swing Components
View records from the Access table/SQL using Swing JTable
Updates records into the Access/SQL table using Swing Components
Deletes records from the Access/SQL table using Swing Components






JDBC - Programming:

View records from the Access/SQL table.
Inserts records into the Access/SQL table.
Updates records into the Access/SQL table.
Deletes records from the Access/SQL table.
Drops the Access/SQL table.





Updates records from the Access/SQL table using Swing Components

Home  »  Java  »  Advance Java  »  Java Database Connectivity  »  Updates records-GUI


This example updates records into the Access table 'SchoolClasses' on the hard disk, using classes of java.sql and java.awt and more packages with Grapchical User Interface.

  •  A JDBC-ODBC bridge driver is loaded
  •  A Connection to the database is made, using getConnection method()
  •  Using the Connection object, SQL's 'update' statement is sent to the database
  •  A message is printed on the command line indicating the command was successful
  •  Minimal exception handling is done


To build these concepts and skill, a 'SchoolClasses' table will be used as a working model. This table will be created in an Access database i.e. the SchoolDemo.mdb database will hold a 'SchoolClasses' table. The structure of the table will be as described below:

Database NameSchoolDemo.mdb
Table NameSchoolClasses
Primary KeyAdmission_No


The Following block of code creates the UI described using Java Swing Components.




The Output of UpdatesRecords table is as shown below:

Diagram 22.28 The Update Record Screen for UpdatesRecords.class.



Once the UI, (as shown in diagram 22.25), has been created it is necessary to add the functionality to the UI so that it works like any standard Window's based commerical application data entry form.

Now, first of all, you will 'Search' record entering Registration No in Reg.No JTextField like '601' and then click on the 'Search' Button. If record is exist in database then it will show all records in UI fields otherwise form will show Error Message that 'Registration No. 61 is Not Exist!!!' as shown below diagram 22.26.


Diagram 22.29 Message displayed when wrong Reg.No is entered.

Diagram 22.30 Screen Ready for udpate record

Diagram 22.31 Screen asking Do you want update this record?

Diagram 22.32 Display message Record Successfully Updated

Diagram 22.33 Display Screen after successfully updated record




JDBC - Working With User Interfaces - Using Swing Compoents

Inserts records into the Access/SQL table using Swing Components
View records from the Access table/SQL using Swing JTable
Updates records into the Access/SQL table using Swing Components
Deletes records from the Access/SQL table using Swing Components






JDBC - Programming:

View records from the Access/SQL table.
Inserts records into the Access/SQL table.
Updates records into the Access/SQL table.
Deletes records from the Access/SQL table.
Drops the Access/SQL table.





View records from the Access/SQL table using Swing JTable

Home  »  Java  »  Advance Java  »  Java Database Connectivity  »  View records-GUI


This example shows records from the Access table 'SchoolClasses' on the hard disk, using classes of java.sql and java.awt and more packages with Grapchical User Interface. This program simply use UI component of java swing like JLabel, JButton, JMenu and JTable, after view records from database in java table you can simple print the records using print() method of java.

  •  A JDBC-ODBC bridge driver is loaded
  •  A Connection to the database is made, using getConnection method()
  •  A message is printed on the command line indicating the command was successful
  •  Minimal exception handling is done


To build these concepts and skill, a 'SchoolClasses' table will be used as a working model. This table will be created in an Access database i.e. the SchoolDemo.mdb database will hold a 'SchoolClasses' table. The structure of the table will be as described below:

Database NameSchoolDemo.mdb
Table NameSchoolClasses
Primary KeyAdmission_No




The Output of View table is as shown below:





Diagram 22.25



After running ViewRecords.class file you will see above screen just simply click on 'View Records' Button this will show the next screen as shown in diagram 22.27 in JTable.



Diagram 22.26 of ViewRecords.class in JTable



You can use print command too in java using print() method as shown in below diagram 22.28.


Diagram 22.27 using printer in JTable



JDBC - Working With User Interfaces - Using Swing Compoents

Inserts records into the Access/SQL table using Swing Components
View records from the Access table/SQL using Swing JTable
Updates records into the Access/SQL table using Swing Components
Deletes records from the Access/SQL table using Swing Components






JDBC - Programming:

View records from the Access/SQL table.
Inserts records into the Access/SQL table.
Updates records into the Access/SQL table.
Deletes records from the Access/SQL table.
Drops the Access/SQL table.