Wednesday, 2 January 2013

Java Swing :- Window Application ( View Phase 3 JForm Table Window Using JDBC)

import java.sql.*;
import com.sun.org.apache.bcel.internal.generic.TABLESWITCH;
import java.util.ArrayList;
import model.MyTableModel;
import model.Person;

/**
 *
 * @author Amol
 */
public class SwingTable extends javax.swing.JFrame {

    ArrayList<Person> personList = new ArrayList<Person>();

    /** Creates new form SwingTable */
    public SwingTable() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb1", "root", "root");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from student_marks");
            System.out.println("rs = " + rs);
            if (rs != null) {
                while (rs.next()) {
                    String roll = rs.getString(1);
                    System.out.println("roll = " + roll);
                    String total = rs.getString(2);
                    System.out.println("total = " + total);
                    String obtain = rs.getString(3);
                    System.out.println("obtain = " + obtain);
                    personList.add(new Person(roll, total, obtain));
                }
            }
            System.out.println("personList.size() = " + personList.size());

        }
catch(Exception e)
{
    System.out.println("e = " + e);
    e.printStackTrace();
}


        initComponents();


        MyTableModel model = new MyTableModel(personList);
        jTable1.setModel(model);

    }

 public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SwingTable().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                  
}




No comments:

Post a Comment