Created
January 9, 2021 13:32
-
-
Save VicoErv/20e1fce2c6077ef18bf93d7350399dc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package perpustakaan.ui; | |
import java.awt.event.WindowEvent; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.swing.DefaultListModel; | |
import javax.swing.JOptionPane; | |
import perpustakaan.ConnectionHelper; | |
/** | |
* | |
* @author ygsto | |
*/ | |
public class TambahPeminjamanJFrame extends javax.swing.JFrame { | |
Connection conn; | |
PreparedStatement stmtBook; | |
PreparedStatement stmtMahasiswa; | |
PreparedStatement stmtCekDipinjam; | |
PreparedStatement stmtAddPinjam; | |
DefaultListModel modelBuku; | |
DefaultListModel modelMahasiswa; | |
ArrayList<Integer> idsBuku = new ArrayList<>(); | |
ArrayList<Integer> idsMahasiswa = new ArrayList<>(); | |
/** | |
* Creates new form TambahPeminjamanJFrame | |
*/ | |
public TambahPeminjamanJFrame() { | |
initComponents(); | |
modelBuku = new DefaultListModel(); | |
modelMahasiswa = new DefaultListModel(); | |
this.listBuku.setModel(modelBuku); | |
this.listMahasiswa.setModel(modelMahasiswa); | |
try { | |
conn = ConnectionHelper.getConnection(); | |
stmtBook = this.conn.prepareStatement("SELECT * FROM buku WHERE judul LIKE ?"); | |
stmtMahasiswa = this.conn.prepareStatement("SELECT * FROM mahasiswa WHERE nama LIKE ?"); | |
stmtCekDipinjam = this.conn.prepareStatement("SELECT COUNT(*) FROM peminjaman WHERE tanggal_kembali IS null AND id_buku = ?"); | |
stmtAddPinjam = this.conn.prepareStatement("INSERT INTO peminjaman VALUES(null, ?, ?, NOW(), null)"); | |
} catch (SQLException ex) { | |
Logger.getLogger(TambahPeminjamanJFrame.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/** | |
* This method is called from within the constructor to initialize the form. | |
* WARNING: Do NOT modify this code. The content of this method is always | |
* regenerated by the Form Editor. | |
*/ | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code"> | |
private void initComponents() { | |
jScrollPane1 = new javax.swing.JScrollPane(); | |
listMahasiswa = new javax.swing.JList<>(); | |
jScrollPane2 = new javax.swing.JScrollPane(); | |
listBuku = new javax.swing.JList<>(); | |
txtBuku = new javax.swing.JTextField(); | |
btnBuku = new javax.swing.JButton(); | |
btnMahasiswa = new javax.swing.JButton(); | |
txtMahasiswa = new javax.swing.JTextField(); | |
jLabel1 = new javax.swing.JLabel(); | |
jLabel2 = new javax.swing.JLabel(); | |
btnSend = new javax.swing.JButton(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
listMahasiswa.setModel(new javax.swing.AbstractListModel<String>() { | |
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; | |
public int getSize() { return strings.length; } | |
public String getElementAt(int i) { return strings[i]; } | |
}); | |
jScrollPane1.setViewportView(listMahasiswa); | |
listBuku.setModel(new javax.swing.AbstractListModel<String>() { | |
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; | |
public int getSize() { return strings.length; } | |
public String getElementAt(int i) { return strings[i]; } | |
}); | |
jScrollPane2.setViewportView(listBuku); | |
btnBuku.setText("Cari"); | |
btnBuku.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
btnBukuActionPerformed(evt); | |
} | |
}); | |
btnMahasiswa.setText("Cari"); | |
btnMahasiswa.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
btnMahasiswaActionPerformed(evt); | |
} | |
}); | |
jLabel1.setText("Buku"); | |
jLabel2.setText("Mahasiswa"); | |
btnSend.setText("Kirim"); | |
btnSend.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
btnSendActionPerformed(evt); | |
} | |
}); | |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
getContentPane().setLayout(layout); | |
layout.setHorizontalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addComponent(jLabel1) | |
.addGap(0, 0, Short.MAX_VALUE)) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addGap(0, 2, Short.MAX_VALUE) | |
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addComponent(txtBuku) | |
.addGap(18, 18, 18) | |
.addComponent(btnBuku))) | |
.addGap(18, 18, 18) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jLabel2) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addComponent(txtMahasiswa) | |
.addGap(18, 18, 18) | |
.addComponent(btnMahasiswa)) | |
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE))) | |
.addGap(10, 10, 10)) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(btnSend) | |
.addContainerGap()) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jLabel1) | |
.addComponent(jLabel2)) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(txtBuku, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(btnBuku) | |
.addComponent(txtMahasiswa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(btnMahasiswa)) | |
.addGap(16, 16, 16) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addGap(18, 18, 18) | |
.addComponent(btnSend) | |
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
); | |
pack(); | |
}// </editor-fold> | |
private void btnBukuActionPerformed(java.awt.event.ActionEvent evt) { | |
try { | |
// TODO add your handling code here: | |
modelBuku.setSize(0); | |
this.stmtBook.setString(1, '%' + txtBuku.getText() + '%'); | |
ResultSet rs = this.stmtBook.executeQuery(); | |
while(rs.next()) { | |
modelBuku.addElement(rs.getString("judul")); | |
idsBuku.add(rs.getInt("id_buku")); | |
} | |
} catch (SQLException ex) { | |
Logger.getLogger(TambahPeminjamanJFrame.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
private void btnMahasiswaActionPerformed(java.awt.event.ActionEvent evt) { | |
// TODO add your handling code here: | |
try { | |
modelMahasiswa.setSize(0); | |
idsMahasiswa.clear(); | |
this.stmtMahasiswa.setString(1, '%' + txtMahasiswa.getText() + '%'); | |
ResultSet rs = this.stmtMahasiswa.executeQuery(); | |
while(rs.next()) { | |
modelMahasiswa.addElement(rs.getString("nama")); | |
idsMahasiswa.add(rs.getInt("id_mahasiswa")); | |
} | |
} catch (SQLException ex) { | |
Logger.getLogger(TambahPeminjamanJFrame.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) { | |
// TODO add your handling code here: | |
int selectedBuku = listBuku.getSelectedIndex(); | |
int selectedMahasiswa = listMahasiswa.getSelectedIndex(); | |
if (selectedBuku == -1) { | |
JOptionPane.showMessageDialog(this, "Pilih buku terlebih dahulu."); | |
return; | |
} | |
if (selectedMahasiswa == -1) { | |
JOptionPane.showMessageDialog(this, "Pilih mahasiswa terlebih dahulu."); | |
return; | |
} | |
int idBuku = idsBuku.get(selectedBuku); | |
int idMahasiswa = idsMahasiswa.get(selectedMahasiswa); | |
try { | |
this.stmtCekDipinjam.setInt(1, idBuku); | |
ResultSet rs = this.stmtCekDipinjam.executeQuery(); | |
int size = 0; | |
if (rs != null) | |
{ | |
rs.last(); // moves cursor to the last row | |
size = rs.getInt("COUNT(*)"); // get row id | |
} | |
if (size > 0) { | |
JOptionPane.showMessageDialog(this, "Buku masih dipinjam."); | |
return; | |
} | |
this.stmtAddPinjam.setInt(1, idBuku); | |
this.stmtAddPinjam.setInt(2, idMahasiswa); | |
this.stmtAddPinjam.execute(); | |
} catch (SQLException ex) { | |
Logger.getLogger(TambahPeminjamanJFrame.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String args[]) { | |
/* Set the Nimbus look and feel */ | |
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
*/ | |
try { | |
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
if ("Nimbus".equals(info.getName())) { | |
javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
break; | |
} | |
} | |
} catch (ClassNotFoundException ex) { | |
java.util.logging.Logger.getLogger(TambahPeminjamanJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(TambahPeminjamanJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(TambahPeminjamanJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(TambahPeminjamanJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} | |
//</editor-fold> | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
new TambahPeminjamanJFrame().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify | |
private javax.swing.JButton btnBuku; | |
private javax.swing.JButton btnMahasiswa; | |
private javax.swing.JButton btnSend; | |
private javax.swing.JLabel jLabel1; | |
private javax.swing.JLabel jLabel2; | |
private javax.swing.JScrollPane jScrollPane1; | |
private javax.swing.JScrollPane jScrollPane2; | |
private javax.swing.JList<String> listBuku; | |
private javax.swing.JList<String> listMahasiswa; | |
private javax.swing.JTextField txtBuku; | |
private javax.swing.JTextField txtMahasiswa; | |
// End of variables declaration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment