Last active
August 11, 2016 04:34
-
-
Save dimMaryanto93/9670215f595e5415326e78df2d22977a to your computer and use it in GitHub Desktop.
Membuat table Mahasiswa di database MariaDB/MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import belajar.jdbc.model.Jurusan; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class BatchProcessing { | |
public static void main(String[] args) { | |
try { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
koneksi.setAutoCommit(false); | |
// inisialisasi data | |
List<Jurusan> daftarJurusan = new ArrayList<>(); | |
daftarJurusan.add(new Jurusan("AK", "Komputer Akutansi")); | |
daftarJurusan.add(new Jurusan("MI", "Management Informatika")); | |
daftarJurusan.add(new Jurusan("SE", "Ekonomi")); | |
daftarJurusan.add(new Jurusan("IF", "Informatika")); | |
daftarJurusan.add(new Jurusan("SI", "Sistem Informasi")); | |
daftarJurusan.add(new Jurusan("HI", "Hubungan Internasional")); | |
// membuat query | |
PreparedStatement ps = koneksi.prepareStatement("INSERT INTO jurusan(id, nama) VALUES (?,?)"); | |
// akan menampilkan sql: INSERT INTO jurusan (id, nama) VALUES ('KODE', 'NAMA'); | |
for (Jurusan j : daftarJurusan) { | |
ps.setString(1, j.getId()); | |
ps.setString(2, j.getNama()); | |
// ditambahkan ke batch / memory | |
ps.addBatch(); | |
System.out.println("ditambahkan ke batch: " + ps.toString()); | |
} | |
// dikirim ke database supaya di ekseskusi | |
ps.executeBatch(); | |
System.out.println("queries dikirim ke database!"); | |
// bersihkan batch | |
ps.clearBatch(); | |
ps.close(); | |
koneksi.commit(); | |
System.out.println("queries disimpan secara permanen"); | |
// keneksinya di close | |
koneksi.close(); | |
} catch (SQLException ex) { | |
Logger.getLogger(BatchProcessing.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create database jdbc_mysql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE FROM mahasiswa | |
WHERE nim = '10511148'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc mahasiswa; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Properties; | |
public class ExternalisasiSetupJDBC { | |
public static void main(String[] args) throws IOException { | |
// load configurasi dari file jdbc.properties | |
Properties prop = new Properties(); | |
InputStream input = ExternalisasiSetupJDBC.class.getResourceAsStream("/jdbc.properties"); | |
prop.load(input); | |
input.close(); | |
// menampilkan valuenya berdasarkan key dari file jdbc.properties | |
System.out.println("Host " + prop.getProperty("host")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO mahasiswa | |
VALUES ('10511148','Dimas','SI'), | |
('10511150','Riansyah','SI'), | |
('10511173','Hanif','SI'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert into mahasiswa (nim, nama, jurusan) | |
values ('10511148', 'Dimas Maryanto', 'SI'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import belajar.jdbc.model.Mahasiswa; | |
public class JavaBeanDeleteMahasiswa{ | |
public static void main(String[] args) throws SQLException{ | |
// membuat objek mahasiswa | |
Mahasiswa m = new Mahasiswa(); | |
m.setNim("10511173"); | |
m.setNama("Muhamad Hanif"); | |
m.setJurusan("IF"); | |
String sql = "DELETE FROM mahasiswa WHERE nim = ?"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, mahasiswa.getNim()); | |
ps.executeUpdate(); | |
ps.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.List; | |
import belajar.jdbc.model.Mahasiswa; | |
public class JavaBeanSelectAllMahasiswa{ | |
public static void main(String[] args) throws SQLException{ | |
String sql = "select * from mahasiswa"; | |
List<Mahasiswa> daftarMahasiswa = new ArrayList<>(); | |
Statement st = koneksi.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
while (rs.next()) { | |
// membuat objek mahasiswa | |
Mahasiswa m = new Mahasiswa(); | |
/** | |
* mengambil nilai dari database dengan rs.getTipeData('nama_kolom'); | |
* kemudian value tersebut dimasukan ke method setter | |
**/ | |
m.setNim(rs.getString(1)); | |
m.setNama(rs.getString(2)); | |
m.setJurusan(rs.getString(3)); | |
// menambahkan object mahasiswa ke array | |
daftarMahasiswa.add(m); | |
} | |
st.close(); | |
rs.close(); | |
// untuk menampilkan data dari List ke text biasa | |
for (Mahasiswa mhs : daftarMahasiswa) { | |
StringBuilder sb = new StringBuilder(); | |
// mengambil nilai dari array kemudian tampilkan ke objek StringBuilder | |
sb.append("NIM: ").append(mhs.getNim()).append("\n"); | |
sb.append("Nama: ").append(mhs.getNama()).append("\n"); | |
sb.append("Jurusan: ").append(mhs.getJurusan()).append("\n"); | |
System.out.println(sb.toString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import belajar.jdbc.model.Mahasiswa; | |
public class JavaBeanTambahMahasiswa{ | |
public static void main(String[] args) throws SQLException{ | |
// membuat objek mahasiswa | |
Mahasiswa m = new Mahasiswa(); | |
m.setNim("10511173"); | |
m.setNama("Muhamad Hanif"); | |
m.setJurusan("SI"); | |
String sql = "INSERT INTO mahasiswa (nim, nama, jurusan) VALUES (?,?,?)"; | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
// masih sama seperti yang posting sebelumnya hanya diganti dengan objek mahasiswa | |
ps.setString(1, mahasiswa.getNim()); | |
ps.setString(2, mahasiswa.getNama()); | |
ps.setString(3, mahasiswa.getJurusan()); | |
ps.executeUpdate(); | |
ps.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import belajar.jdbc.model.Mahasiswa; | |
public class JavaBeanUbahMahasiswa{ | |
public static void main(String[] args) throws SQLException{ | |
// membuat objek mahasiswa | |
Mahasiswa m = new Mahasiswa(); | |
m.setNim("10511173"); | |
m.setNama("Muhamad Hanif"); | |
m.setJurusan("IF"); | |
String sql = "UPDATE mahasiswa SET jurusan = ? WHERE nim = ?"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, mahasiswa.getJurusan()); | |
ps.setString(2, mahasiswa.getNim()); | |
ps.executeUpdate(); | |
ps.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drop database if exists jdbc_mysql; | |
create database if not exists jdbc_mysql; | |
use jdbc_mysql; | |
CREATE TABLE IF NOT EXISTS mahasiswa( | |
nim varchar(8) primary key, | |
nama varchar(25) not null, | |
id_jurusan varchar(2) not null | |
)engine = InoDB; | |
CREATE TABLE IF NOT EXISTS JURUSAN( | |
id varchar(2) primary key, | |
nama varchar(25) not null | |
)engine = InoDB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
host=localhost | |
port=3306 | |
db=mysql | |
dbname=jdbc_mysql | |
user=root | |
passwd=admin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
public class MainApplication { | |
public static void main(String[] args) { | |
try { | |
Connection koneksi = DriverManager | |
.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "DELETE FROM mahasiswa WHERE nim = ?"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
System.out.println("sebelum set value: " + ps.toString()); | |
ps.setString(1, "10511150"); | |
System.out.println("setelah set value: " + ps.toString()); | |
ps.executeUpdate(); | |
ps.close(); | |
koneksi.close(); | |
} catch (SQLException ex) { | |
System.err.println("Database tidak ditemukan!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
public class MainApplication { | |
public static void main(String[] args) { | |
try { | |
Connection koneksi = DriverManager. | |
getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "INSERT INTO mahasiswa (nim, nama, jurusan) VALUES (?,?,?)"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
System.out.println("sebelum set value: "+ps.toString()); | |
ps.setString(1, "10511150"); | |
ps.setString(2, "Riansyah Permana"); | |
ps.setString(3, "SI"); | |
System.out.println("setelah set value: " + ps.toString()); | |
ps.executeUpdate(); | |
ps.close(); | |
koneksi.close(); | |
} catch (SQLException ex) { | |
System.err.println("Database tidak ditemukan!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
public class MainApplication { | |
public static void main(String[] args) { | |
try { | |
Connection koneksi = DriverManager | |
.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "select * from mahasiswa"; | |
Statement st = koneksi.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
while (rs.next()) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("NIM: ").append(rs.getString(1)).append(", "); | |
sb.append("Nama: ").append(rs.getString("nama")).append(", "); | |
sb.append("Jurusan: ").append(rs.getString(3)); | |
System.out.println(sb.toString()); | |
} | |
koneksi.close(); | |
} catch (SQLException ex) { | |
System.err.println("Database tidak ditemukan!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
public class MainApplication { | |
public static void main(String[] args) { | |
try { | |
Connection koneksi = DriverManager | |
.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "UPDATE mahasiswa SET jurusan = ? WHERE nim = ?"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
System.out.println("sebelum set value: "+ps.toString()); | |
ps.setString(1, "IF"); | |
ps.setString(2, "10511150"); | |
System.out.println("setelah set value: " + ps.toString()); | |
ps.executeUpdate(); | |
ps.close(); | |
koneksi.close(); | |
} catch (SQLException ex) { | |
System.err.println("Database tidak ditemukan!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
public class Jurusan implements Serializable { | |
public Jurusan() { | |
} | |
public Jurusan(String id, String nama) { | |
this.id = id; | |
this.nama = nama; | |
} | |
private String id; | |
private String nama; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getNama() { | |
return nama; | |
} | |
public void setNama(String nama) { | |
this.nama = nama; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
public class MainApplication { | |
public static void main(String[] args) { | |
try { | |
String url = "jdbc:mysql://localhost:3306/jdbc_mysql"; | |
Connection koneksi = DriverManager.getConnection( url, "root", "admin"); | |
System.out.println("Database ditemukan!"); | |
koneksi.close(); | |
} catch (SQLException ex) { | |
System.err.println("Database tidak ditemukan!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc.model; | |
import java.io.Serializable; | |
/** | |
* | |
* @author dimMaryanto | |
*/ | |
public class Mahasiswa implements Serializable{ | |
private String nim; | |
private String nama; | |
private String jurusan; | |
public String getNim() { | |
return nim; | |
} | |
public void setNim(String nim) { | |
this.nim = nim; | |
} | |
public String getNama() { | |
return nama; | |
} | |
public void setNama(String nama) { | |
this.nama = nama; | |
} | |
public String getJurusan() { | |
return jurusan; | |
} | |
public void setJurusan(String jurusan) { | |
this.jurusan = jurusan; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table mahasiswa( | |
nim varchar(8) not null primary key, | |
nama varchar(50) not null, | |
jurusan varchar(2) not null | |
) engine=InoDB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
/** | |
* | |
* @author dimMaryanto | |
*/ | |
public class MahasiswaUpdated { | |
private String nim; | |
private String nama; | |
private Jurusan jurusan; | |
public String getNim() { | |
return nim; | |
} | |
public void setNim(String nim) { | |
this.nim = nim; | |
} | |
public String getNama() { | |
return nama; | |
} | |
public void setNama(String nama) { | |
this.nama = nama; | |
} | |
public Jurusan getJurusan() { | |
return jurusan; | |
} | |
public void setJurusan(Jurusan jurusan) { | |
this.jurusan = jurusan; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import belajar.jdbc.model.Jurusan; | |
import belajar.jdbc.repo.RepositoryJurusan; | |
import belajar.jdbc.service.ServiceJurusan; | |
import java.sql.SQLException; | |
import java.util.List; | |
public class ModularJDBC { | |
public static void main(String[] args) throws SQLException { | |
RepositoryJurusan repo = new ServiceJurusan(); | |
// cara menggunakan untuk insert data | |
System.out.println("INSERT ---------------------"); | |
repo.save(new Jurusan("IF", "Informatika")); | |
System.out.println("FIND BY ID------------------"); | |
// cara menggunakan untuk mencari data berdasarkan id | |
Jurusan IF = repo.findById("IF"); | |
System.out.println("Kode IF adalah " + IF.getNama()); | |
System.out.println("UPDATE -----------------------"); | |
IF.setNama("INFORMATIKA"); | |
repo.update(IF); | |
System.out.println("FIND BY ID after Update------------------"); | |
// cara menggunakan untuk mencari data berdasarkan id | |
Jurusan IFUpdated = repo.findById("IF"); | |
System.out.println("Kode IF adalah " + IFUpdated.getNama()); | |
System.out.println("DELETE ---------------------"); | |
// cara menggunakan untuk hapus data | |
repo.delete(IFUpdated.getId()); | |
System.out.println("SELECT ALL ------------------"); | |
List<Jurusan> daftarJurusan = repo.selectAll(); | |
System.out.println("Jumlah data jurusan: " + daftarJurusan.size()); | |
for (Jurusan jur : daftarJurusan) { | |
System.out.println("Kode: " + jur.getId() | |
+ ", Namanya: " + jur.getNama()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import belajar.jdbc.model.Jurusan; | |
import belajar.jdbc.model.MahasiswaUpdated; | |
import belajar.jdbc.repo.RepositoryJurusan; | |
import belajar.jdbc.repo.RepositoryMahasiswa; | |
import belajar.jdbc.service.ServiceJurusan; | |
import belajar.jdbc.service.ServiceMahasiswa; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ModularJDBCMahasiswa { | |
public static void main(String[] args) throws SQLException { | |
RepositoryJurusan repo = new ServiceJurusan(); | |
RepositoryMahasiswa repoMhs = new ServiceMahasiswa(); | |
// cara menggunakan untuk insert data | |
System.out.println("INSERT JURUSAN---------------------"); | |
Jurusan jurusan = new Jurusan("IF", "Informatika"); | |
repo.save(jurusan); | |
System.out.println("INSERT MAHASISWA ------------------"); | |
MahasiswaUpdated mhs = new MahasiswaUpdated(); | |
mhs.setNim("10511148"); | |
mhs.setNama("Dimas Maryanto"); | |
mhs.setJurusan(jurusan); | |
repoMhs.save(mhs); | |
List<MahasiswaUpdated> daftarMahasiswa = repoMhs.selectAll(); | |
System.out.println("Jumlah data mahasiswa: " + daftarMahasiswa.size()); | |
for (MahasiswaUpdated mahasiswa : daftarMahasiswa) { | |
System.out.println( | |
"NIM: " + mahasiswa.getNim() | |
+ ", Nama: " + mahasiswa.getNama() | |
+ ", Nama Jurusan: " + mahasiswa.getJurusan().getNama()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc.repo; | |
import belajar.jdbc.model.Jurusan; | |
import java.sql.SQLException; | |
import java.util.List; | |
/** | |
* | |
* @author dimMaryanto | |
*/ | |
public interface RepositoryJurusan { | |
public List<Jurusan> selectAll() throws SQLException; | |
public Jurusan save(Jurusan jurusan) throws SQLException; | |
public Jurusan update(Jurusan jurusan) throws SQLException; | |
public void delete(String id) throws SQLException; | |
public Jurusan findById(String id) throws SQLException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc.repo; | |
import belajar.jdbc.model.MahasiswaUpdated; | |
import java.sql.SQLException; | |
import java.util.List; | |
/** | |
* | |
* @author dimMaryanto | |
*/ | |
public interface RepositoryMahasiswa { | |
public List<MahasiswaUpdated> selectAll() throws SQLException; | |
public MahasiswaUpdated save(MahasiswaUpdated mhs) throws SQLException; | |
public MahasiswaUpdated update(MahasiswaUpdated mhs) throws SQLException; | |
public void delete(String id) throws SQLException; | |
public MahasiswaUpdated findById(String id) throws SQLException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * from mahasiswa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * from mahasiswa | |
where nim = '10511148'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * | |
from mahasiswa | |
where nim = '10511150'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc.service; | |
import belajar.jdbc.model.Jurusan; | |
import belajar.jdbc.repo.RepositoryJurusan; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* | |
* @author dimMaryanto | |
*/ | |
public class ServiceJurusan implements RepositoryJurusan { | |
@Override | |
public List<Jurusan> selectAll() throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "select * from jurusan"; | |
List<Jurusan> daftarJurusan = new ArrayList<>(); | |
Statement st = koneksi.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
while (rs.next()) { | |
Jurusan m = new Jurusan(rs.getString(1), rs.getString(2)); | |
daftarJurusan.add(m); | |
} | |
st.close(); | |
rs.close(); | |
return daftarJurusan; | |
} | |
@Override | |
public Jurusan save(Jurusan jurusan) throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "INSERT INTO jurusan (id, nama) VALUES (?,?)"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, jurusan.getId()); | |
ps.setString(2, jurusan.getNama()); | |
ps.executeUpdate(); | |
ps.close(); | |
return jurusan; | |
} | |
@Override | |
public Jurusan update(Jurusan jurusan) throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "UPDATE jurusan SET nama = ? WHERE id = ?"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, jurusan.getNama()); | |
ps.setString(2, jurusan.getId()); | |
ps.executeUpdate(); | |
return jurusan; | |
} | |
@Override | |
public void delete(String id) throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "DELETE FROM jurusan WHERE id = ?"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, id); | |
ps.executeUpdate(); | |
ps.close(); | |
} | |
@Override | |
public Jurusan findById(String id) throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "select * from jurusan"; | |
Statement st = koneksi.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
Jurusan jurusan = null; | |
if (rs.next()) { | |
jurusan = new Jurusan(rs.getString(1), rs.getString(2)); | |
} | |
st.close(); | |
rs.close(); | |
return jurusan; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc.service; | |
import belajar.jdbc.model.Jurusan; | |
import belajar.jdbc.model.MahasiswaUpdated; | |
import belajar.jdbc.repo.RepositoryMahasiswa; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ServiceMahasiswa implements RepositoryMahasiswa { | |
@Override | |
public List<MahasiswaUpdated> selectAll() throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "SELECT m.nim, m.nim, j.id, j.nama\n" | |
+ "FROM mahasiswa m JOIN jurusan j ON (m.id_jurusan = j.id)"; | |
List<MahasiswaUpdated> daftarMahasiswa = new ArrayList<>(); | |
Statement st = koneksi.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
while (rs.next()) { | |
MahasiswaUpdated m = new MahasiswaUpdated(); | |
m.setNim(rs.getString(1)); | |
m.setNama(rs.getString(2)); | |
// mendapatkan nilai jurusan dari kolom ke 3 dan ke 4 | |
Jurusan j = new Jurusan(rs.getString(3), rs.getString(4)); | |
m.setJurusan(j); | |
daftarMahasiswa.add(m); | |
} | |
st.close(); | |
rs.close(); | |
return daftarMahasiswa; | |
} | |
@Override | |
public MahasiswaUpdated save(MahasiswaUpdated mhs) throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "INSERT INTO mahasiswa (nim, nama, id_jurusan) VALUES (?,?,?)"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, mhs.getNim()); | |
ps.setString(2, mhs.getNama()); | |
// yang dipasing hanya id jurusannya aja | |
ps.setString(3, mhs.getJurusan().getId()); | |
ps.executeUpdate(); | |
ps.close(); | |
return mhs; | |
} | |
@Override | |
public MahasiswaUpdated update(MahasiswaUpdated mhs) throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "UPDATE mahasiswa SET id_jurusan = ?, nama = ? WHERE nim = ?"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, mhs.getJurusan().getId()); | |
ps.setString(2, mhs.getNama()); | |
ps.setString(3, mhs.getNim()); | |
ps.executeUpdate(); | |
ps.close(); | |
koneksi.close(); | |
return mhs; | |
} | |
@Override | |
public void delete(String id) throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "DELETE FROM mahasiswa WHERE nim = ?"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, id); | |
ps.executeUpdate(); | |
ps.close(); | |
koneksi.close(); | |
} | |
@Override | |
public MahasiswaUpdated findById(String id) throws SQLException { | |
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
String sql = "SELECT m.nim, m.nim, j.id, j.nama\n" | |
+ "FROM mahasiswa m JOIN jurusan j ON (m.id_jurusan = j.id)"; | |
List<MahasiswaUpdated> daftarMahasiswa = new ArrayList<>(); | |
Statement st = koneksi.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
MahasiswaUpdated m = null; | |
if (rs.next()) { | |
m = new MahasiswaUpdated(); | |
m.setNim(rs.getString(1)); | |
m.setNama(rs.getString(2)); | |
// mendapatkan nilai jurusan dari kolom ke 3 dan ke 4 | |
Jurusan j = new Jurusan(rs.getString(3), rs.getString(4)); | |
m.setJurusan(j); | |
daftarMahasiswa.add(m); | |
} | |
st.close(); | |
rs.close(); | |
return m; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package belajar.jdbc; | |
import belajar.jdbc.model.Jurusan; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
public class TransactionJDBC { | |
public static void main(String[] args) { | |
Jurusan j = new Jurusan("SI", "SISTEM INFORMASI"); | |
Connection koneksi = null; | |
// insert data SI | |
try { | |
koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
koneksi.setAutoCommit(false); | |
String sql = "INSERT INTO jurusan (id, nama) VALUES (?,?)"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, j.getId()); | |
ps.setString(2, j.getNama()); | |
ps.executeUpdate(); | |
ps.close(); | |
koneksi.commit(); | |
System.out.println("query ke 1 di eksekusi"); | |
} catch (SQLException ex) { | |
System.err.println("query ke 1 gagal disimpan"); | |
try { | |
koneksi.rollback(); | |
} catch (SQLException sql) { | |
System.err.println("gak bisa di rollback"); | |
} | |
} | |
try { | |
koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_mysql", "root", "admin"); | |
koneksi.setAutoCommit(false); | |
// insert data baru | |
j = new Jurusan("MI", "Management Informatika"); | |
String sql = "INSERT INTO jurusan (id, nama) VALUES (?,?)"; | |
PreparedStatement ps = koneksi.prepareStatement(sql); | |
ps.setString(1, j.getId()); | |
ps.setString(2, j.getNama()); | |
ps.executeUpdate(); | |
ps.close(); | |
System.out.println("query ke 2 di eksekusi"); | |
// udpate jurusan IS ke IF | |
j.setId("IF"); | |
sql = "UPDATE jurusan SET id = ? WHERE nama = ?"; | |
ps = koneksi.prepareStatement(sql); | |
ps.setString(1, j.getId()); | |
ps.setString(2, j.getNama()); | |
ps.executeUpdate(); | |
ps.close(); | |
System.out.println("query ke 3 di eksekusi"); | |
koneksi.commit(); | |
koneksi.close(); | |
} catch (SQLException ex) { | |
System.err.println("query ke 2 dan 3 gagal disimpan"); | |
try { | |
koneksi.rollback(); | |
} catch (SQLException sql) { | |
System.err.println("gak bisa di rollback"); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UPDATE mahasiswa | |
SET jurusan = 'IF' | |
WHERE nim = '10511148'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment