How To Use Object Oriented Database Interface in Java Programming Language
The Following script is the simplest way to declaring and using Object Oriented Database Interface 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;
}
}
interface Menu{
void menu();
}
class OODBMahasiswa extends OODBTabel implements Menu{
int loop = 1;
OODBMahasiswa(){
setDriver("org.gjt.mm.mysql.Driver");
setDatabase("jdbc:mysql://localhost/akademik");
setUsername("root");
setPassword("");
setTabel("mahasiswa");
}
OODBMahasiswa(String driver, String database, String username, String password, String tabel){
setDriver(driver);
setDatabase(database);
setUsername(username);
setPassword(password);
setTabel(tabel);
}
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);
}
}
public void menu(){
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
int pilih;
while(loop == 1){
try{
System.out.println("------------------------------");
System.out.println("Menu");
System.out.println("------------------------------");
System.out.println("1. List");
System.out.println("2. Create");
System.out.println("3. Retreive");
System.out.println("4. Update");
System.out.println("5. Delete");
System.out.println("0. Exit");
System.out.println("------------------------------");
System.out.print("Pilih Menu [0..5]:");
pilih = Integer.parseInt(stdin.readLine());
if (pilih == 1) {list(); loop = 1;}
if (pilih == 2) {create(); loop = 1;}
if (pilih == 3) {retreive(); loop = 1;}
if (pilih == 4) {update(); loop = 1;}
if (pilih == 5) {delete(); loop = 1;}
if (pilih == 0) loop = 0;
System.out.println("\n\n");
} catch(Exception e){
System.out.println("Error : "+ e);
}
}
}
}
class OODBInterface{
public static void main(String args[]){
OODBMahasiswa Mhs = new OODBMahasiswa();
Mhs.menu();
}
}
Output:
ShadowOfBdg© - All rights reserved.
No comments:
Post a Comment