Advertisement
Guest User

soalno3

a guest
Dec 14th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. import java.sql.*;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import javax.swing.*;
  10. import javax.swing.table.*;
  11. /**
  12. *
  13. * @author Muh Farid Zia
  14. */
  15. public class Soal3 extends javax.swing.JFrame {
  16.  
  17. public Connection conn;
  18. public ResultSet rs;
  19. public PreparedStatement pst;
  20.  
  21. /**
  22. * Creates new form Soal3
  23. */
  24. public Soal3() throws SQLException {
  25. initComponents();
  26.  
  27. conn = null;
  28. conn = DriverManager.getConnection("jdbc:mysql://localhost/soal","root","");
  29. UpdateTabel();
  30. setLocationRelativeTo(null);
  31.  
  32.  
  33. }
  34.  
  35. private void UpdateTabel() {
  36. try {
  37. String sql = "SELECT m.nama, m.alamat, n.nilai FROM nilai n INNER JOIN mahasiswa m ON m.id_mahasiswa = n.id_mahasiswa";
  38. pst = conn.prepareStatement(sql);
  39. rs = pst.executeQuery();
  40. DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
  41. dtm.setRowCount(0);
  42. String [] data = new String[6];
  43. int i = 1;
  44.  
  45. while(rs.next()) {
  46. data[0] = rs.getString("Nama");
  47. data[1] = rs.getString("Alamat");
  48. data[2] = rs.getString("Nilai");
  49. dtm.addRow(data);
  50. i++;
  51. }
  52. }
  53. catch (Exception e) {
  54. JOptionPane.showMessageDialog(null, e);
  55. }
  56. }
  57.  
  58.  
  59. /**
  60. * This method is called from within the constructor to initialize the form.
  61. * WARNING: Do NOT modify this code. The content of this method is always
  62. * regenerated by the Form Editor.
  63. */
  64. @SuppressWarnings("unchecked")
  65. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  66. private void initComponents() {
  67.  
  68. jLabel1 = new javax.swing.JLabel();
  69. jScrollPane1 = new javax.swing.JScrollPane();
  70. jTable1 = new javax.swing.JTable();
  71.  
  72. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  73.  
  74. jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  75. jLabel1.setText("Data Nilai Mahasiswa");
  76.  
  77. jTable1.setModel(new javax.swing.table.DefaultTableModel(
  78. new Object [][] {
  79. {null, null, null},
  80. {null, null, null},
  81. {null, null, null},
  82. {null, null, null}
  83. },
  84. new String [] {
  85. "Nama", "Alamat", "Nilai"
  86. }
  87. ));
  88. jScrollPane1.setViewportView(jTable1);
  89.  
  90. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  91. getContentPane().setLayout(layout);
  92. layout.setHorizontalGroup(
  93. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  94. .addGroup(layout.createSequentialGroup()
  95. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  96. .addGroup(layout.createSequentialGroup()
  97. .addGap(124, 124, 124)
  98. .addComponent(jLabel1))
  99. .addGroup(layout.createSequentialGroup()
  100. .addContainerGap()
  101. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
  102. .addContainerGap(15, Short.MAX_VALUE))
  103. );
  104. layout.setVerticalGroup(
  105. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106. .addGroup(layout.createSequentialGroup()
  107. .addGap(21, 21, 21)
  108. .addComponent(jLabel1)
  109. .addGap(18, 18, 18)
  110. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
  111. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  112. );
  113.  
  114. pack();
  115. }// </editor-fold>
  116.  
  117. /**
  118. * @param args the command line arguments
  119. */
  120. public static void main(String args[]) {
  121. /* Set the Nimbus look and feel */
  122. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  123. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  124. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  125. */
  126. try {
  127. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  128. if ("Nimbus".equals(info.getName())) {
  129. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  130. break;
  131. }
  132. }
  133. } catch (ClassNotFoundException ex) {
  134. java.util.logging.Logger.getLogger(Soal3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  135. } catch (InstantiationException ex) {
  136. java.util.logging.Logger.getLogger(Soal3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  137. } catch (IllegalAccessException ex) {
  138. java.util.logging.Logger.getLogger(Soal3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  139. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  140. java.util.logging.Logger.getLogger(Soal3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  141. }
  142. //</editor-fold>
  143.  
  144. /* Create and display the form */
  145. java.awt.EventQueue.invokeLater(new Runnable() {
  146. public void run() {
  147. try {
  148. new Soal3().setVisible(true);
  149. } catch (SQLException ex) {
  150. Logger.getLogger(Soal3.class.getName()).log(Level.SEVERE, null, ex);
  151. }
  152. }
  153. });
  154. }
  155.  
  156. // Variables declaration - do not modify
  157. private javax.swing.JLabel jLabel1;
  158. private javax.swing.JScrollPane jScrollPane1;
  159. private javax.swing.JTable jTable1;
  160. // End of variables declaration
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement