How to show data in database with a JTable?

How to display details in database in frame using JTable??? please help?

  • my code is : private void displayPrisoners() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDri… Connection conn = DriverManager.getConnection("jdbc:odbc:P… Statement state=conn.createStatement(); String sql="Select * from Prisoner"; ResultSet rs=state.executeQuery(sql); FillTable model = new FillTable(rs); tblPrisoners=new JTable(model); rs.close(); state.close(); conn.close(); } catch(Exception e) { System.out.print(e.toString()); } but i don't know how to display the data in a Table.

  • Answer:

    YAH. I GOT UR QUES. HERE U NEED TO CREATE A PACKAGE OF ABSTRACTTABLEMODEL AND THEN IMPORT IT TO THE MAIN CLASS TO BE USED O DIPLAY THE DETAILS. THE PACKAGE CCAN BE CEATED AS: public class FillTable extends AbstractTableModel{ private Vector<String[]> RowData=new Vector<String[]>(); private String[] columns=new String[0]; public FillTable() { try{ setResultset(_rs); } catch(Exception e){ System.out.print(e.toString()); } } public void setResultset( results) { try { ResultSetMetaData metaDt=results.getMetaData(); int cols=metaDt.getColumnCount(); columns=new String[cols]; for(int i=0;i<cols;i++) { columns[i]=metaDt.getColumnLabel(i+1); } String[] rowData; while(next()) { rowData=new String[cols]; for(int i=0;i<cols;i++) { rowData[i]=results.getString(i+1); } RowData.addElement(rowData); } fireTableChanged(null); } catch(Exception e) { } } public int getRowCount() { return RowData==null?0:RowData.size(); } public int getColumnCount() { return columns.length; } public String getValueAt(int rowIndex, int columnIndex) { return RowData.elementAt(rowIndex)[columnIndex]… } public String getColumnName(int col) { return columns[col]==null?" } } AFTER THIS YOU CAN IMPORT IT TO THE DISPLAY CLASS.

RUNJHUN at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.