Tampilan Sederhana menggunakan Data Base Pada Java

Tampilan Sederhana menggunakan Data Base sebagai berikut;

import javax.swing.*;

import java.awt.GridLayout;

import java.awt.event.*;

import java.sql.*;

public class insert 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 panel = new JPanel();

insert(){

GridLayout gl = new GridLayout(6,2);

panel.setLayout(gl);

panel.add(labelNim);

panel.add(fieldNim);

panel.add(labelNama);

panel.add(fieldNama);

panel.add(labelProdi);

panel.add(fieldProdi);

panel.add(labelAlamat);

panel.add(fieldAlamat);

panel.add(labelKota);

panel.add(fieldKota);

panel.add(tombolSimpan);

panel.add(tombolBatal);

tombolSimpan.addActionListener(this);

tombolBatal.addActionListener(this);

JFrame frame = new JFrame(“input data”);

frame.setContentPane(panel);

frame.setSize(400,200);

frame.setVisible(true);

//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false);

}

public void actionPerformed(ActionEvent event) {

try {

if(event.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”);

Statement statement = koneksi.createStatement();

String sql = “INSERT INTO mahasiswa values (“+nim+”,”+nama+”,”+prodi+”,”+alamat+”,”+kota+”);”;

statement.executeUpdate(sql);

fieldNama.setText(“”);

fieldNim.setText(“”);

fieldProdi.setText(“”);

fieldAlamat.setText(“”);

fieldKota.setText(“”);

statement.close();

koneksi.close();

JOptionPane.showMessageDialog(panel, “data sukses tersimpan”);

}

}

catch(Exception e) {

System.out.println(“salah”);

}

if(event.getSource()==tombolBatal){

fieldNama.setText(“”);

fieldNim.setText(“”);

fieldProdi.setText(“”);

fieldAlamat.setText(“”);

fieldKota.setText(“”);

}

}

public static void main(String[] args) {

insert insertRecord = new insert();

}

}

About ILMUKAMU

Anak pengangguran yang akan menjadi sukses!!

Posted on September 28, 2011, in Java and tagged . Bookmark the permalink. Tinggalkan komentar.

Komentarnya Brow...

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: