0% found this document useful (0 votes)
79 views24 pages

Praktikum2. Java Database - Part2 RNS

The document discusses connecting Java applications to databases using JDBC. It describes the key steps: 1. Loading the appropriate JDBC driver for the target database. This registers the driver with the DriverManager. 2. Obtaining a database connection through the DriverManager, specifying properties like the URL, username, and password. 3. Using the Connection object to create Statement objects for executing SQL statements like SELECT, and processing the ResultSet objects that are returned. It provides examples of connecting to databases like PostgreSQL and executing simple queries to retrieve and display data. The document also outlines some sample database applications that could be built, like a phone book application storing contact data in a database table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views24 pages

Praktikum2. Java Database - Part2 RNS

The document discusses connecting Java applications to databases using JDBC. It describes the key steps: 1. Loading the appropriate JDBC driver for the target database. This registers the driver with the DriverManager. 2. Obtaining a database connection through the DriverManager, specifying properties like the URL, username, and password. 3. Using the Connection object to create Statement objects for executing SQL statements like SELECT, and processing the ResultSet objects that are returned. It provides examples of connecting to databases like PostgreSQL and executing simple queries to retrieve and display data. The document also outlines some sample database applications that could be built, like a phone book application storing contact data in a database table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Java Database

PART 2

Reni Nursyanti, M.Kom


6.3 Koneksi Aplikasi Java ke
Database
Tahapan Akses Database dengan JDBC
Driver
MySQL PostgreSQL Oracle

DriverManager

Connection
user password host database url

Statement
select update create insert

ResultSet
JDBC (Java DB Connectivity)

Java application
{ ...
"SELECT ... FROM ... WHERE"
... }

DBMS
JDBC Drivers
Java
application
JDBC-API
JDBC-
Driver manager

Native JDBC- JDBC-ODBC Native


Protocol driver Net-driver bridge API-driver
DB-
ODBC Client library
Middleware

Client library
Running a JDBC Application
Phase Task Relevant java.sql classes

Initialisation Load driver DriverManager


Create connection Connection

Processing Generate SQL statements Statement


Process result data ResultSet etc.

Terminate connection Connection


Termination
Release data structures Statement etc.
A Simple JDBC application

import java.sql.*;
loadDriver public class jdbctest {
public static void main(String args[]){
getConnection try{
Class.forName("org.postgresql.Driver");
createStatement Connection con = DriverManager.getConnection
("jdbc:postgresql://lsir-cis-pc8:5401/pcmdb", "user", "passwd");
execute(SQL) Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
Result handling ("select name, number from pcmtable where number < 2");
while(rs.next())
yes System.out.println(rs.getString(1) + " (" + rs.getInt(2) + ")");
More stmt.close()
results ? con.close();
} catch(Exception e){
no System.err.println(e);
closeStatment }
}}
closeConnection
Loading of Driver
• Creates an instance of the driver
• Registers driver in the driver manager
• Explicit loading
String l_driver = "org.postgresql.Driver";
Class.forName(l_driver);
• Several drivers can be loaded and
registered
Implicit Driver Loading
• Setting system property: jdbc.drivers
• A colon-separated list of driver classnames
• Can be set when starting the application
java -Djdbc.drivers=org.postgresql.Driver application
• Can also be set from within the Java application
Properties prp = System.getProperties();
prp.put("jdbc.drivers"
"com.mimer.jdbc.Driver:org.postgresql.Driver");
System.setProperties(prp);
• The DriverManager class attempts to load all the
classes specified in jdbc.drivers when the
DriverManager class is initialized
Addressing Database
• A connection is a session with one database
• Databases are addressed using a URL of the
form "jdbc:<subprotocol>:<subname>"
• Examples
jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
• Defaults: host=localhost, port=5432
Connecting to Database
• Connection is established
Connection con = DriverManager.getConnection(URL,USERID,PWD);
• Connection properties (class Properties)
• Close the connection
con.close();
Simple SQL Statements
• Statement object for invocation
stmt = conn.createStatement();
ResultSet rset= stmt.executeQuery(
"SELECT address,script,type FROM worklist");

• ResultSet object for result processing


6.4 Studi Kasus Aplikasi Database
Aplikasi Database
1. Aplikasi Telepon
2. Aplikasi Guru
3. Aplikasi Bank
4. Aplikasi Penjualan Barang
Aplikasi Telepon
Aplikasi Telepon

1. Ekstrak xampplite dan jalankan xampp_start.exe untuk


mengaktifkan Apache dan MySQL
2. Buka browser, arahkan url ke http://localhost dan klik link
ke phpMyAdmin
3. Buat database telepon
4. Buat satu table bukutelepon, yang berisi field dengan id
sebagai primary key (PK):
1. id integer (auto increment)
2. nama varchar(20)
3. alamat varchar(50)
4. telepon varchar(20)
5. handphone varchar(20)

You might also like