Jtable with database manually in netbeans.


package Demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author NAVJYOTI
 */
public class Jtablee extends javax.swing.JFrame {

    /**
     * Creates new form Jtablee
     */
    public Jtablee() {
        initComponents();
        set();
    }
    private int getsize()
{
int count=0;
try

{Class.forName("com.mysql.jdbc.Driver");
            java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3305/commity","root","root");
Statement stmt=con.createStatement();
String query="select * from group1";
ResultSet rs= stmt.executeQuery(query);
while(rs.next())
{
count++;
}
return count;


}
catch(Exception e)
{
return count;
}


}
public void set()
{
      try
        {
           Class.forName("com.mysql.jdbc.Driver");
            java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3305/commity","root","root");
    String sql="select * from group1";
    java.sql.PreparedStatement pst=con.prepareStatement(sql);
    DefaultTableModel tm33 = (DefaultTableModel) jTable1.getModel();
        tm33.setRowCount(0);
    ResultSet rs = pst.executeQuery(sql);
     int i=0;
  while(rs.next())
  {
Vector v2 = new Vector();
//       jTable1.getModel().setValueAt(rs.getString(1), i, 0);
//       jTable1.getModel().setValueAt(rs.getDate(2),i, 1);
//       jTable1.getModel().setValueAt(rs.getDate(3),i, 2);
//       jTable1.getModel().setValueAt(rs.getDouble(4),i, 3);
//       jTable1.getModel().setValueAt(rs.getDouble(5),i, 4);
//       jTable1.getModel().setValueAt(rs.getInt(6),i, 5);
//       i++;
v2.addElement(rs.getString(1));
   v2.addElement(rs.getDate(2));
   v2.addElement(rs.getDate(3));v2.addElement(rs.getDouble(4));
   v2.addElement(rs.getDouble(5));
   v2.addElement(rs.getInt(6));
   tm33.addRow(v2);
  }
  //   int rows = getsize()-1;
//   System.out.println(""+rows);
//   for(int row = 0; row<rows ; row++){
con.close();
pst.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
            JOptionPane.showMessageDialog(null, e);
        }
}
    /**
     * 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();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Group_Number", "Starting_Date", "Ending_Date", "Cost", "Installment", "No_of_Members"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                      

    /**
     * @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(Jtablee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Jtablee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Jtablee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Jtablee.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 Jtablee().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                  
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                
}

Popular posts from this blog

Simple Sign up design. Android.

Cart page design in android.

Set Date on jDateChooser and retrieve record from database.