Beruflich Dokumente
Kultur Dokumente
public koneksi(){
konek = null;
}
//KONEKSI KE DATBASE DENGAN PARAMETER
public void konekParam() throws SQLException{
String server = "localhost", user="root", pass="", db="tokodb";
if(isKonek()){
System.out.println("Konek");
}
else KoneksiDB(server, user, pass, db);
if(isKonek()){
System.out.println("Konek");
}else{
System.out.println("Gak Konek");
}
}
//KONEKSI DATABASE MENGGUNAKAN dbDriver MySQL
public boolean KoneksiDB(String server,
String user,
String pass,
String db) throws SQLException, IllegalStateExcepti
on{
String dbDriver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://" + server + "/" + db;
this.tutupKoneksi();
try{
Class.forName(dbDriver);
konek = DriverManager.getConnection(dbURL, user, pass);
return true;
}catch(ClassNotFoundException ex){
}catch(SQLException ex){
}
return false;
}
//TUTUP KONEKSI KE DATABASE
public void tutupKoneksi() throws SQLException, IllegalStateException {
if(konek != null){
konek.close();
konek = null;
}
}
//CEK APAKAH KONEK
public boolean isKonek(){
if(konek != null){
return true;
}
return false;
}