How To Use Object Oriented Database Polymorphism in Java Programming Language
The Following script is the simplest way to declaring and using Object Oriented Database Polymorphism in Java programming language:
Class Design:
Java Program:
import java.sql.*;
import java.io.*;
abstract 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;
}
abstract void list();
abstract void create();
abstract void retreive();
abstract void update();
abstract void delete();
}
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("Tabel : "+ tabel);
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");
} 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 : ");
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");
}
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(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");
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 "+ tabel +" 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);
}
}
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(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");
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 "+ tabel +" where nim='"+nim+"'";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data terhapus");
}
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
}
class OODBMataKuliah extends OODBTabel{
void init(){
setDriver("org.gjt.mm.mysql.Driver");
setDatabase("jdbc:mysql://localhost/akademik");
setUsername("root");
setPassword("");
setTabel("matakuliah");
}
void list(){
System.out.println("Tabel : "+ tabel);
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("Kode\tMata Kuliah\t\t\tSKS");
System.out.println("--------------------------------------------------------");
while(rs.next()){
System.out.println(rs.getString(1) +"\t"+ rs.getString(2) +"\t\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 kode,matakuliah,sks;
System.out.println("Create data : ");
try {
System.out.print("Kode : ");
kode = stdin.readLine();
System.out.print("Mata Kuliah : ");
matakuliah = stdin.readLine();
System.out.print("SKS : ");
sks = stdin.readLine();
Class.forName(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql = "insert into "+ tabel +" values ('"+ kode +"','"+ matakuliah +"','"+ sks +"');";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data tersimpan");
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
void retreive(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String kode,matakuliah,sks;
System.out.println("Lihat data : ");
try {
System.out.print("Kode : ");
kode = stdin.readLine();
Class.forName(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql="select * from "+ tabel +" where Kode like '"+ kode +"'";
ResultSet rs=statement.executeQuery(sql);
if(rs.next()){
System.out.println("Mata Kuliah : "+rs.getString(2));
System.out.println("SKS : "+rs.getString(3));
}else{
System.out.println("Kode : "+ kode +" tidak terdaftar");
}
statement.close();
connection.close();
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
void update(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String kode,matakuliah,sks,pilih;
System.out.println("Edit data : ");
try {
System.out.print("Kode : ");
kode = stdin.readLine();
Class.forName(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql="select * from "+ tabel +" where Kode like '"+ kode +"'";
ResultSet rs=statement.executeQuery(sql);
if(rs.next()){
System.out.println("Mata Kuliah : "+rs.getString(2));
System.out.println("SKS : "+rs.getString(3));
}else{
System.out.println("Kode : "+ kode +"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("Mata Kuliah : ");
matakuliah = stdin.readLine();
System.out.print("SKS : ");
sks = stdin.readLine();
sql = "update "+ tabel +" set MataKuliah='"+ matakuliah +"',SKS='"+ sks +"' where Kode='"+ kode +"'";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data teredit");
}
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
void delete(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String kode,pilih;
System.out.println("Hapus data : ");
try {
System.out.print("Kode : ");
kode = stdin.readLine();
Class.forName(driver);
Connection connection = DriverManager.getConnection(database,username,password);
Statement statement = connection.createStatement();
String sql="select * from "+ tabel +" where Kode like '"+ kode +"'";
ResultSet rs=statement.executeQuery(sql);
if(rs.next()){
System.out.println("Mata Kuliah : "+rs.getString(2));
System.out.println("SKS : "+rs.getString(3));
}else{
System.out.println("Kode : "+ kode +"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 "+ tabel +" where Kode='"+ kode +"'";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Data terhapus");
}
} catch (Exception e) {
System.out.println("Error : "+e);
}
}
}
class OODBPolymorphism{
public static void main(String args[]){
OODBMahasiswa Mhs = new OODBMahasiswa();
Mhs.init();
Mhs.list();
System.out.println("\n");
OODBMataKuliah MK = new OODBMataKuliah();
MK.init();
MK.list();
}
}
Output:
ShadowOfBdg© - All rights reserved.
No comments:
Post a Comment