Swing Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

P.O.

Box85 KARONGI- RWANDA

IPRC KARONGI Tel: +250 788871075

Email:info@iprckarongi.rp.ac.rw
Integrated Polytechnic Regional College
www.iprckarongi.rp.ac.rw

INFORMATION AND COMMUNICATION TECHNOLOGY DEPARTMENT


PROGRAM: INFORMATION TECHNOLOGY

Module Name: Object Oriented Programming Using Java


Module Code: ICT212
Module Leader: BIZIMANA Zephanie & IRAGENA Jean d’ Amour Wilson

ADMINISTRATIVE DEPARTMENT: ICT


Year 2, Semester II
Academic year 2019/2020
20, April, 2020

1
Contents
SAMPLE CODES TO INSERT, UPDATE, DELETE AND SEARCH USING SWING: .............................................................................................................3
SWING Components Used: .....................................................................................................................................................................................4
DESIGN: ..............................................................................................................................................................................................................5
HOW TO ADD MySQL Connector in your project? ...............................................................................................................................................6
CODES TO GENERATE JTABLE..............................................................................................................................................................................9
CODES TO DISPLAY DATA FROM DATABASE IN JTABLE ......................................................................................................................................12
CODES TO INSERT DATA IN THE DATABASE .......................................................................................................................................................13
CODES TO UPDATE DATA FROM THE DATABASE ...............................................................................................................................................16
CODES TO SEARCH DATA FROM THE DATABASE ...............................................................................................................................................18
CODES TO DELETE DATA FROM THE DATABASE ................................................................................................................................................19
CODES TO CLEAR DATA FROM THE FORM .........................................................................................................................................................20
CODES TO EXIT THE FORM ................................................................................................................................................................................20
FINAL OUTPUT AFTER INSERTING SOME DATA IN THE DATABASE .....................................................................................................................21

2
SAMPLE CODES TO INSERT, UPDATE, DELETE AND SEARCH USING SWING:

Database: Create database named “IprcDb”


Database’s table: create table named “results”
Structure of results table

3
SWING Components Used:

Name Variable Description


Name
JButton Clear Clear
JButton Delete Delete
JButton Exit Exit
JButton Insert Insert
JButton Search Search
JButton Update Update
JTextField a1
JTextField a2
JTextField a3
JTextField a4
JLabel jLabel1 REG NUMBER
JLabel jLabel2 FIRST NAME
JLabel jLabel3 LAST NAME
JLabel jLabel4 MARKS
JLabel jLabel5 MARKS MANAGEMENT SYSTEM
JPanel jPanel1
JLabel msg
JTable table

JPanel must be selected first and add other components on it.


To change variable name of any components, you press right click on any components and select Change Variable Name.

4
DESIGN:

5
HOW TO ADD MySQL Connector in your project?

Step 1: open NetBeans IDE

Step 2: create a new project

Step 3: View all projects in your NetBeans Interface and select the project you want to Add Connector open it and press right click on
Libraries

Step 4: select Add JAR/Folder… and Browser MySQL Connector

6
7
8
CODES TO GENERATE JTABLE
 After selecting table press right click and select properties
 After selecting properties select model

9
 Also select Events and select tableMouseClicked after selecting tableMouseClicked you get the
interface to write codes

10
private void tableMouseClicked(java.awt.event.MouseEvent evt) {

int row = table.getSelectedRow();


String tableclick = (String) (table.getModel().getValueAt(row, 0));
try {
Class.forName("com.mysql.jdbc.Driver");
Connection y = DriverManager.getConnection("jdbc:mysql://localhost:3306/IprcDb", "root", "");
String querys = "SELECT * FROM results WHERE Reg_Number='" + tableclick + "' ";
PreparedStatement ps = y.prepareStatement(querys);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String RegNumber = rs.getString("Reg_Number");
a1.setText(RegNumber);

String FirstName = rs.getString("First_Name");


a2.setText(FirstName);

String LastName = rs.getString("Last_Name");


a3.setText(LastName);

String Result = rs.getString("Result");


a4.setText(Result);
}
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e);
}
}

11
CODES TO DISPLAY DATA FROM DATABASE IN JTABLE
public DbConnection() {
initComponents();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/IprcDb", "root", "");
String query = "select * from results";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
DefaultTableModel model =(DefaultTableModel) table.getModel();
model.addRow(new Object[]{rs.getString(1), rs.getString(2), rs.getString(3),
rs.getFloat(4),rs.getString(5),rs.getString(6)});
}
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e);
}
}

12
CODES TO INSERT DATA IN THE DATABASE
private void InsertActionPerformed(java.awt.event.ActionEvent evt) {

String RegNumber=a1.getText();
String FirstName=a2.getText();
String LastName=a3.getText();
String Result=a4.getText();
float x=Float.parseFloat(Result);
String Grade="";
String Decision="";
if((x <=100) && (x >=80)){
Grade="A";
}
else if((x <80) && (x >=70))
{
Grade="B";
}
else if((x <70) && (x >=60))
{
Grade="C";
}
if(x <60 && x >=50)
{
Grade= "D";
}
else if(x <=50 )
{
Grade= "E";
}
13
if(x>= 50)
{

Decision="PASS";
}
else{
Decision="FAIL";
}
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/IprcDb","root","");
String query="insert into results values(?,?,?,?,?,?)";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1,RegNumber );
ps.setString(2, FirstName);
ps.setString(3, LastName);
ps.setFloat(4, x);
ps.setString(5, Grade);
ps.setString(6,Decision);
int pr=ps.executeUpdate();
if(pr>0)
{
JOptionPane.showMessageDialog(null, "1 record is saved to database");
}
else
{
JOptionPane.showMessageDialog(null, "1 record not saved to database");
14
}
}
catch( ClassNotFoundException | SQLException e)
{
System.out.println(e);
}
}

15
CODES TO UPDATE DATA FROM THE DATABASE
private void UpdateActionPerformed(java.awt.event.ActionEvent evt) {

if(table.getSelectedRow()== -1) {
if (table.getRowCount()== 0) {
msg.setText(" Table is empty");
} else {
msg.setText("please select any row");
}
} else {
String RegNumber=a1.getText();
String FirstName=a2.getText();
String LastName=a3.getText();
String Result=a4.getText();
double x=Float.parseFloat(Result);
String Grade="";
String Decision="";
if((x <=100) && (x >=80)){
Grade="A";
}
else if((x <80) && (x >=70))
{
Grade="B";
}
else if((x <70) && (x >=60))
{
Grade="C";
}
if(x <60 && x >=50)
16
{
Grade= "D";
}
else if(x <=50 )
{
Grade= "E";
}
if(x>= 50)
{
Decision="PASS";
}
else{
Decision="FAIL";
}
try {
Class.forName("com.mysql.jdbc.Driver");
Connection y = DriverManager.getConnection("jdbc:mysql://localhost:3306/IprcDb", "root",
"");
String query = "Update results set Result="+Result+", First_Name='" + FirstName +
"',Last_Name='" +LastName+ "',Decision='"+Decision+"',Grade='"+Grade+"' WHERE
Reg_Number='" + RegNumber + "'";
PreparedStatement pst = y.prepareStatement(query);
pst.execute();
JOptionPane.showMessageDialog(null, " 1 record Updated successfully!!!");
} catch ( ClassNotFoundException | SQLException e) {
System.out.println(e); }
}
}

17
CODES TO SEARCH DATA FROM THE DATABASE
private void SearchActionPerformed(java.awt.event.ActionEvent evt) {

String RegNumber=a1.getText();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/IprcDb",
"root", "");
PreparedStatement ps = conn.prepareStatement(" SELECT * FROM results WHERE
Reg_Number=?");
ps.setString(1, RegNumber);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
a2.setText(rs.getString("First_Name"));
a3.setText(rs.getString("Last_Name"));
a4.setText(rs.getString("Result"));
}
}
catch(ClassNotFoundException | SQLException ex)
{
System.out.println(ex) ;
}

18
CODES TO DELETE DATA FROM THE DATABASE
private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/IprcDb",
"root", "");
String queryss = " DELETE FROM results WHERE Reg_Number=?";
int p = JOptionPane.showConfirmDialog(null, " are you sure to delete?", "confirm",
JOptionPane.YES_NO_OPTION);
if (p == 0){
PreparedStatement ps = con.prepareStatement(queryss);
ps.setString(1,a1.getText());
ps.execute();
JOptionPane.showMessageDialog(null, " 1 Record deleted successfull");
}
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e);
}
}

19
CODES TO CLEAR DATA FROM THE FORM
private void ClearActionPerformed(java.awt.event.ActionEvent evt) {
a1.setText(null);
a2.setText(null);
a3.setText(null);
a4.setText(null);
}

CODES TO EXIT THE FORM


private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

20
FINAL OUTPUT AFTER INSERTING SOME DATA IN THE DATABASE

21

You might also like