MYSQL – JAVA NET BEANS CONNECTIVITY
Concept of Front End and Back End:
Back End refers to the program where the requests are processed and where the
information are stored in a systematic manner. E.g. My-SQL, Oracle, MS-Access etc.
Front End refers to the program through which the requests are made. Front End refers
to the GUI which helps in interaction with the Back-End in a user-friendly manner. E.g.
Java, C, C++, VB etc.
Database connectivity deals with connecting Back End and Front End using a
connectivity driver such that data transaction can be performed with the Back End
using Front End. In order to connect to a database from within an application, you need
to framework that helps you send and execute SQL statements within the application
code.
Concept of JDBC Driver:
JDBC stands for Java Data Base Connectivity. JDBC is a driver software which performs
the following tasks:
1. Establish a connection with a database.
2. Send SQL statements to the database server.
3. Process the results obtained.
Steps for Designing a Project using MySQL- Java Net Beans Connectivity:
Step-1 (Back End Design):
Open MySQL Command Line Client and create a Database “Staff” as given
below:
Open the Database using the following command:
Create a Table Teacher with the following specifications:
Column Name Data Type Size Constraint
Teacher_ID Varchar 10 Primary Key
Name Varchar 40
Designation Varchar 40
Subject Varchar 40
Experience Integer
Insert 3 records in the above table as follows:
Step-2 (Register MySQL Server with Net Beans):
Open Net Beans IDE. On the left side Panel click on the Services Tab.
The first Option available is “Databases”.
Right click on Databases and click on “Register MySQL Server”
In the MySQL Server Properties Dialog Box that opens up, type in the
Administrator User Name (if not displayed). Also type in the Administrator
Password for your MySQL Server. Check the Remember Password checkbox and
click on OK as shown below:
Note: If MySQL is already registered then the above steps are not required.
Click on + sign next to Databases. The MySQL Server should now appear under
the Database node in the Services tab in the NetBeans IDE. However, it is shown
disconnected as shown below:
To connect the MySQL Server to NetBeans, under the Databases node, right click
the MySQL Server at localhost:3306 [root] (disconnected) and select Connect as
shown below:
When the server is connected you should see the [disconnected] removed from the
MySQL Server at localhost:3306 [root] database. You should also be able to expand
the MySQL Server node by Clicking on the + sign to view all the available MySQL
databases as shown below:
Step-3 (Designing the GUI using Net Beans IDE):
Open Net Beans IDE. Create a Project called “Faculty” and design the following
interface:
Step-4 (Registering the JDBC Driver):
Before writing the Java Program to connect to the database, we also need to add the
MySQL connector JAR file to the Libraries in our project. The following steps show you
how to do just that:
a) Open the desired Project(Faculty) in Net Beans
b) From the Project Window click on the “+” sign against your project name.
c) Right click on Libraries. Click on Add JAR/Folder as shown below:
d) Browse the hard drive (or download from dev.mysql.com/download) and select
“mysql-connector-java-5.1.12-bin”
e) Click on Open.
Step-5 (Adding Functionalities to the Buttons):
SHOW RECORDS:
Double click on “SHOW RECORDS” and type the following Code:
new NewJFrame1().setVisible(true);
Design the following Frame called “NewJFrame1” for SHOW RECORDS button.
Add a Table from Swing Control Option. Change the label from the
Model Property.
At the top of the code window type:
import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
Double click on “SHOW” and type the following Code:
Output:
SEARCH:
Double click on “SEARCH” and type the following Code:
new NewJFrame2().setVisible(true);
Design the following Frame called “NewJFrame2” for SEARCH button.
At the top of the code window type:
import java.sql.*;
import javax.swing.JOptionPane;
Double click on “FIND” and type the following Code:
Output:
INSERT:
Double click on “INSERT” and type the following Code:
new NewJFrame3().setVisible(true);
Design the following Frame called “NewJFrame3” for INSERT RECORDS button.
At the top of the code window type:
import java.sql.*;
import javax.swing.JOptionPane;
Double click on “INSERT RECORDS” and type the following Code:
Output:
__________________________