Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * NewJFrame.java
- *
- * Created on Jul 1, 2010, 6:43:51 PM
- */
- /**
- *
- * @author oracle
- */
- import java.sql.*;
- import javax.swing.*;
- import java.awt.*;
- import java.util.Vector;
- public class NewJFrame extends javax.swing.JFrame {
- public Connection con = null;
- public String connectionUrl = "jdbc:mysql://localhost/stock?" + "user=root&password=root";
- /** Creates new form NewJFrame */
- Container contentPane = getContentPane();
- public NewJFrame() {
- initComponents();
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con =DriverManager.getConnection(connectionUrl);
- // set layout manager
- System.out.println(new Vector().capacity());
- contentPane.setLayout(new BorderLayout());
- }
- catch (Exception e){System.out.print(e.toString()); }
- }
- /** 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() {
- show_all = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- show_all.setText("Show All");
- show_all.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- show_allActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(489, Short.MAX_VALUE)
- .addComponent(show_all)
- .addGap(87, 87, 87))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(29, 29, 29)
- .addComponent(show_all)
- .addContainerGap(442, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void show_allActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String query=null;
- String[] columnNames = {"id",
- "Name",
- "price",
- "quantity",
- };
- int i = 0;
- try {
- Statement s=this.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
- query="select * from product";
- ResultSet rs=s.executeQuery(query);
- int cols=rs.getMetaData().getColumnCount();
- System.out.print(cols);
- Vector data = new Vector () ;
- Vector columnN=new Vector () ;
- for (int j=1; j <= cols; j++){
- columnN.add(rs.getMetaData().getColumnClassName(j));
- }
- while (rs.next())
- {
- Vector columns = new Vector();
- for ( i=1; i<cols; i++ )
- {
- columns.addElement(rs.getObject(i+1)); // get the Object at i instead of i+1object
- } // end for
- data.addElement(columns);
- }
- System.out.print(s.getMaxRows());
- JTable table = new JTable(data, columnN);
- int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
- int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
- JScrollPane jsp = new JScrollPane(table,v,h);
- jsp.add(table);
- table.setFillsViewportHeight(true);
- // Add scroll pane to content pane
- contentPane.add(jsp, BorderLayout.CENTER);
- }
- catch (SQLException e){ System.out.print(e.toString()+"1");}
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new NewJFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton show_all;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement