Results 1 to 3 of 3
  1. #1
    elizabeth is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Problems with Jtable connected to data base

    I'm new in java and I have a problem with a Jtable.
    I'm doing a windows that has a Jtable in a JScrollPane, it should shows information from data base, but it doesn't

    this is the code
    Java Code:
    public
    
    class Students extends JDialog { 
    
    private static final long serialVersionUID = 1L; 
    
    private JPanel jContentPane = null; 
    
    private JButton jButton1 = null; 
    
    private JScrollPane jScrollPane = null; 
    
    private JTable jTable = null; 
    
    
    /** 
    
    * @param owner 
    
    */ 
    
    public Students () { 
    
    super(); 
    initialize();
    
    }
    
    
    private void initialize() { 
    
    this.setSize(486, 230); 
    
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    
    
    this.setContentPane(getJContentPane()); 
    
    this.setLocationRelativeTo(null); 
    }
    
    
    
    
    private JPanel getJContentPane() { 
    
    if (jContentPane == null) { 
    
    jContentPane = new JPanel(); 
    
    jContentPane.setLayout(null); 
    
    jContentPane.add(getJButton1(), null); 
    
    jContentPane.add(getJScrollPane(), null); 
    }
    
    
    return jContentPane; 
    }
    
    
    
    private JButton getJButton1() { 
    
    if (jButton1 == null) { 
    
    jButton1 = new JButton(); 
    
    jButton1.setBounds(new Rectangle(174, 168, 100, 25)); 
    
    jButton1.setText("Exit"); 
    
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
    
    public void actionPerformed(java.awt.event.ActionEvent e) { 
    Students .
    
    this.dispose(); 
    }
    
    });
    
    }
    
    
    return jButton1; 
    }
    
    
    private JScrollPane getJScrollPane() { 
    
    if (jScrollPane == null) { 
    
    jScrollPane = new JScrollPane(); 
    
    jScrollPane.setBounds(new Rectangle(15, 16, 453, 131)); 
    
    jScrollPane.setViewportView(getJTable()); 
    }
    
    
    return jScrollPane; 
    }
    
    
    
    
    private JTable getJTable() { 
    
    if (jTable == null) { 
    
    //jTable = new JTable(); 
    DefaultTableModel model = 
    
    new DefaultTableModel(); 
    JTable jTable = 
    
    new JTable(model); 
    
    modelo.addColumn(
    
    "Name"); 
    model.addColumn(
    
    "Number"); 
    
    ResultSet rs;
    
    
    try { 
    rs = Connection.
    
    st.executeQuery("SELECT name,nro FROM student order by name,nro"); 
    
    while (rs.next()) 
    {
    
    
    
    Object [] row = 
    
    new Object[2]; 
    
    
    
    for (int i=0;i<2;i++) 
    row [i] = rs.getObject(i+1); 
    
    
    
    model.addRow(row ); 
    
    }
    
    } 
    
    catch (SQLException ex) { 
    JOptionPane.showMessageDialog(
    
    null, "Error  "+ex, null, JOptionPane.ERROR_MESSAGE); 
    }
    
    }
    
    
    return jTable; 
    }
    
    }

  2. #2
    levent Guest

    Default

    Which exception do you get? We can't test this code on our side since we don't have your database. We can only help you to track the error if any.

  3. #3
    fred is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default

    write this:
    Java Code:
    this.getContentPane().add(getJContentPane() );
    instead of:
    Java Code:
    this.setContentPane(getJContentPane());

Similar Threads

  1. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM
  2. Replies: 1
    Last Post: 08-06-2007, 05:38 PM
  3. problems with Instance Data
    By paty in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 05:45 PM
  4. 2 threads sharing a data base connection
    By Ed in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 04:41 AM
  5. Connection to data base
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 06-07-2007, 04:55 AM

Posting Permissions

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