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 yourResultSet
. This can be a simple wrap around aobject []
. - A
list & lt; Line & gt;
Archive, and subclassAbstractTableModel
can be supported by this collection. -
List by reading from the built in
ResultSet > & lt; Line & gt;
uses to populateSvingvokr
orBackground thread on (ie
doInBackground ()
within the law). Callswingwalker
topublish
the method to publish the event despaste thread (like every 100 lines) toline
. When theprocess
ofswingwalker
is called with the latest section of the rows, enter yourlist & lt; Line & gt; Add to
and the appropriate fireTableEvent
s due to updating the display. - In addition, use
ResultSetMetaData to define
class
of each columntablesmodel
Definition from them It will be sung correctly (which will not happen if you use 2Dobject [] []
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
Post a Comment