0% found this document useful (0 votes)
18 views16 pages

Week 03 - GUI and Databases

The document outlines the creation of Graphical User Interfaces (GUIs) in Java using frameworks like AWT, Swing, and JavaFx. It also discusses connecting Java applications to various databases using Java Database Connectivity (JDBC), detailing the necessary steps and providing a sample code for inserting a record into a MySQL database. Key components include importing packages, loading drivers, establishing connections, creating statements, executing queries, and processing results.

Uploaded by

mewoxas729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views16 pages

Week 03 - GUI and Databases

The document outlines the creation of Graphical User Interfaces (GUIs) in Java using frameworks like AWT, Swing, and JavaFx. It also discusses connecting Java applications to various databases using Java Database Connectivity (JDBC), detailing the necessary steps and providing a sample code for inserting a record into a MySQL database. Key components include importing packages, loading drivers, establishing connections, creating statements, executing queries, and processing results.

Uploaded by

mewoxas729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GRAPHICAL USER INTERFACE (GUI) AND DATABASES

GUI

• To create GUIs in java, we use one of the following frameworks:

• AWT (Abstract Window Toolkit)


• Swing
• JavaFx

• These frameworks provide a range of tools and components for creating GUIs
that are not only functional but also visually appealing.
DATABASES
DATABASES

Data

Database, give the


user with id number 2

Database
DATABASES

Data

Structured Query
Language

SQL

Database
DATABASES

• Not everyone knows SQL or how to build applications

• So, it is your job as the programmer to build applications

• You can use any programming language you like


DATABASES

Application Database
JAVA DATABASE CONNECTIVITY (JDBC)

Application JDBC Database

Java Database Connectivity


JAVA DATABASES CONNECTIVITY (JDBC)

• There are a lot of DBMS. Below are some of them:

• Oracle
• MySQL
• PostgreSQL
• Microsoft SQL Server
• Etc

• All these databases have libraries to connect with java.


JAVA DATABASES CONNECTIVITY (JDBC)

• So, you need these libraries, if you want to connect your


application to these database.

• First thing, get the library, depending on the database you are using.
• This is like a recipe

• Then, follow the 7 steps, If you want to connect your java application to
the database
JAVA DATABASES CONNECTIVITY (JDBC)

• 7 steps to connect your java application to the database

1. import the Package


a. (import java.sql.*;)

2. a. Load the Driver (different driver for different databases)


- MySQL: com.mysql.jdbc.Driver

b. Register the Driver


- To register the driver, use a method called forName()
Class.forName(com.mysql.jdbc.Driver)

* Where do we load it from?


- download a jar file / library from the internet (mysql-connector)
- once downloaded, load it into project, then register
JAVA DATABASES CONNECTIVITY (JDBC)

• 7 steps to connect your java application to the database

3. Create / Establish the Connection


a. Instantiate an interface called Connection

4. Create a Statement
a. Normal statement (Statement)
- INSERT INTO employee VALUES (1, ‘James’);

a. PreparedStatement
- for executing inbuilt or predefined query with different values
int empID = 1;
String name = “James”;
INSERT INTO employee VALUES (?, ?);

a. Collable Statement
- for executing Procedural Language (PL) / Stored procedures
JAVA DATABASES CONNECTIVITY (JDBC)

• 7 steps to connect your java application to the database

5. Execute the Query


6. Process the Results

7. Close the connection

• If everything goes well, you get the response.


JAVA DATABASES CONNECTIVITY (JDBC)

Simple java program to insert a record into the database


JAVA DATABASES CONNECTIVITY (JDBC)
Insert Record into Database (MySQL)

package jdbcdemo;

import java.sql.*; // 1. import the Package

public class InsertRecord {

public static void main(String[] args) throws Exception {

String url = "jdbc:mysql://localhost:3306/prg620s"; // database url


String uname = "root"; // database username
String pass = "123"; // database password

String query = "INSERT INTO employee VALUES (1, ‘James’)";


JAVA DATABASES CONNECTIVITY (JDBC)

Class.forName("com.mysql.jdbc.Driver"); // 2. load and register the driver

Connection con = DriverManager.getConnection(url, uname, pass); /*3. create or


establish connection */
Statement st = con.createStatement(); // 4. create statement

int count = st.executeUpdate(query); /* 5. Execute query. Here use excecuteUpdate.


excecuteUpdate does not return a table, instead it affects a certain number of rows */

System.out.println(count + " row(s) affected.");

st.close(); // close statement


con.close(); //close connection
}
}

You might also like