Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * NewJFrame.java
  8.  *
  9.  * Created on Jul 1, 2010, 6:43:51 PM
  10.  */
  11.  
  12. /**
  13.  *
  14.  * @author oracle
  15.  */
  16. import java.sql.*;
  17. import javax.swing.*;
  18. import java.awt.*;
  19. import java.util.Vector;
  20. public class NewJFrame extends javax.swing.JFrame {
  21. public Connection con = null;
  22. public String connectionUrl = "jdbc:mysql://localhost/stock?" + "user=root&password=root";
  23.     /** Creates new form NewJFrame */
  24. Container contentPane = getContentPane();
  25.     public NewJFrame() {
  26.         initComponents();
  27.         try{
  28.            Class.forName("com.mysql.jdbc.Driver");
  29.           con =DriverManager.getConnection(connectionUrl);
  30.          
  31.             // set layout manager
  32.           System.out.println(new Vector().capacity());
  33.     contentPane.setLayout(new BorderLayout());
  34.  
  35.         }
  36.         catch (Exception e){System.out.print(e.toString()); }
  37.     }
  38.  
  39.     /** This method is called from within the constructor to
  40.      * initialize the form.
  41.      * WARNING: Do NOT modify this code. The content of this method is
  42.      * always regenerated by the Form Editor.
  43.      */
  44.     @SuppressWarnings("unchecked")
  45.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  46.     private void initComponents() {
  47.  
  48.         show_all = new javax.swing.JButton();
  49.  
  50.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  51.  
  52.         show_all.setText("Show All");
  53.         show_all.addActionListener(new java.awt.event.ActionListener() {
  54.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  55.                 show_allActionPerformed(evt);
  56.             }
  57.         });
  58.  
  59.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  60.         getContentPane().setLayout(layout);
  61.         layout.setHorizontalGroup(
  62.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  63.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  64.                 .addContainerGap(489, Short.MAX_VALUE)
  65.                 .addComponent(show_all)
  66.                 .addGap(87, 87, 87))
  67.         );
  68.         layout.setVerticalGroup(
  69.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  70.             .addGroup(layout.createSequentialGroup()
  71.                 .addGap(29, 29, 29)
  72.                 .addComponent(show_all)
  73.                 .addContainerGap(442, Short.MAX_VALUE))
  74.         );
  75.  
  76.         pack();
  77.     }// </editor-fold>
  78.  
  79.     private void show_allActionPerformed(java.awt.event.ActionEvent evt) {                                        
  80.         // TODO add your handling code here:
  81.         String query=null;
  82.  
  83.         String[] columnNames = {"id",
  84.                         "Name",
  85.                         "price",
  86.                         "quantity",
  87.                         };
  88.  
  89.        
  90.  
  91.  
  92.         int i = 0;
  93.         try {
  94.         Statement s=this.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  95.         query="select * from product";
  96.         ResultSet rs=s.executeQuery(query);
  97.         int cols=rs.getMetaData().getColumnCount();
  98.  
  99.         System.out.print(cols);
  100.  
  101.         Vector data = new Vector () ;
  102.         Vector columnN=new Vector () ;
  103.         for (int j=1; j <= cols; j++){
  104.         columnN.add(rs.getMetaData().getColumnClassName(j));
  105.         }
  106.  
  107.            while (rs.next())
  108.       {
  109.         Vector columns = new Vector();
  110.         for (  i=1; i<cols; i++ )
  111.         {
  112.           columns.addElement(rs.getObject(i+1));  //   get the Object at i instead of i+1object
  113.         }  // end for
  114.         data.addElement(columns);
  115.       }
  116.  
  117.  
  118.         System.out.print(s.getMaxRows());
  119.         JTable table = new JTable(data, columnN);
  120.  
  121.         int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
  122.       int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
  123.       JScrollPane jsp = new JScrollPane(table,v,h);
  124.         jsp.add(table);
  125.         table.setFillsViewportHeight(true);
  126.  
  127.       // Add scroll pane to content pane
  128.       contentPane.add(jsp, BorderLayout.CENTER);
  129.  
  130.              
  131.        
  132.         }
  133.          catch (SQLException  e){ System.out.print(e.toString()+"1");}
  134.  
  135.  
  136.     }                                        
  137.  
  138.     /**
  139.     * @param args the command line arguments
  140.     */
  141.     public static void main(String args[]) {
  142.         java.awt.EventQueue.invokeLater(new Runnable() {
  143.             public void run() {
  144.                 new NewJFrame().setVisible(true);
  145.             }
  146.         });
  147.     }
  148.  
  149.     // Variables declaration - do not modify
  150.     private javax.swing.JButton show_all;
  151.     // End of variables declaration
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement