GUI : PART II
MVC PATTERN
JDBC
GUI PART 2 : INTERPRETED BY : ASAMINEW G. 06/18/2025 1
MVC pattern
• MVC pattern : - a technique that separate look from action
- makes the big task easier and less error
prone
- allow to divide the task of designing a Swing
GUI into two main subtasks: (1) designing and coding the
appearance of the GUI on the screen; (2) designing and coding
the actions performed in response to button clicks and other
user actions.
• MVC is stands for Model View Controller
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 2
Cont. …
• Model pattern : - performs the heart of the application
• View pattern : - the output part; it displays a picture of the
Model’s state
• Controller pattern : - the input part; it relays commands from
the user to the Model
• Each of the three interacting parts is realized as an object
with responsibility for its own tasks. you can have a single
object with different methods to realize each of the roles
Model, View, and Controller.
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 3
Cont. …
• MVC Pattern communication
Request for data
Response to (manipulation )
the request
View Controller Model
… ….. Data 1
Delete ( ); ….. Data 2
…. …. ….
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 4
Relational data model
• Suppose we have the following employee table
Employee Secretary
id Fnam Mnam age EId Tspeed
e e 5202 100char/
5201 abebe kebed 22 sec
e
5202 almaz 20
Key relationship
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 5
SQL
• SQL - stands for Structural Query Language
- a command used to insert data into a table, delete data
from a table, retrieve data from a table and update data of table
• Insertion :
• Keywords – INSERT, INTO, VALUES
• Syntax – insert into tableName (list of column [optional]) values(data
to be inserted)
• E.g. insert into employee values(5201, ‘abebe’, ‘kebede’, 22) or
insert into employee (id, Fname, age) values(5202, ‘almaz’, 20)
• You have to specify column name if number of data is not the same
as a number of column
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 6
Cont. …
• Modifying data – updating column value
• Keywords – UPDATE, SET, WHERE
• Syntax – update tableName
set columnName = newData
where columnName = previousData
• E.g. update employee
set Fname = ‘chale’
where id = 5201 or
update employee
set Fname = ‘asnakech’
where Fname = ‘almaz’
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 7
Cont. …
• Displaying data – retrieve data and show to the user
• Keywords – SELECT, FROM, WHERE, AND, OR
• Syntax – select [all | distinct | list column]
from tableName list
where condition
• E.g. select * from employee where id = 5201 or
select id, Fname
from employee
where id = 5201 and age > 20
* => all column
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 8
Cont. …
• Displaying data by sorting – retrieve data and show to the
user according to the sorting condition
• Keywords – SELECT, FROM, WHERE, ORDER, BY
• Syntax – select [all | distinct | list column]
from tableName list
where condition
order by columnName
• E.g. select id, Fname
from employee
where id = 5201
order by Fname
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 9
Cont. …
• Joining : - displaying data from multiple
tables
• Keywords – SELECT, FROM, JOIN, AS, ON
• E.g. select * from employee as e
join secretary as s
on e.id = s.Eid
• The output of one selection may be given as
an input of another selection
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 10
JDBC
• Java Database Connectivity : - a way of communication
between java and database server
objec
Java program
t
JDBC
Sql
Tables type
s
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 11
Cont. …
• Jdbc involve the following things :
• Driver Manager
• knows how to connect to different DBs
• Connection
• represents a connection to a particular DB
• Statement
• a query or other request made to a DB
• Use executeQuery/executeUpdate method to execute query
• Result Set
• results returned from a query
• access contents via getXXX (column index/label) methods
• Use next() method to traverse the set in the row
• next() method return true if there is next row, else it returns false
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 12
Cont. …
• Loading drivers :
• Class.forName("com.mysql.jdbc.Driver"); - used to load mysql
connector driver which is located in the com.mysql.jdbc package and it can
be directly downloaded from mysql web site.
• Throw ClassNotFoundException, so you have to catch it using try…catch
• Creating connection :
String url = “jdbc:mysql:mydatabase”;
Connection conn = DriverManager.getConnection(url, ”UserName",
”Password");
• Mydatabase is represent database server address
E.g. //localhost:3306/databaseName
• Throw SQLException if a database access error occurs or url is null
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 13
Cont. …
• Statement :
• Statement s = conn.createStatement();
• Throw SQLException if a database access error occurs or this method is called on a
closed connection
• Submitting a SQL Query to a DBMS
• The following steps should be performed in order to submit a SQL statement to a
DBMS using a Statement (S) object:
• 1. Define a SQL query as a string
• 2. Create a S object
• If the SQL statement is one of CREATE, INSERT,DELETE, UPDATE, or SET type:
• Apply executeUpdate( )method onto a S object Else (if the SQL statement is of the
SELECT type):
• 3. Create a ResultSet object
• 4. Feed into the ResultSet object the return value of applying executeQuery()
method onto S object
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 14
Cont. …
• Example : // constructor
package jdbc; public DBconnect() {
try {
import java.sql.*;
String url = "jdbc:mysql://localhost:3306/EMS";
Class.forName("com.mysql.cj.jdbc.Driver");
/*a class used to create Connection con =
connection and provide DriverManager.getConnection(url, "root", "");
different operation */ Statement st = con.createStatement();
class DBConnect { } catch (ClassNotFoundException |
SQLException e) {
private Connection
System.out.println("error while connecting
con;
to DB : " + e);
private Statement st; }
private ResultSet rs; }
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 15
Cont. …
public void getData() { // a method that return all data from account table
try {
String q = "select * from account";
rs = st.executeQuery(q);
System.out.println("records from DB");
while (rs.next()) {
String name = rs.getString(2);
String pass = rs.getString(3);
System.out.println("Name = " + name + " password = " + pass);
}
} catch (SQLException e) {
System.out.println(e);
}
}
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 16
Cont. …
public String operation(String sql) {
// used to execute insert, delete, and update query
int r = 0;
try {
r = st.executeUpdate(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return " (" + r + ") row affected";
}
} // end of class DBConnect
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 17
Cont. …
package jdbc; public static void main(String[] args) {
DBConnect con = new DBConnect();
import javax.swing.JOptionPane;
String i =
public class Main { JOptionPane.showInputDialog(null, msg,
private static String msg = title, 1);
"enter 1 to insert data \n" String sql = null;
switch (i) {
+ "enter 2 to display data\
case "1":
n"
sql = "insert into account(uname,
+ "enter 3 to delete data\n" password) values('dage', '123')";
+ "enter 4 to upadte data\ break;
n"; case "2":
private static String title = con.getData();
"select your option"; break;
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 18
Cont. …
case "3": default:
sql = "delete from break;
account where uname = }// end of switch
'dage'"; if (!i.equals("2")) {
break; sql = con.operation(sql);
case "4":
JOptionPane.showMessageDialog(
sql = "update null, sql);
account set uname = 'abebe'
}
where uname = 'dage'";
}// end of main method
break;
}// end of Main class
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 19
…
•…
GUI part 2 : interpreted BY : Asaminew G. 06/18/2025 20