java - How to fill data in a JTable with database? -


I want to display a JTable which displays the data from the database table as it is.

Up till now, I have used Jettbble to display data from the object [] [].

I know that one way to display data is to convert the database table into an object [] [] but someone else is easier and more powerful and flexible

I would recommend taking the following approach:

  • a line class To display the row read from your ResultSet . This can be a simple wrap around a object [] .
  • A list & lt; Line & gt; Archive, and subclass AbstractTableModel can be supported by this collection.
  • List by reading from the built in ResultSet & lt; Line & gt; uses to populate Svingvokr or Background thread on (ie doInBackground () within the law). Call swingwalker to publish the method to publish the event despaste thread (like every 100 lines) to line . When the process of swingwalker is called with the latest section of the rows, enter your list & lt; Line & gt; Add to and the appropriate fire TableEvent s due to updating the display.
  • In addition, use ResultSetMetaData to define class of each column tablesmodel Definition from them It will be sung correctly (which will not happen if you use 2D object [] [] array).

The advantage of this approach is that UI older ResultSet will not lock while processing s, and that the results are sequentially updated due to increased Will go

EDIT

Example code below:

  / ** * Simple cover around object [] ResultSet Represent a line from * / Private class line {Private last item [] value; Public line (object [] value) {this.values ​​= values; } Public int getSize () {return values.length; } Public object getValue (int i) {return value [i]; }} // Telecommunication Implementation which will be implemented by swingwalker Public Class Rijltsetetbl model Aebtratebl model (private final Srtiseting metadata Arsmdi; private final list & lt; ro & gt; rows; public Prinamsetset model (resulting metadata Arsmdi) {this.rsmd = rsmd; This.rows = new Arrelist & lt; line & gt; ();} public int GetRowCount () {return rows.size ();} public int get Colmkount () {return rsmd.getColumnCount ();} public object getValue (int row, int column) {return lines. Line (line) KgetValue (column);} public string getColumnName (int col) {return rsmd.getColumnName (col - 1); // ResultSetMetaData columns not indexed from 1, not 0} public class & lt ;? & Gt; GetColumnClass (int col) {// TODO: SQL type (Int) to change, returned to Java class by ResultSetMetaDatakgetType (COLA).}} // Swingvrkr execution Svingvorkr & lt; zero line & gt; () {Public Voice Do In-Background () {// TODO: Process as a result and create rows. Publish calls for each N rows ()} protected void process (line ... selected) Add {// TODO: ResultSetTableModel list and fire TableEvent. }}. Execute ();  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -