Thursday, October 15, 2015

How To Use Object Oriented Database Access Specifier in Java Programming Language

How To Use Object Oriented Database Access Specifier in Java Programming Language

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

Class diagram:


Java program:

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

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

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

 protected String getDriver(){
  return driver;
 }

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

 protected String getDatabase(){
  return database;
 }

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

 protected String getUsername(){
  return username;
 }

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

 protected String getPassword(){
  return password;
 }

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

 protected String getTabel(){
  return tabel;
 } 
}

class OODBMahasiswa extends OODBTabel{  
 public void init(){
setDriver("org.gjt.mm.mysql.Driver");
setDatabase("jdbc:mysql://localhost/akademik");
setUsername("root");
setPassword("");
setTabel("mahasiswa");
 } 
 public void list(){
System.out.println("Tabel : "+ getTabel());
try {
Class.forName(getDriver());
Connection connection = DriverManager.getConnection(getDatabase(),getUsername(),getPassword());
Statement statement = connection.createStatement();
String sql="select * from "+ getTabel();
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);
}
 }

 public 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(getDriver());
Connection connection = DriverManager.getConnection(getDatabase(),getUsername(),getPassword());
Statement statement = connection.createStatement();
String sql = "insert into "+ getTabel() +" values ('"+ nim +"','"+ nama +"','"+ alamat +"');";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data tersimpan");
} catch (Exception e) {
System.out.println("Error : "+e);
}  
 }

 public void retreive(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String nim,nama,alamat;
System.out.println("Lihat data : ");
try {
System.out.print("nim : ");
nim = stdin.readLine();
Class.forName(getDriver());
Connection connection = DriverManager.getConnection(getDatabase(),getUsername(),getPassword());
Statement statement = connection.createStatement();
String sql="select * from "+ getTabel() +" 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");
}
statement.close();
connection.close();
} catch (Exception e) {
System.out.println("Error : "+e);
}
 }

 public 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(getDriver());
Connection connection = DriverManager.getConnection(getDatabase(),getUsername(),getPassword());
Statement statement = connection.createStatement();
String sql="select * from "+ getTabel() +" 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 mengedit data ini (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 "+ getTabel() +" set nama='"+nama+"',alamat='"+alamat+"' where nim='"+ nim +"'";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data teredit");
}
} catch (Exception e) {
System.out.println("Error : "+e);
}
 }

 public void delete(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String nim,pilih;
System.out.println("Hapus data : ");
try {
System.out.print("nim : ");
nim = stdin.readLine();
Class.forName(getDriver());
Connection connection = DriverManager.getConnection(getDatabase(),getUsername(),getPassword());
Statement statement = connection.createStatement();
String sql="select * from "+ getTabel() +" 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)? ");
pilih = stdin.readLine();
if(pilih.equalsIgnoreCase("t"))
System.exit(1);
else{
sql = "delete from "+ getTabel() +" where nim='"+nim+"'";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data terhapus");
}
} catch (Exception e) {
System.out.println("Error : "+e);
}
 }
}

class OODBAccessSpecifier{
 public static void main(String args[]){  
  OODBMahasiswa Mhs = new OODBMahasiswa();
  Mhs.init();
  Mhs.list();  
 }
}

Output:

you can see the tutorial video here:


ShadowOfBdg© - All rights reserved. 

No comments:

Post a Comment