Setalah sekian lama (ga sampai 10 tahun;) browsing cara koneksi database yang berbeda ( mysql dan postgres) akhirnya kami menemukannya. Dan langsung di coba, buat class koneksi dan class backup. Class koneksi untuk menghubungkan ke dua database/server berbeda, Class backup untuk memindahkan isi tabel di mysql ke postgres. Jangan lupa library kedua database harus pas dan cucok (lihat gambar dibawah ini). Kalau tidak sesuai, dijamin program tidak jalan.
Baca juga :
Java di windows-7 instal compile dan run
Semoga bermanfaat bagi antum semuanya.
1. buat class misal koneksi.java di package lib.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lib;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author set
*/
public class koneksi {
private String url, user, pass, cls, pesan = null;
public koneksi(String ip, String port, String db, String users, String passw, String database) {
//ip db, por db, nama db, user db, password db, jenis db
this.user = users;
this.pass = passw;
if(database.equalsIgnoreCase("postgres")) {
this.url = "jdbc:postgresql://"+ip+":"+port+"/"+db;
this.cls = "org.postgresql.Driver";
}
else if(database.equalsIgnoreCase("mysql")) {
this.cls = "com.mysql.jdbc.Driver";
if(port==null){
this.url = "jdbc:mysql://"+ip+"/"+db;
}
else {
this.url = "jdbc:mysql://"+ip+":"+port+"/"+db;
}
}
}
//Khusus Ambil Koneksi
public Connection getKoneksi() {
pesan = null;
Connection koneksi = null;
try {
Class.forName(cls);
koneksi = DriverManager.getConnection(url, user, pass);
}
catch(SQLException se) {
pesan = "SQL Exception : "+se.getMessage();
}
catch(ClassNotFoundException cn) {
pesan = "Class Exception : "+cn.getMessage();
}
return koneksi;
}
//Khusus Menangani Perintah Select
public String[][] TampilkanData(String sQry) {
pesan = null;
String[][] mymodel = new String[0][0];
try {
int i = 0;
int maxcol;
Connection con = getKoneksi();
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery(sQry);
if (rs.last()) i = rs.getRow();
maxcol = rs.getMetaData().getColumnCount();
mymodel = new String[i][maxcol];
rs.beforeFirst();
i=0;
while (rs.next()) {
for (int j = 0; j < maxcol;j++) {
try {
if (rs.getObject(j+1) != null) {
mymodel[i][j] = rs.getString(j+1);
}
} catch (ArrayStoreException ae) { mymodel[i][j] = null; }
}
i++;
}
rs.close();
st.close();
con.close();
}
catch(SQLException se) {
pesan = "SQL Exception : "+se.getMessage();
}
catch(Exception cn) {
pesan = "Exception : "+cn.getMessage();
}
return mymodel;
}
//Khusus Menangani Proses Insert, Update, Delete
public void CRUD(String sQry){
pesan = null;
try {
Connection con = getKoneksi();
java.sql.Statement st = con.createStatement();
st.executeUpdate(sQry);
st.close();
con.close();
}
catch(SQLException se) {
pesan = "SQL Exception : "+se.getMessage();
}
catch(Exception cn) {
pesan = "Exception : "+cn.getMessage();
}
}
//Ambil Value Error
public String getPesan() {
return pesan;
}
}
2. Buat class misal backup.java di package Tes.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import lib.koneksi;
/**
*
* @author set
*/
public class backup {
public static void main(String[] d2c) {
koneksi k1 = new koneksi("localhost", "3306", "coba_db1", "root", "", "mysql");
koneksi k2 = new koneksi("localhost", "5432", "coba_db2", "postgres", "123", "postgres");
//ip db, por db, nama db, user db, password db, jenis db
try {
String tabel1 = "crud1";
String tabel2 = "crud2";
//ambil data dari MySQL
String data[][] = k1.TampilkanData("select * from "+tabel1);
if(k1.getPesan() == null) {
for (int i = 0; i < data.length; i++) {
String a = "insert into "+tabel2+" values (";
for (int j = 0; j < data[i].length; j++) {
a = a+"'"+data[i][j]+"',";
}
a = a.substring(0, a.lastIndexOf(","))+")";
//insert data ke database PostgreSQL
k2.CRUD(a);
if(k2.getPesan() == null) {
System.out.println("Insert data ke-"+(i+1)+" Sukses…");
}
else {
System.out.println("Kesalahan (PostgreSQL) Pada : "+k2.getPesan());
}
}
}
else {
System.out.println("Kesalahan (MySQL) Pada : "+k1.getPesan());
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Thx Source : https://d2csoft.wordpress.com/2012/11/22/multi-koneksi-ke-dalam-database-yang-berbeda-beda/
Tidak ada komentar:
Posting Komentar