Guest User

Untitled

a guest
Mar 29th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.83 KB | None | 0 0
  1. import java.sql.*;
  2. import javax.swing.JOptionPane;
  3. import javax.swing.table.DefaultTableModel;
  4.  
  5.  
  6. public class NewJFrame extends javax.swing.JFrame {
  7. Connection con;
  8. Statement stmt;
  9. String query;
  10. ResultSet rs;
  11.     /**
  12.      * Creates new form NewJFrame
  13.      */
  14.     public NewJFrame() {
  15.         initComponents();
  16.     }
  17.  
  18.     /**
  19.      * This method is called from within the constructor to initialize the form.
  20.      * WARNING: Do NOT modify this code. The content of this method is always
  21.      * regenerated by the Form Editor.
  22.      */
  23.     @SuppressWarnings("unchecked")
  24.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  25.     private void initComponents() {
  26.  
  27.         jScrollPane1 = new javax.swing.JScrollPane();
  28.         tb = new javax.swing.JTable();
  29.         jButton1 = new javax.swing.JButton();
  30.         jButton2 = new javax.swing.JButton();
  31.  
  32.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  33.  
  34.         tb.setModel(new javax.swing.table.DefaultTableModel(
  35.             new Object [][] {
  36.  
  37.             },
  38.             new String [] {
  39.                 "Serial No", "Item", "Quantity", "Price", "Total"
  40.             }
  41.         ));
  42.         jScrollPane1.setViewportView(tb);
  43.  
  44.         jButton1.setText("Obtain Data");
  45.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  46.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  47.                 jButton1ActionPerformed(evt);
  48.             }
  49.         });
  50.  
  51.         jButton2.setText("jButton2");
  52.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  53.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  54.                 jButton2ActionPerformed(evt);
  55.             }
  56.         });
  57.  
  58.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  59.         getContentPane().setLayout(layout);
  60.         layout.setHorizontalGroup(
  61.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  62.             .addGroup(layout.createSequentialGroup()
  63.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  64.                     .addGroup(layout.createSequentialGroup()
  65.                         .addGap(30, 30, 30)
  66.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE))
  67.                     .addGroup(layout.createSequentialGroup()
  68.                         .addGap(73, 73, 73)
  69.                         .addComponent(jButton1)
  70.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  71.                         .addComponent(jButton2)))
  72.                 .addContainerGap(146, Short.MAX_VALUE))
  73.         );
  74.         layout.setVerticalGroup(
  75.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  76.             .addGroup(layout.createSequentialGroup()
  77.                 .addContainerGap()
  78.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
  79.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  80.                     .addGroup(layout.createSequentialGroup()
  81.                         .addGap(69, 69, 69)
  82.                         .addComponent(jButton1))
  83.                     .addGroup(layout.createSequentialGroup()
  84.                         .addGap(61, 61, 61)
  85.                         .addComponent(jButton2)))
  86.                 .addContainerGap(76, Short.MAX_VALUE))
  87.         );
  88.  
  89.         pack();
  90.     }// </editor-fold>                        
  91.  
  92.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  93.        
  94.         DefaultTableModel model=(DefaultTableModel)tb.getModel();
  95.         try{
  96.                 con = DriverManager.getConnection("jdbc:mysql://localhost/anmol","root","tiger");
  97.                 stmt=con.createStatement();
  98.                 query="SELECT * FROM bill1;";
  99.                 rs=stmt.executeQuery(query);
  100.                 while(rs.next())
  101.                 {
  102.                     String item=rs.getString("Item");
  103.                     int q=rs.getInt("quantity");
  104.                     int price=rs.getInt("price");
  105.                     int total=rs.getInt("Total");
  106.                     model.addRow(new Object[]{item,q,price,total});
  107.                 }
  108.                 }
  109.         catch(Exception e)
  110.         {
  111.             JOptionPane.showMessageDialog(null,e.getMessage());
  112.         }
  113.        
  114.     }                                        
  115.  
  116.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  117.      
  118.     }                                        
  119.  
  120.     /**
  121.      * @param args the command line arguments
  122.      */
  123.     public static void main(String args[]) {
  124.         /* Set the Nimbus look and feel */
  125.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  126.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  127.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  128.          */
  129.         try {
  130.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  131.                 if ("Nimbus".equals(info.getName())) {
  132.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  133.                     break;
  134.                 }
  135.             }
  136.         } catch (ClassNotFoundException ex) {
  137.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  138.         } catch (InstantiationException ex) {
  139.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  140.         } catch (IllegalAccessException ex) {
  141.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  142.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  143.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  144.         }
  145.         //</editor-fold>
  146.  
  147.         /* Create and display the form */
  148.         java.awt.EventQueue.invokeLater(new Runnable() {
  149.             public void run() {
  150.                 new NewJFrame().setVisible(true);
  151.             }
  152.         });
  153.     }
  154.  
  155.     // Variables declaration - do not modify                    
  156.     private javax.swing.JButton jButton1;
  157.     private javax.swing.JButton jButton2;
  158.     private javax.swing.JScrollPane jScrollPane1;
  159.     private javax.swing.JTable tb;
  160.     // End of variables declaration                  
  161. }
Add Comment
Please, Sign In to add comment