0% found this document useful (0 votes)
3 views

DATABASE APPLICATION WITH GUI16

This document provides a step-by-step guide for creating a Java Swing application that connects to a MySQL database using JDBC. It outlines the necessary components, including setting up the JDBC driver, creating a new Java application in NetBeans, designing the GUI, and establishing database connectivity. Specific commands for creating a database and table in MySQL are also included, along with instructions for importing required libraries in the Java project.

Uploaded by

buushman151
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DATABASE APPLICATION WITH GUI16

This document provides a step-by-step guide for creating a Java Swing application that connects to a MySQL database using JDBC. It outlines the necessary components, including setting up the JDBC driver, creating a new Java application in NetBeans, designing the GUI, and establishing database connectivity. Specific commands for creating a database and table in MySQL are also included, along with instructions for importing required libraries in the Java project.

Uploaded by

buushman151
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA SWING APPLICATION WITH DATABASE CONNECTION

1. JDBC API: Java Database Connectivity Application Program Interface is a set of


interfaces and classes using which you can write Java programs for accessing and
manipulating databases. It acts as a communication between the application and the
database.
2. JDBC Driver: It enables a Java application to interact with the database. We need to set
up different JDBC drivers for different databases.

Workflow of a Java application and database interaction through JDBC drivers

Steps to create the application:


1. First, open Netbeans and click on the File option from the menu bar.

2. Now create a new Java application by clicking on New Project -> Java -> Java Application and
give a suitable project name and click finish.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 1


3. Now create a new file by going to the File option again on the menu bar, then New File ->
Swing GUI Forms -> JFrame Form, and give a suitable file name click finish.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 2


4. After successful file creation, we will now be presented with the following screen. The 3 important
parts of this window are:
• Design: This is the area where we will create the design/template of our application.
• Source: This is where the logic code of the program is written.
• Palette: This component contains all the widgets which we need to drag and drop on
the design area

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 3


Window displayed after successful Java file creation.

5. Now from the palette situated at the right-hand side of the window, start dragging the toolkit
widgets.

Drag Components from palette to design area

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 4


6. Since we need to display all data in a tabulated form, drag the table widget from the palette onto
the design area. Now to set the headings, right-click on the table, select Properties -> Model -
>Add/delete Columns.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 5


7. Now let us create the database to store the data. Open MySQL command client, enter password,
and type in the following commands to create a new database, new table, and defining the attributes.
mysql> create database student;
Query OK, 1 row affected (0.14 sec)

mysql> use student;


Database changed
mysql> create table record(
-> rollno int(3),
-> name char(20),
-> class int(2),
-> section char(2),

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 6


-> address varchar(40));
Query OK, 0 rows affected (2.03 sec)

mysql> describe record;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| rollno | int(3) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| class | int(2) | YES | | NULL | |
| section | char(2) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.03 sec)
Going back to NetBeans, we need to follow these steps for database connectivity:
8. We need to import libraries that are needed to set up a connection with the database and retrieve
data which is done by – DriverManager Class, Connection Class, and Statement Class. Thus go to the
menubar, under Tools->Libraries, and add MySQL JDBC connector. Note down the Library
ClassPath and click OK. Now go to Projects toolbar and go to your application’s Libraries. Right-
click and select Add Jar/Library and browse the Library classpath noted down previously.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 7


Add Jar file

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 8


Add mysql connector

9. Go to Windows->Services->Databases and enter the required credentials of your MySQL


username and password. After clicking test connection, if it’s successful, the connector logo appears
connected.

Connection is initially broken

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 9


GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 10
GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 11

You might also like