Thursday, October 15, 2015

How To Use Object Oriented Database Inheritance in Java Programming Language

How To Use Object Oriented Database Inheritance in Java Programming Language

The Following script is the simplest way to declaring and using Object Oriented Database Inheritance in Java programming language:

Class diagram:


Java program:

//Mahasiswa = student
//NIM = Student#
//Nama = Name
//Alamat = Address

import java.sql.*;
import java.io.*;

class OODBTabel{
 String driver;
 String database;
 String username;
 String password;
 String tabel;

 void setDriver(String driver){
  this.driver = driver;
 }

 void setDatabase(String database){
  this.database = database;
 }

 void setUsername(String username){
  this.username = username;
 }

 void setPassword(String password){
  this.password = password;
 }

 void setTabel(String tabel){
  this.tabel = tabel;
 }
}

class OODBMahasiswa extends OODBTabel{  
 void init(){
setDriver("org.gjt.mm.mysql.Driver");
setDatabase("jdbc:mysql://localhost/akademik");
setUsername("root");
setPassword("");
setTabel("mahasiswa");
 } 
 void list(){
System.out.println("List data : ");
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql="select * from "+ tabel;
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(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql = "insert into "+ tabel +" values ('"+ nim +"','"+ nama +"','"+ alamat +"');";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data tersimpan"); //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(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql="select * from "+ tabel +" 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 : "); //update data
try {
System.out.print("nim : ");
nim = stdin.readLine();
Class.forName(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql="select * from "+ tabel +" 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 "+ tabel +" 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(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql="select * from "+ tabel +" 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 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 "+ tabel +" 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 OODBInheritance{
 public static void main(String args[]){  
  OODBMahasiswa Mhs = new OODBMahasiswa();
  Mhs.init();
  Mhs.list();  
 }
}

Output:


ShadowOfBdg© - All rights reserved. 

No comments:

Post a Comment