Software Tutorial, - 5 Januari 2010
Akhirnya walaupun ketinggalan zaman,saya bisa melakukan koneksi dari Java ke MySQL as simple as this….
package tubesoot;
//add mysql connector di library pada package anda
import java.sql.*;
import javax.swing.JOptionPane;
/**
*
* @author Juanitaoke
*/
public class koneksi {
//inisialisasi statement pertamanya kosong selalu
private Statement stmt = null;
private Connection con = null;
private ResultSet rs = null;
public koneksi(){
try{
//ini mah wajib harus ada..
Class.forName(“com.mysql.jdbc.Driver”);
}
catch (Exception e){
JOptionPane.showMessageDialog(null,” “+e.getMessage(),”JDBC Driver Error”,JOptionPane.WARNING_MESSAGE);
}
try{
//mulai masuk ke database kitahhh
con = DriverManager.getConnection(“jdbc:mysql://localhost/akademik”,”root”,”");
stmt = con.createStatement();
}
catch (Exception e){
JOptionPane.showMessageDialog(null,” “+e.getMessage(),”JDBC Driver Error”,JOptionPane.WARNING_MESSAGE);
}
}
//yang ini buat nampung data
public ResultSet getData(String SQLString){
try{
rs = stmt.executeQuery(SQLString);
}
catch (Exception e){
JOptionPane.showMessageDialog(null,”Error: “+e.getMessage(),”Communication Error”,JOptionPane.WARNING_MESSAGE);
}
return rs;
}
//yang ini buat eksekusi command a.k.a perintah a.k.a query
public void query(String SQLString){
try{
stmt.executeUpdate(SQLString);
}
catch (Exception e){
JOptionPane.showMessageDialog(null,”Error: “+e.getMessage(),”Communication Error”,JOptionPane.WARNING_MESSAGE);
}
}
}
Senin, 04 Januari 2010
Langganan:
Posting Komentar (Atom)
bgus...sob hampir lngkap......prthankan kyk gini klo prlu....lo tmbahin lg tntang ilmu komputer
BalasHapus