Sunday 3 July 2011

Adding database record in Table components in java

package mydatabase;
import java.sql.*;
/**
 *
 * @author user
 */
public class JFrameRecord extends javax.swing.JFrame {
 ClassFun cf=new ClassFun();
    /** Creates new form JFrameRecord */
    public JFrameRecord() {
        initComponents();
        cf.Connect();
    }
    /** 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() {
        jLabel2 = new javax.swing.JLabel();
        JtextId = new javax.swing.JTextField();
        jButtonSearch = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableRecord = new javax.swing.JTable();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel2.setText("College ID");
        JtextId.setFont(new java.awt.Font("Times New Roman", 0, 14));
        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });
        jTableRecord.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Roll No", "Student Name", "Father's Name", "Course", "Year", "Semester"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableRecord);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 444, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(28, 28, 28)
                        .addComponent(JtextId, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addComponent(jButtonSearch)))
                .addContainerGap(139, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(JtextId, 0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonSearch))
                    .addComponent(jLabel2))
                .addGap(56, 56, 56)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(233, 233, 233))
        );
        pack();
    }// </editor-fold>
    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here
        try{
        cf.smt=cf.con.createStatement();
         cf.res = cf.smt.executeQuery("SELECT * FROM APP.STUDB where ID="+Integer.parseInt(JtextId.getText()));
         while (cf.res.next())
         {
            jTableRecord.setValueAt(cf.res.getInt("ID"),0,0);
            jTableRecord.setValueAt(cf.res.getString("SNAME"),0,1);
            jTableRecord.setValueAt(cf.res.getString("FNAME"),0,2);
            jTableRecord.setValueAt(cf.res.getString("COURSE"),0,3);
            jTableRecord.setValueAt(cf.res.getInt("SYEAR"),0,4);
            jTableRecord.setValueAt(cf.res.getString("SEM"),0,5);
           
//      
//          jTableRecord.getColumn(1).setHeaderValue(cf.res.getInt("ID"));
//             // cf.res.getInt("ID");
//            jTableRecord.setValueAt(cf, WIDTH, WIDTH).getColumn(2).setsetValueAt(cf.res.getString("SNAME"));
//            jTableRecord.getColumn(3).setHeaderValue(cf.res.getString("FNAME"));
//            jTableRecord.getColumn(4).setHeaderValue(cf.res.getString("COURSE"));
//            jTableRecord.getColumn(5).setHeaderValue(cf.res.getString("SYEAR"));
//            jTableRecord.getColumn(6).setHeaderValue(cf.res.getString("SEM"));
//          
          
         }
         cf.smt.close();
            cf.res.close();
        }
        catch(SQLException e)
        {
//            cf.smt.close();
//            cf.res.close();
            System.err.println(e.getMessage());
        }
    }                                            
    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify
    private javax.swing.JTextField JtextId;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableRecord;
    // End of variables declaration
}

2 comments:

  1. first u create a class that contain connection to database

    ReplyDelete
  2. create a frame that contain a id to search in database table Studb and add a button.
    add jTable component in Frame

    ReplyDelete