- Nama VarChar (30)
- Alamat Text
- Stambuk Char (4)
- Fakultas VarChar (15)
- Jurusan VarChar (15)
Berikut ini adalah langkah-langkah membuat database :
1. Aktifkan Apache dan MySQL pada XAMPP
2. Bukalah Web Browser, pada hal ini saya menggunakan Opera. Ketiklah "localhost/xampp", seperti pada tampilan berikut :
3. Click phpMyAdmin > New, setelah itu buatlah nama Tabel tersebut dan buat juga Fieldnya. Seperti pada tampilan berikut :
1. Desainlah JFrame seperti tampilan berikut ini :
2. Source Code :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import
javax.swing.table.DefaultTableModel;
import java.awt.Dimension;
import java.awt.Toolkit;
public class InputDataMahasiswa
extends javax.swing.JFrame {
//Untuk membuat Tampilan Screen Awal berada di Tengah
public InputDataMahasiswa() {
initComponents();
tampilkandata();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation(
(screenSize.width -
frameSize.width) / 2,
(screenSize.height -
frameSize.height) / 2);
}
//Untuk Menghubungkan
public Connection conn;
public void koneksi() throws SQLException
{
try {
conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mahasiswa",
"root", "");
} catch (ClassNotFoundException ex)
{
Logger.getLogger(InputDataMahasiswa.class.getName()).log(Level.SEVERE,
null, ex);
} catch (SQLException e)
{
Logger.getLogger(InputDataMahasiswa.class.getName()).log(Level.SEVERE,
null, e);
} catch (Exception es)
{
Logger.getLogger(InputDataMahasiswa.class.getName()).log(Level.SEVERE,
null, es);
}
}
@SuppressWarnings("unchecked")
//Menyimpan
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String nim = jTextField1.getText();
String nama = jTextField2.getText();
String alamat = jTextField3.getText();
String stambuk = jTextField4.getText();
String fakultas =
jTextField5.getText();
String jurusan = jTextField6.getText();
try{
koneksi();
String sql = "insert into mhs
values('"+nim+"','"+nama+"','"+alamat+"','"+stambuk+"','"+fakultas+"','"+jurusan+"');";
Statement statement = conn.createStatement();
statement.executeUpdate(sql);
statement.close();
bersih();
tampilkandata();
}catch (Exception e) {
}
}
//Mencari
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String NIM =jTextField1.getText();
try {
koneksi();
String sql = "select Nama, Alamat,
Stambuk, Fakultas, Jurusan from mhs where NIM='"+NIM+"'" ;
Statement
stat = conn.createStatement();
ResultSet
res=stat.executeQuery(sql);
while
(res.next()) {
jTextField2.setText(res.getString(1));
jTextField3.setText(res.getString(2));
jTextField4.setText(res.getString(3));
jTextField5.setText(res.getString(4));
jTextField6.setText(res.getString(5));
}
} catch (Exception e) {
}
}
//Mengubah
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String nim = jTextField1.getText();
String nama = jTextField2.getText();
String alamat = jTextField3.getText();
String stambuk = jTextField4.getText();
String fakultas =
jTextField5.getText();
String jurusan = jTextField6.getText();
try{
koneksi();
Statement statement = conn.createStatement();
String sql = "UPDATE mhs set
Nama
='"+nama+"',Alamat='"+alamat+"',Stambuk='"+stambuk+"',Fakultas='"+fakultas+"',Jurusan
='"+jurusan+"'where NIM='"+nim+"'";
statement.executeUpdate(sql);
statement.close();
bersih();
tampilkandata();
}catch (Exception e) {
}
bersih();
}
//Menghapus
private void
jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
String nim = jTextField1.getText();
try{
koneksi();
Statement statement = conn.createStatement();
String sql = "Delete from mhs
where NIM='"+nim+"'";
statement.executeUpdate(sql);
statement.close();
bersih();
tampilkandata();
}catch (Exception e) {
}
bersih();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new
Runnable() {
public void run() {
new
InputDataMahasiswa().setVisible(true);
}
});
}
//Menampilkan Tabel
private void tampilkandata() {
DefaultTableModel x = new
DefaultTableModel();
x.addColumn("NIM");
x.addColumn("Nama");
x.addColumn("Alamat");
x.addColumn("Stambuk");
x.addColumn("Fakultas");
x.addColumn("Jurusan");
try {
koneksi();
String sql = "select * from
mhs";
Statement stat = conn.createStatement();
ResultSet
res=stat.executeQuery(sql);
while (res.next()) {
x.addRow(new
Object[]{res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5),res.getString(6)});}
jTable1.setModel(x);
} catch (Exception e) {
}
}
private void bersih(){
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField1.requestFocus();
}
}
0 komentar:
Post a Comment