Posted by: Bayu Sulistyanto Untoro | April 24, 2010

Source Code Membuat Koneksi Basisdata MySQL di Java

// Deklarasi package
package engine.code;

// import kelas yg dibutuhkan
import java.sql.*;
import javax.swing.JOptionPane;

// Deklarasi Class
public class Database {
// Deklarasi variable global
private Connection connectiondb;
private Statement statement;

public Database() {
}

//***************************************************************************//
/**
* Method tryConnectMySQL
* Keterangan : Untuk mengkoneksikan MySQL
* Nilai kembalian Connection
*/
// awal method tryConnectMySQL
public Connection tryConnectMySQL(){
try {
Class.forName(“org.gjt.mm.mysql.Driver”);
} catch(ClassNotFoundException ikierrore) {
JOptionPane.showMessageDialog(null,”Error Loading Driver: “+ikierrore);
}
try{
String urldb=”jdbc:mysql://localhost:3306/nama_basisdata”;
connectiondb = DriverManager.getConnection(urldb,”root”,””);
} catch(Exception e){
JOptionPane.showMessageDialog(null,”Error Database: Tidak dapat konek ke Basis data”+e);
}
return connectiondb;
}

//***************************************************************************//
/**
* Method closeConnection
* keterangan : Untuk menutup koneksi
**/
public Connection closeConnection(){
try{
connectiondb.close();
}catch(Exception e){
System.out.println(e);
}
return connectiondb;
}

//***************************************************************************//
/**
* Method setKoneksi
* keterangan : Untuk mengeset koneksi
**/
public void setKoneksi(Connection connectiondb) {
this.connectiondb = connectiondb;
}

//****************************************************************************//
/**
* Method exeQuery
* keterangan : Menjalankan Query yg tidak menampilkan hasil
* Nilai kembalian : int
**/
public int exeQuery(String sql){
try{
connectiondb=this.tryConnectMySQL();
statement=connectiondb.createStatement();
int success = statement.executeUpdate(sql);
statement.close();
connectiondb.close();
return success;
} catch(Exception e){
return 0;
}
}   // akhir method exeQuery

//****************************************************************************//
/**
* Method selectData
* keterangan : Mengambil data dari basdat
* nilai kembalian: resultset
**/
public ResultSet selectData(String selectStatement){
try{
connectiondb=this.tryConnectMySQL();
Statement statements = connectiondb.createStatement();
ResultSet resultSet = statements .executeQuery(selectStatement);
int maxCol=1;
while(resultSet.next()){
maxCol++;
}
return resultSet;
} catch(Exception e){
return null;
}
}   //akhir method selectData

}   // akhir class Database

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: