Bab 15 - Allegra Fernanda Santoso - 215150201111066

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

PRAKTIKUM SISTEM BASIS DATA

BAB : DASAR PEMROGRAMAN DATABASE


NAMA : ALLEGRA FERNANDA SANTOSO
NIM : 215150201111066
ASISTEN : BRAHMA HANIF FARHANSYAH
THORIQ ACHMAD RAMADHAN
TGL : 02/12/2022 TGL PENGUMPULAN :11/12/2022
PRAKTIKUM

Pertanyaan
1. Soal Buatlah dua tabel berikut :

Implementasi
CREATE DATABASE TestDB
use TestDB
CREATE TABLE Mahasiswa(
NIM varchar(20),
Nama varchar(20),
PRIMARY KEY (NIM)
)

CREATE TABLE Nilai(


NIM varchar(20),
Nilai float,
FOREIGN KEY(NIM) references Mahasiswa(NIM)
)

INSERT INTO Mahasiswa VALUES ('21515','Allegra')

INSERT INTO Mahasiswa VALUES ('0201111','Fernanda')

INSERT INTO Mahasiswa VALUES ('066','Santoso')

INSERT INTO Nilai VALUES ('21515',40)


INSERT INTO Nilai VALUES ('0201111', 58)
INSERT INTO Nilai VALUES ('066',98)

SELECT * FROM Mahasiswa m JOIN Nilai n ON m.NIM = n.NIM

Tampilan Keluaran
Pembahasan dan Analisis
Pembuatan database testdb dengan data yang tertera

Pertanyaan
2. Tampilkan tabel menggunakan program java

Implementasi
1./*1
*2 Click
nbfs://nbhost/SystemFileSystem/Templates/Licenses/licen
1
se-default.txt to change this license
* Click
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.
java to edit this template
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Allegra
*/
public class Sbd1 {
static String url =
"jdbc:jtds:sqlserver://localhost:1443/TestDB";

public static void main(String[] args) {


Connection conn = null;
Sbd1 sbd1 = new Sbd1();
try {
conn = sbd1.getConnection(url);
System.out.println("Before Insert");
sbd1.statementQuery(conn);
sbd1.statementInsert(conn);
System.out.println("After Insert");
sbd1.statementQuery(conn);
sbd1.preparedStatementInsert(conn);
System.out.println("After Insert with
preparedstatement");
sbd1.statementQuery(conn);
sbd1.statementDelete(conn);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public Connection getConnection(String url)
throws SQLException, ClassNotFoundException {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(url);
return conn;
}
public void statementQuery(Connection conn) throws
SQLException {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from
Mahasiswa");

System.out.println("===================================
=======");
System.out.println("| ID | NAMA | JUMLAH");

System.out.println("===================================
=======");
while (rs.next()) {
String NIM = rs.getString(1);
String nama = rs.getString(2);
//Integer jml = rs.getInt(3);
System.out.println("| " + NIM + " | " +
nama + " | ");
}
}
public void statementInsert(Connection conn) throws
SQLException {
//. . . . .
}
public void preparedStatementInsert(Connection
conn) throws SQLException {
//. . . . . .
}
public void statementDelete(Connection conn) throws
SQLException {
Statement st = conn.createStatement();
st.executeUpdate("delete from Inventory where
id=3");
st.executeUpdate("delete from Inventory where
id=4");
st.executeUpdate("delete from Inventory where
id=5");
}
}

Tampilan Keluaran
Pembahasan dan Analisis
Penampilan tabel menggunakan program java dari netbeans.

You might also like