Buat tampilan seperti gambar diatas menggunakan neetbeans
Sintak Tombol Cari:
String nim=tfNim.getText();Sintak Tombol Baru
try {
//1. mengidentifikasi class driver
Class.forName("com.mysql.jdbc.Driver");
//2. membuat koneksi
Connection koneksi= (Connection)
DriverManager.getConnection(
"jdbc:mysql://localhost:3306/akademik",
"root","");
//3. membuat statement
Statement stetmen= (Statement)koneksi.
createStatement();
//4. menjalankan statement
ResultSet result=stetmen.executeQuery("SELECT nim, nama, alamat, jenisKelamin, "
+ "programStudi FROM mahasiswa WHERE nim='"+nim+"'");
//tampilkan data kalau ketemu
result.next();
tfNim.setText(result.getString("nim"));
tfNama.setText(result.getString("nama"));
tfAlamat.setText(result.getString("alamat"));
if(result.getString("jenisKelamin").equals("L")){
rbLakilaki.setSelected(true);
}else{
rbPerempuan.setSelected(true);
}
//menampilkan progdi ke comboBox
} catch (SQLException ex) {
if(ex.getErrorCode()==0){
JOptionPane.showMessageDialog(rootPane,
"Data tidak ditemukan", "Peringatan",
JOptionPane.WARNING_MESSAGE);
tfNim.setText(null);
tfNama.setText(null);
tfAlamat.setText(null);
rbLakilaki.setSelected(true);
}else{
JOptionPane.showMessageDialog(rootPane,
"Terjadi kesalahan:\n"+ex,
"Kesalahan",
JOptionPane.ERROR_MESSAGE);
}
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(rootPane,
"Class tidak ditemukan",
"Kesalahan",
JOptionPane.ERROR_MESSAGE);
}
}
tfNim.setText(null);Sintak Tombol Simpan
tfNama.setText(null);
tfAlamat.setText(null);
rbLakilaki.setSelected(true);
tfNim.requestFocus();
}
String nim= tfNim.getText();Download Hasil yang udh jadi
String nama= tfNama.getText();
String alamat= tfAlamat.getText();
String jenisKelamin;
if(rbLakilaki.isSelected()){
jenisKelamin= "L";
}else{
jenisKelamin= "P";
}
String programStudi;
if(cbProgdi.getSelectedIndex()==0){
programStudi= "Teknik Informatika";
}else if(cbProgdi.getSelectedIndex()==1){
programStudi= "Sistem Informatika";
}else if(cbProgdi.getSelectedIndex()==2){
programStudi= "Manajemen Informatika";
}else{
programStudi= "K. Akuntansi";
}
try {
//1. mengidentifikasi class driver
Class.forName("com.mysql.jdbc.Driver");
//2. membuat koneksi
Connection koneksi= (Connection)
DriverManager.getConnection(
"jdbc:mysql://localhost:3306/akademik",
"root","");
//3. membuat statement
Statement stetmen= (Statement)koneksi.
createStatement();
//4. menjalankan statement
stetmen.executeUpdate(
"INSERT INTO mahasiswa(nim, nama, alamat,jeniskelamin,programStudi) "
+ "VALUES('"+nim+"', '"+nama+
"', '"+alamat+"', '"+jenisKelamin+"','"+programStudi+"')"
);
JOptionPane.showMessageDialog(rootPane,
"Data berhasil disimpan",
"Informasi",
JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(rootPane,
"Maaf terjadi kesalahan:\n"+ex,
"Kesalahan",
JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(rootPane,
"Class tidak ditemukan",
"Kesalahan",
JOptionPane.ERROR_MESSAGE);
}
Post a Comment
Silahkan berkomentar disini. komentar anda sangat membantu demi kemajuan blog ini. dan terima kasih atas komentar-komentar yang sudah masuk.