0% found this document useful (0 votes)
43 views

Database: Java Fundamental

This document discusses Java Database Connectivity (JDBC) and how to connect to and query databases from Java programs. It explains that JDBC drivers are needed to connect to different database types and how they allow Java programs to execute SQL statements and access results. It also covers potential issues when testing JDBC drivers and provides examples of using JDBC to connect to a database, perform queries, inserts, updates and deletes.

Uploaded by

adisaputra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Database: Java Fundamental

This document discusses Java Database Connectivity (JDBC) and how to connect to and query databases from Java programs. It explains that JDBC drivers are needed to connect to different database types and how they allow Java programs to execute SQL statements and access results. It also covers potential issues when testing JDBC drivers and provides examples of using JDBC to connect to a database, perform queries, inserts, updates and deletes.

Uploaded by

adisaputra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Database

JAVA FUNDAMENTAL

HILMY A. TAWAKAL 1
JDBC

JDBC: Java Database Connectivity

You need a JDBC driver to access a database from a
Java program

Different databases require different drivers

Drivers may be supplied by the database
manufacturer or a third party

When your Java program issues SQL commands, the
driver forwards them to the database and lets your
program analyze the results

04/25/2019 HILMY A. TAWAKAL 2


JDBC Architecture

04/25/2019 HILMY A. TAWAKAL 3


Testing the JDBC Driver: Possible Problems

Missing driver
– Check the class path and the driver name

Driver cannot connect to the database
– The database may not be started
– Database may not be reachable

Failed login
– Check the database name, user name, and password

A missing Test table
– Make sure you create and populate the table as described in the
database test

04/25/2019 HILMY A. TAWAKAL 4


Connection
public class Koneksi {
​private Connection con;
private String dbhost = "localhost";
private String dbuser = "myuser";
private String dbpass = "mypass";
private String dbname = "anggaran";
public Koneksi() {
initDB();
}

04/25/2019 HILMY A. TAWAKAL 5


Connection
private void initDB() {
try {
String url = "jdbc:mysql://"+dbhost+":3306/"+dbname;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url,dbuser,dbpass);
System.out.println("Koneksi Sukses");
} catch (Exception ex) {
System.out.println("Koneksi Gagal");
ex.printStackTrace();
}
}

04/25/2019 HILMY A. TAWAKAL 6


Connection
public Connection getKoneksi(){
try {
if(this.con==null || this.con.isClosed()){
this.initDB();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return this.con;
}

04/25/2019 HILMY A. TAWAKAL 7


Connection
public void closeKoneksi(){
if(this.con != null){
try {
this.con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

04/25/2019 HILMY A. TAWAKAL 8


Selecting data
String sql = "select * from user";
Connection con = new Koneksi().getKoneksi();
ResultSet rs =
con.createStatement().executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String email = rs.getString("email");
String pass = rs.getString("password");
String fullname =rs.getString("fullname");
}

04/25/2019 HILMY A. TAWAKAL 9


Inserting data
Connection con = new Koneksi().getKoneksi();
String sql = "INSERT INTO
user(email,password,fullname) values(?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, “[email protected]”);
ps.setString(2, this.hashPass(“pass”));
ps.setString(3, “hilmi tawakal”);
ps.executeUpdate();

04/25/2019 HILMY A. TAWAKAL 10


Updating data
Connection con = new Koneksi().getKoneksi();
String sql = "UPDATE user set fullname=? where
id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,10);
ps.setString(2, “hilmi a. tawakal”);
ps.executeUpdate();

04/25/2019 HILMY A. TAWAKAL 11


Updating data
Connection con = new Koneksi().getKoneksi();
String sql = "UPDATE user set fullname=? where
id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,10);
ps.setString(2, “hilmi a. tawakal”);
ps.executeUpdate();

04/25/2019 HILMY A. TAWAKAL 12

You might also like