package logicClass;
import classtoko.ComboItem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
/**
*
* @author a3m-nix
*/
public class JMySQL {
private static Connection koneksi;
private Statement stat;
private ResultSet rs;
private String query;
private int row;
boolean status;
String querySave;
String queryUpdate;
String queryView;
String table;
String field;
public static String getHostname() {
return setProgram.getValue("hostname");
}
private static String getUsername() {
return setProgram.getValue("user");
}
private static String getPassword() {
return setProgram.getValue("password");
}
public static String getDbName() {
return setProgram.getValue("dbname");
}
public static Connection getKoneksi(){
String hostDbDriver = "jdbc:mysql://"+getHostname()+"/"+getDbName();
try {
Class.forName("com.mysql.jdbc.Driver");
koneksi = DriverManager.getConnection(hostDbDriver,getUsername(),getPassword());
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Tidak terkoneksi ke database, error konstruktor : "+e);
koneksi = null;
}
return koneksi;
}
public static void main(String[] args) throws SQLException{
}
public static void closeKoneksi(Connection con, Statement st,ResultSet rs){
try {
if(con != null){
con.close();
}
if(st != null){
st.close();
}
if(rs != null){
rs.close();
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Close database error "+ex);
}
}
}
Karena method yang digunakan adalah method static maka untuk menggunakan class ini contohnya :
Connection cn = JMySQL.getKoneksi();
Statement st = null;
ResultSet rs = null;
try {
st = cn.createStatement();
st.executeUpdate("Disini query SQL");
} catch (SQLException ex) {
JOptionPane.showConfirmDialog(null, ex);
}
JMySQL.closeKoneksi(cn, st, rs);
Tidak ada komentar:
Posting Komentar