Sie sind auf Seite 1von 2

private Connection konek = null;

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;
}

//EKSEKUSI QUERY SELECT


public ResultSet executeSelect(String query) throws SQLException,IllegalStat
eException{
Statement stm;
ResultSet rs = null;
if(konek==null)
return null;
stm = (Statement) konek.createStatement(ResultSet.TYPE_SCROLL_SENSITIV
E,
ResultSet.CONCUR_READ_ONLY);
rs = stm.executeQuery(query);
return rs;
}
//EKSEKUSI QUERY UPDATE
public int executeUpdate(String query)
throws SQLException,IllegalStateException{
Statement stmt;
if(konek==null)
return 0;
stmt = (Statement) konek.createStatement();
return stmt.executeUpdate(query);
}
public PreparedStatement persiapkanStatement(String query)
throws SQLException,IllegalStateException{
PreparedStatement pstmt = (PreparedStatement) konek.prepareStatement(que
ry);
return pstmt;
}

Das könnte Ihnen auch gefallen