Results 1 to 3 of 3
  1. #1
    tpyq is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Default [SOLVED] Cant display data in jTable

    I tried to display data from database to jTable. This is what i try and i cant display the data. Can anyone tell me where is the problem?




    Java Code:
    public class NewJPanel extends javax.swing.JPanel {
    
        static JTable myTable;
    
        /** Creates new form NewJPanel */
        public NewJPanel() {
            initComponents();
            myTable = new JTable(3, 2);
            JScrollPane myPane = new JScrollPane(myTable,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            add(myPane);
            myTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
        }
    
        public static void main(String args[]) throws SQLException {
            JFrame myFrame = new JFrame("Table");
            myFrame.getContentPane().add(new NewJPanel());
            myFrame.setVisible(true);
            myFrame.pack();
    
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (java.lang.ClassNotFoundException e) {
                System.err.print("ClassNotFoundException: ");
                System.err.println(e.getMessage());
            }
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jigsaw puzzle", "root", "MP037");
    
            Statement stmt = null;
            ResultSet rs = null;
            String cuser = "SELECT user_id, score_easy FROM score ";
            stmt = connection.createStatement();
            rs = stmt.executeQuery(cuser);
    
            int li_row = 0;
            while (rs.next()) {
                myTable.setValueAt(rs.getString(1), li_row, 0);
                myTable.setValueAt(rs.getString(2), li_row, 1);
                li_row++;
            }
    
        }

  2. #2
    sunilpatel28 is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default use any tablemodel to display data in table

    public class NewJPanel extends javax.swing.JPanel {

    static JTable myTable;

    /** Creates new form NewJPanel */
    public NewJPanel() {
    initComponents();
    myTable = new JTable(3, 2);
    JScrollPane myPane = new JScrollPane(myTable,
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    add(myPane);
    myTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
    }

    public static void main(String args[]) throws SQLException {
    JFrame myFrame = new JFrame("Table");
    myFrame.getContentPane().add(new NewJPanel());
    myFrame.setVisible(true);
    myFrame.pack();

    try {
    Class.forName("com.mysql.jdbc.Driver");
    } catch (java.lang.ClassNotFoundException e) {
    System.err.print("ClassNotFoundException: ");
    System.err.println(e.getMessage());
    }
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jigsaw puzzle", "root", "MP037");

    Statement stmt = null;
    ResultSet rs = null;
    String cuser = "SELECT user_id, score_easy FROM score ";
    stmt = connection.createStatement();
    rs = stmt.executeQuery(cuser);
    DefaultTableModel resultData = null;
    final Object[] columnNames=new String[] {"user_id","score_easy"};

    resultData = new DefaultTableModel(columnNames,0);

    int li_row = 0;
    while (rs.next()) {
    resultData.addRow(new Vector());
    resultData.setValueAt(rs.getString(1), li_row, 0);
    resultData.setValueAt(rs.getString(2), li_row, 1);
    li_row++;
    }
    myTable..setModel(resultData);
    }

  3. #3
    vpkathirvel is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default

    Follow this code it will work perfectly

    public void SearchMonthSD(){
    Vector columnNames = new Vector();
    Vector data = new Vector();
    try
    {
    Viewingtstack sv =new Viewingtstack();
    String sql = "SELECT *FROM Stackdetails where (Datediff(mm,ProductIndate,CURRENT_TIMESTAMP))=0";//Writwe your Qurey
    Statement smt = con.createStatement();
    ResultSet rs = smt.executeQuery( sql );
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    // System.out.println("OK Ok OK");
    for (int i = 1; i <= columns; i++)
    {
    columnNames.addElement( md.getColumnName(i) );
    System.out.println(columnNames);
    }

    while (rs.next())
    {
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++)
    {
    row.addElement( rs.getObject(i) );
    }
    data.addElement( row );
    System.out.println(data);
    }
    //Here you have to create JTable
    rs.close();
    smt.close();
    }
    catch(Exception e)
    {
    System.out.println( e );
    }
    }
    Last edited by vpkathirvel; 12-09-2008 at 02:44 PM.

Similar Threads

  1. JButton to display JTable
    By Nemesis777 in forum New To Java
    Replies: 0
    Last Post: 12-08-2008, 12:16 PM
  2. Display XML in JTable
    By boy22 in forum XML
    Replies: 2
    Last Post: 12-07-2008, 06:03 PM
  3. Replies: 0
    Last Post: 12-04-2008, 05:39 PM
  4. Problems getting JTable to display information
    By Gatts79 in forum AWT / Swing
    Replies: 2
    Last Post: 11-08-2008, 02:35 AM
  5. display rows in jtable
    By osval in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •