The Artima Developer Community
Sponsored Link

Java Answers Forum
Populate JTable

2 replies on 1 page. Most recent reply: Jan 19, 2004 5:25 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Samuel Quarcoo Kofi

Posts: 2
Nickname: kqsam
Registered: Jan, 2004

Populate JTable Posted: Jan 15, 2004 8:40 AM
Reply to this message Reply
Advertisement
I have Query of which I will like to populate the resultset in a table(JTable).

You know,it is given me sleepless night.

Thanks.


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Populate JTable Posted: Jan 19, 2004 5:24 AM
Reply to this message Reply
get the rows from result set and set the datavector to JTable.

Did u go thru the APIs?

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class SimpleCounter extends HttpServlet {
 
  int count = 0;
 
  public void doGet(HttpServletRequest req, HttpServletResponse res) 
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    count++;
    out.println("Since loading, this servlet has been accessed " +
                count + " times.");
  }
}
 

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Populate JTable Posted: Jan 19, 2004 5:25 AM
Reply to this message Reply
// TableModel definition
String[] tableColumnsName = {"col 1","col 2","col 3"}; 
DefaultTableModel aModel = (DefaultTableModel) aTable.getModel();
aModel.setColumnIdentifiers(tableColumnsName);
 
// the query
ResultSet rs = statement.executeQuery("select col1,col2,col3 from mytable");
 
// Loop through the ResultSet and transfer in the Model
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int colNo = rsmd.getColumnCount();
while(rs.next()){
 Object[] objects = new Object[colNo];
 // tanks to umit ozkan for the bug fix!
 for(int i=0;i<colNo;i++){
  objects[i]=rs.getObject(i+1);
  }
 aModel.addRow(objects);
}
aTable.setModel(aModel);
 
 



Sorry my last post was for some other question

Flat View: This topic has 2 replies on 1 page
Topic: generic java programming questions Previous Topic   Next Topic Topic: RDBMS Engine and java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use