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
}
}