CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2005
    Posts
    1

    ResultSet and JTable

    HI,
    am trying to get my data and display it in a JTable, but nothing is showing. No error and no nothing.
    I have initialised my JTable with a constructor 'a table model'.
    Here's the code

    public class FrameView extends JFrame {
    .
    .
    .

    MyModel mymodel=new MyModel();
    JTable jTable2= new JTable(mymodel);
    JScrollPane jScrollPane1 = new JScrollPane(jTable2);
    }
    class MyModel extends AbstractTableModel{
    connecting aconnecting=new connecting();
    Statement stmt;
    Connection connview;
    Vector cache;
    int colcount;
    String[] headers;

    public MyModel(){
    cache=new Vector();
    }

    public String getColumnName(int i) {
    return headers[i];
    }

    public int getColumnCount() {
    return colcount;
    }

    public int getRowCount() {
    return cache.size();
    }

    public Object getValueAt(int row, int col) {
    return ((String[]) cache.elementAt(row))[col];
    }

    public void myfunction(String myyear, String mymonth){
    String year, month,query;
    year=myyear;
    month=mymonth;
    try{
    this.connview= aconnecting.conn;
    aconnecting.connect();
    query="SELECT * FROM OPIS";
    stmt=connview.createStatement();
    ResultSet rs=stmt.executeQuery(query);
    ResultSetMetaData meta=rs.getMetaData();
    meta.getColumnCount();
    //we rebuild the headers array with the new column names
    headers=new String[colcount];
    for(int h=1;h<=colcount;h++){
    headers[h - 1] = meta.getColumnName(h);
    }
    while(rs.next()){
    String[] record=new String[colcount];
    for(int i=0;i<colcount;i++){
    record[i]=rs.getString(i+1);
    }
    cache.addElement(record);
    }
    }
    catch(SQLException se){
    System.err.print("Error:"+se.getMessage());
    cache = new Vector(); // blank it out and keep going.
    se.printStackTrace();
    }

    }
    }


    It cant work, sb help.

  2. #2
    Join Date
    Jun 2005
    Posts
    33

    Thumbs up Re: ResultSet and JTable

    Hi,
    Kindly refer the JTable samples that comes along with JDK.
    It contains a simple example of retrieving data from a database and displaying in the JTable depending upon the datatype.


    Happy coding!!!!

  3. #3
    Join Date
    Jul 2005
    Posts
    3

    Re: ResultSet and JTable

    check this
    http://java.sun.com/docs/books/tutor...able.html#data

    also,
    cache.addElement(record) must be inside the while loop
    connecting type really gives you a Connection object?

    try to follow the example at the link above, it is very clear

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured