Pages

Subscribe:

Ads 468x60px

♪ ♥ Get Paid Daily - The Best Sites PTC ♥ ♪
DonkeyMails.com: No Minimum Payout
AyuWage Services - Get Paid to Visits Sites and Complete Surveys

Minggu, 15 Juli 2012

Akses Database Menggunakan Java

Akses Database Menggunakan Java

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
public class sisip implements ActionListener {
JLabel labelNama = new JLabel(“nama:”);
JTextField fieldNama = new JTextField(10);
JLabel labelNim = new JLabel(“nim:”);
JTextField fieldNim = new JTextField(10);
JLabel labelProdi = new JLabel(“prodi:”);
JTextField fieldProdi = new JTextField(10);
JLabel labelAlamat = new JLabel(“alamat:”);
JTextField fieldAlamat = new JTextField(10);
JLabel labelKota = new JLabel(“kota:”);
JTextField fieldKota = new JTextField(10);
JButton tombolSimpan = new JButton(“simpan”);
JButton tombolBatal = new JButton(“batal”);
JPanel panelSemua = new JPanel();
JPanel panelAtas = new JPanel();
JPanel panelCenter = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JTable tabel = new JTable();
DefaultTableModel tabMode;
Connection koneksi;
sisip(){
BorderLayout bl = new BorderLayout();
panelSemua.setLayout(bl);
GridLayout gl1 = new GridLayout(6,2);
panelAtas.setLayout(gl1);
panelAtas.add(labelNim);
panelAtas.add(fieldNim);
panelAtas.add(labelNama);
panelAtas.add(fieldNama);
panelAtas.add(labelProdi);
panelAtas.add(fieldProdi);
panelAtas.add(labelAlamat);
panelAtas.add(fieldAlamat);
panelAtas.add(labelKota);
panelAtas.add(fieldKota);
panelAtas.add(tombolSimpan);
panelAtas.add(tombolBatal);
panelSemua.add(“North”, panelAtas);
Object[] row = {“NIM”, “Nama”, “Prodi”, “Alamat”, “Kota”};
tabMode = new DefaultTableModel(null, row);
tabel.setModel(tabMode);
scrollPane.getViewport().add(tabel, null);
panelSemua.add(“Center”, scrollPane);
koneksiDatabase();
tampilDataKeTabel();
tombolSimpan.addActionListener(this);
tombolBatal.addActionListener(this);
JFrame frame = new JFrame(“input data”);
frame.setContentPane(panelSemua);
frame.setSize(400,250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
}
public void actionPerformed(ActionEvent kejadian) {
try {
if(kejadian.getSource()==tombolSimpan) {
String nama = fieldNama.getText();
String nim = fieldNim.getText();
String prodi = fieldProdi.getText();
String alamat = fieldAlamat.getText();
String kota = fieldKota.getText();
//Class.forName(“com.mysql.jdbc.Driver”);
//System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
//Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
//                                                 “root”,”t3l0g0dh0g”);
//System.out.println(“koneksi sukses…”);
koneksiDatabase();
Statement perintah = koneksi.createStatement();
String sql = “INSERT INTO mahasiswa VALUES (‘”+nim+”‘,’”+nama+”‘,’”+prodi+”‘,’”+alamat+”‘,’”+kota+”‘);”;
perintah.executeUpdate(sql);
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
//perintah.close();
//koneksi.close();
JOptionPane.showMessageDialog(panelSemua, “data sukses tersimpan…”);
//hapusTabel();
tampilDataKeTabel();
}
}
catch(Exception e) {
System.out.println(“terjadi error…”);
}
if(kejadian.getSource()==tombolBatal){
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
}
}
public void koneksiDatabase() {
try {
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“JDBC ditemukan…”);
try {
String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
koneksi = DriverManager.getConnection(url);
System.out.println(“koneksi sukses…”);
}
catch (SQLException se) {
System.out.println(“koneksi gagal = ” + se);
System.exit(0);
}
}
catch (ClassNotFoundException cnfe) {
System.out.println(“class tidak ditemukan… error: ” + cnfe);
System.exit(0);
}
}
public void tampilDataKeTabel() {
hapusTabel();
try {
String sql = “SELECT * FROM mahasiswa”;
Statement stat = koneksi.createStatement();
ResultSet set = stat.executeQuery(sql);
while(set.next()) {
String nim = set.getString(“nim”);
String nama = set.getString(“nama”);
String prodi = set.getString(“prodi”);
String alamat = set.getString(“alamat”);
String kota = set.getString(“kota”);
String[] data = {nim, nama, prodi, alamat, kota};
tabMode.addRow(data);
}
}
catch (Exception e) {
}
}
public void hapusTabel() {
int row = tabMode.getRowCount();
for(int i=0; i<row; i++) {
tabMode.removeRow(0);
}
}
public static void main(String[] args) {
sisip insertRecord = new sisip();
}
}
Output Program:


sebelum sisip record baru

setelah penyisipan record baru

0 komentar:

Posting Komentar

Please Give Your Feedback Or Message.
Thank You!!?

Unique 1:1 Traffic Exchange

♪ ♥ Ma liste de blogs ♥ ♪

♪ ♥ Please Votes! Thanks. ♪ ♥
×