Membuat file konfigurasi mysql di java dengan load file .ini
bagi anda yang biasa menggunakan koneksi dengan file maka kita akan mendapatkan sebuah masalah pada file konfigurasi mysql yaitu file kofigurasi akan langsung di include kan langsung pada koding kita maka jika source kode sudah di konvert ke exe maka kita tidak dapat menganti file konfigurasi kita....
tara kita punya solusi nya yaiti dengan membuat file konfigurasi .ini yang kita letakan di luar aplikasi kita kemudian kita panggil kedalam aplikasi kita...
ok cukup penjelasan nya langsung ke tkp aja
1. kita buat file database.ini yang kita letakan di luar folder aplikasi kita..
isinya
#setting JDBC2. mari kita load file database.ini kedalam aplikasi kita
#getDriver JDBC
DBDriver=com.mysql.jdbc.Driver
#getDatabase
DBDatabase=jdbc:mysql://localhost/pajak
#setting username
DBUsername=root
#setting password
DBPassword= root
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lib;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.swing.JOptionPane;
/**
*
* @author abuyusuf
*/
public class db {
private String driver, database, user, pass, host, port;
public Properties mypanel, myLanguage;
private String strNamePanel;
Connection koneksi;
Statement st;
String Sql;
public String SettingPanel(String nmPanel) {
try {
mypanel = new Properties();
mypanel.load(new FileInputStream("database.ini"));
strNamePanel = mypanel.getProperty(nmPanel);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Tidak Bisa Koneksi", "Error", JOptionPane.INFORMATION_MESSAGE);
System.err.println(e.getMessage());
System.exit(0);
}
return strNamePanel;
}
public Connection connect() {
driver = SettingPanel("DBDriver");
database = SettingPanel("DBDatabase");
user = SettingPanel("DBUsername");
pass = SettingPanel("DBPassword");
port = SettingPanel("port");
host = SettingPanel("host");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
System.out.println("Driver tidak di temukan");
}
try {
String url = "jdbc:mysql://" + host + ":" + port + "/" + database;
koneksi = DriverManager.getConnection(url, user, pass);
System.out.println("Berhasil Koneksi");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
System.out.println("Gagal koneksi");
}
return koneksi;
}
public ResultSet basicQuery(String sql) {
ResultSet res = null;
try {
st = koneksi.createStatement();
res = st.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, e.getMessage());
}
//System.out.println(sql);
return res;
}
public String basicUpdate(String sql) {
String result = "";
try {
st = koneksi.createStatement();
st.executeUpdate(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, e.getMessage());
}
System.out.println(sql);
return result;
}
public ResultSet selectAll(String namaTabel) {
Sql = "SELECT * FROM " + namaTabel;
System.out.println(Sql);
return this.basicQuery(Sql);
}
public ResultSet selectAll(String namaTabel, String kondisi) {
Sql = "SELECT * FROM " + namaTabel + " WHERE " + kondisi;
System.out.println(Sql);
return this.basicQuery(Sql);
}
public ResultSet querySelect(String[] namaKolom, String namaTabel) {
int i;
Sql = "SELECT ";
for (i = 0; i <= namaKolom.length - 1; i++) {
Sql += namaKolom[i];
if (i < namaKolom.length - 1) {
Sql += ", ";
}
}
Sql += " FROM " + namaTabel;
System.out.println(Sql);
return this.basicQuery(Sql);
}
public ResultSet querySelect(String[] namaKolom, String namaTabel, String kondisi) {
int i;
Sql = "SELECT ";
for (i = 0; i <= namaKolom.length - 1; i++) {
Sql += namaKolom[i];
if (i < namaKolom.length - 1) {
Sql += ", ";
}
}
Sql += " FROM " + namaTabel + " WHERE " + kondisi;
System.out.println(Sql);
return this.basicQuery(Sql);
}
//query insert
public String queryInsert(String namaTabel, String[] isiTabel) {
int i;
Sql = "INSERT INTO " + namaTabel + " VALUES(";
for (i = 0; i <= isiTabel.length - 1; i++) {
Sql += isiTabel[i];
if (i < isiTabel.length - 1) {
Sql += ", ";
}
}
Sql += ")";
System.out.println(Sql);
return this.basicUpdate(Sql);
}
public String queryInsert(String namaTabel, String[] namaKolom, String[] isiTabel) {
int i;
Sql = "INSERT INTO " + namaTabel + "(";
for (i = 0; i <= namaKolom.length - 1; i++) {
Sql += namaKolom[i];
if (i < namaKolom.length - 1) {
Sql += ", ";
}
}
Sql += ") VALUES(";
for (i = 0; i <= isiTabel.length - 1; i++) {
Sql += isiTabel[i];
if (i < isiTabel.length - 1) {
Sql += ", ";
}
}
Sql += ")";
System.out.println(Sql);
return this.basicUpdate(Sql);
}
//query for update
public String queryUpdate(String namaTabel, String[] namaKolom, String[] isiTabel, String kondisi) {
int i;
Sql = "UPDATE " + namaTabel + " SET ";
for (i = 0; i <= namaKolom.length - 1; i++) {
Sql += namaKolom[i] + " = " + isiTabel[i];
if (i < namaKolom.length - 1) {
Sql += ", ";
}
}
Sql += " WHERE " + kondisi;
System.out.println(Sql);
return this.basicUpdate(Sql);
}
public String queryDelete(String namaTabel, String kondisi) {
Sql = "DELETE FROM " + namaTabel + " WHERE " + kondisi;
System.out.println(Sql);
return this.basicUpdate(Sql);
}
}
perhatikan baris yang berwarna merah di atas baris diatas adalah fungsi untuk mengambil parameter database dan yang lainnya kemudian akan di masuk kan kedalam variabel masing-masing dengan memanggil fungsi Settingpanel();
ok sampai disini dulu ya ... kalo ada masalah silakan coret2 dibawah
jangan lupa tinggal kan jejak