1 Jan 2012

How to make an user interface for data input in java


Before you copy this script, make sure that you make the UI first, and named is as just as like this example,, so it will work properly, thanks......
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author andy3je
 */

public class FInputData extends javax.swing.JFrame {
Connection koneksi;
Statement perintah;

private void btnKirimActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        try {
        String konek = "jdbc:mysql://localhost:3306/mahasiswa";
        String user = "root";
        String pass = "root";
        Class.forName("com.mysql.jdbc.Driver");
        koneksi = DriverManager.getConnection(konek, user, pass);
        String nim, nama, alamat, jurusan;
        nim = txtNim.getText().trim();
        nama = txtNama.getText().trim();
        alamat = txtAlamat.getText().trim();
        jurusan = txtJurusan.getText().trim();
        perintah = koneksi.createStatement();
        String sql = "insert into dataMhs values('"+nim+"','"+nama+"','"+alamat+"','"+jurusan+"');";
        perintah.execute(sql);
        perintah.close();
        JOptionPane.showMessageDialog(null, "Berhasil Disimpan");
        }catch(ClassNotFoundException ex){
            Logger.getLogger(FKoneksi.class.getName()).log(Level.SEVERE,null, ex);
            JOptionPane.showMessageDialog(null, "GAGAL MENYIMPAN DATA MAHASISWA");
        }catch(SQLException ex){
             Logger.getLogger(FKoneksi.class.getName()).log(Level.SEVERE,null, ex);
            JOptionPane.showMessageDialog(null, "GAGAL.......!! COBA LAGI");
        }
       
    }



As you can see it has been saved,, and this is the prove that it has been saved to the database
mysql> select * from dataMhs;
+----------+------------------+---------+---------+
| nim      | nama             | alamat  | jurusan |
+----------+------------------+---------+---------+
| 08561013 | Tri Juliansyah E | Kalasan | SI      |
+----------+------------------+---------+---------+
1 row in set (0.02 sec)



Tidak ada komentar:

Posting Komentar