Friday, April 12, 2013

Membuat Koneksi Database MySQL Menggunakan Netbeans


Sebelum Mengkoneksikan Mysql and Netbeans terlebih dahulu import mysq konektor untuk java.. untuk mendapatkan Mysql conektor for jav a search aja di om google.. hehehehe.. tapi kalau teman-teman menggunakan netbeans Mysql jdbc (java database conection) nya itu sudah ada, tinggal masukkan jdbc-nya mudahkan... buka jendelah projeck.. lalu click kanan pada libraries ... pilih add library.. kemudian cari MYSQL JDBC Driver... add Library... setelah ketik source dibawah ini.. pada class yg telah anda buat..






package penjualan.koneksi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
*
* @author Fendi Wally
*/
public class db_koneksi {
private static Connection connection;
public Properties mypanel, myLanguage;
private String strNamaPanel;
public static Connection getConnection(){
if(connection == null){
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","fendi");
//localhost merupakan nama host yg kita gunakan..
//3306 : port nya .. krn saya menggunakan Xamp;
//test : nama databasenya, root : admin nya, fendi : password rootnya..
} catch (SQLException ex) {
JOptionPane.showConfirmDialog(null, "Maaf, koneksi gagal dilakukan.","Kesalahan koneksi",JOptionPane.CLOSED_OPTION);
}
}
return connection;
}
}






Semoga Bermanfaat  (^_^)

No comments:

Post a Comment