How To Use Object Oriented Database Encapsulation in Java Programming Language
The Following script is the simplest way to declaring and using Object Oriented Database Encapsulation in Java programming language:
Class diagram:
Java program:
//Mahasiswa = Student
//NIM = Student#
//Nama = Name
//Alamat = Address
import java.sql.*;
import java.io.*;
class OODBMahasiswa{
void list(){
System.out.println("List data : ");
try {
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/akademik","root","");
Statement statement = connection.createStatement();
String sql="select * from mahasiswa";
ResultSet rs=statement.executeQuery(sql);
System.out.println("NIM\tNama\t\tAlamat");
System.out.println("---------------------------------------------------------------");
while(rs.next()){
System.out.println(rs.getString(1) +"\t"+ rs.getString(2) +"\t\t"+ rs.getString(3));
}
statement.close();
connection.close();
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
void create(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String nim,nama,alamat;
System.out.println("Create data : ");
try {
System.out.print("nim : ");
nim = stdin.readLine();
System.out.print("nama : ");
nama = stdin.readLine();
System.out.print("alamat : ");
alamat = stdin.readLine();
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/akademik","root","");
Statement statement = connection.createStatement();
String sql = "insert into mahasiswa values ('"+ nim +"','"+ nama +"','"+ alamat +"');";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data tersimpan"); //the data has been stored
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
void retreive(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String nim,nama,alamat;
System.out.println("Lihat data : "); //view data
try {
System.out.print("nim : ");
nim = stdin.readLine();
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/akademik","root","");
Statement statement = connection.createStatement();
String sql="select * from mahasiswa where nim like '"+ nim +"'";
ResultSet rs=statement.executeQuery(sql);
if(rs.next()){
System.out.println("nama : "+rs.getString(2));
System.out.println("alamat : "+rs.getString(3));
}else{
System.out.println("nim : "+nim+" tidak terdaftar"); //student# not enrolled
}
statement.close();
connection.close();
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
void update(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String nim,nama,alamat,pilih;
System.out.println("Edit data : ");
try {
System.out.print("nim : ");
nim = stdin.readLine();
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/akademik","root","");
Statement statement = connection.createStatement();
String sql="select * from mahasiswa where nim like '"+nim+"'";
ResultSet rs=statement.executeQuery(sql);
if(rs.next()){
System.out.println("nama : "+rs.getString(2));
System.out.println("alamat : "+rs.getString(3));
}else{
System.out.println("nim : "+ nim +"tidak terdaftar"); //student not enrolled
System.exit(0);
}
System.out.print("anda mau mengedit data ini (y/t)? ");
//Do you want to update this data (y/t)?
pilih = stdin.readLine();
if(pilih.equalsIgnoreCase("t"))
System.exit(1);
else{
System.out.print("nama : ");
nama = stdin.readLine();
System.out.print("alamat : ");
alamat = stdin.readLine();
sql = "update mahasiswa set nama='"+nama+"',alamat='"+alamat+"' where nim='"+ nim +"'";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data teredit"); //data has been updated
}
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
void delete(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String nim,pilih;
System.out.println("Hapus data : "); //delete data
try {
System.out.print("nim : ");
nim = stdin.readLine();
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/akademik","root","");
Statement statement = connection.createStatement();
String sql="select * from mahasiswa where nim like '"+nim+"'";
ResultSet rs=statement.executeQuery(sql);
if(rs.next()){
System.out.println("nama : "+rs.getString(2));
System.out.println("alamat : "+rs.getString(3));
}else{
System.out.println("nim : "+nim+"tidak terdaftar");
System.exit(0);
}
System.out.print("anda mau menghapus data ini (y/t)? ");
//Do you want to delete this data (y/t)?
pilih = stdin.readLine();
if(pilih.equalsIgnoreCase("t"))
System.exit(1);
else{
sql = "delete from mahasiswa where nim='"+nim+"'";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data terhapus"); //data has been deleted
}
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
}
class OODBEncapsulation{
public static void main(String args[]){
OODBMahasiswa Mhs = new OODBMahasiswa();
Mhs.list();
}
}
Output:
ShadowOfBdg© - All rights reserved.
No comments:
Post a Comment