Output of Java database query results
From: Beijing Marioter Information Consulting Co., Ltd. uses Java to develop database applications, often need to display query results on the user interface. We can use three classes such as Vector, JTable, AbstractTableModel to better solve this problem. Class Vector: Definitions as follows:
Public Class Vector Extends AbstractList
Implements List, Cloneable, Serializable {...}
Class JTable: JTable Components is a complicated small piece in the SWING component, belonging to the javax.swing package, which can display data in a two-dimensional table. Class JTABLE:
Defined as follows:
Public Class JTable Extends JComponent
Implements TableModellistener,
Scrollable, TableColumnModelliStener,
ListSelectronListener,
CelleditorListener, Accessible {...}
Class ABSTRACTTABLEMODEL:
Defined as follows:
Public Abstract Class AbstractTableModel Extends Object
Implements Tablemodel, Serializable {...}
Generate a specific TableModel as a subclass of AbstractTableMode, at least three methods must be achieved:
Public int getRowcount ();
Public int getColumncount ();
Public Object GetValueat (int Row, int column);
We can build a simple two-dimensional table (5 × 5):
TableModel Datamodel = new AbstractTableModel () {
Public int getColumncount () {Return 5;}
Public int getRowcount () {return 5;
Public Object GetValueat (int Row, INT COL)
{RETURN New Integer (Row * COL);
}
JTable Table = New JTABLE (DATAMODEL);
JscrollPane scrollpane = new jscrollpane (table);
Database and its connection method: We use Sybase databases, data inventory is placed in the database server. The path is: d: / worker, the database name is: worker.dbf. Has the following fields:
Field name type
WNO (employee) VARCHAR
WNAME (employee name) VARCHAR
Sex (gender) VARCHAR
Birthday Date Date
Wage (salary) Float
To connect this database, you need to use the class DriverManager in the java.sql package. This class is a utility class for managing JDBC drivers. It provides methods such as connecting, registration, undo driver, setting registration, and database access login email. The specific connection method is as follows:
Position, load, and link SybDriver classes.
Driver = "com.sybase.jdbc.sybdriver"; Sybdriver Sybdriver = (SybDriver)
Class.Forname (driver) .newinstance ();
Register the Sybdriver class.
Drivermanager.RegisterDriver (Sybdriver);
A SybConnection object is obtained.
User = "sa";
PASSWORD = ""
URL = "JDBC: SYBASE: TDS: 202.117.203.114: 5000 / Worker";
SybConnection connection =
(SybConnection) DriverManager.getConnection
(URL, User, Password);
After establishing the connection, you can perform the database query and change via the Statement interface.
Implementation:
Object declaration.
AbstractTableModel TM;
// Declare a class AbstractTableModel object
JTable jg_table; // Declare a class JTable object
Vector vect; // Declare a vector object
JScrollPane JSP; // Declare a rolling bar object
String Title [] = {"Workers", "Name",
"Gender", "Date of Date", "Wage"};
// Two-dimensional table name
Custom form.
Method in the abstract class AbstractTableModel object TM:
Vect = new vector (); // instantiate vector
TM = New AbstractTableModel () {
Public int getColumncount () {
Return Title.Length;} // Number of tables
Public int getRowcount () {
Return vect.size ();} // Number of lines
Public Object GetValueat (int Row, int column) {
IF (! vect.isempty ())
Return
(Vector) Vect.Elementat (ROW). Elementat (Column);
Else
Return NULL;} // Get attribute values in the cell
Public String getColumnName (int column) {
Return Title [Column];} // Set the table name
Public void setValueat
(Object Value, Int row, int column) {}
// Data model is not editable, this method is set to empty
Public Class getColumnClass (int C) {
Return getValueat (0, c) .getClass ();
} // Get column belongings
Public Boolean IscelleDitable (int Row, Int Column) {
Return False;} // Setting the cell is not editable, for the default implementation
}
Custom form:
JG_TABLE = New JTABLE (TM); // Generate your own data model
JG_Table.SetTooltiptext ("Show all query results");
// Set help prompt
JG_Table.SetAutoSizeMode (jtable.auto_resize_off);
/ / Set the table to adjust the size mode
Jg_table.setcellselectionenableNabled (False);
// Set the cell selection mode
Jg_Table.SetShowVerticalLines (TRUE); //
Set whether or not to display the divided line JG_Table.SetShowhorizontallines (TRUE);
JSP = new jscrollpane (jg_table); // Plus a rolling strobe for the table
Show the result of the query.
Connect the database: It has been given.
Database query:
Statement Stmt = Connection.createStatement ();
ResultSet RS = Stmt.executeQuery
("SELECT * from worker");
Show query results:
Vect.removeAllelements (); // Initialization vector object
TM.FireTablestructureChanged (); // Update Table Content
While (rs.next ()) {
Vector rec_vector = new vector ();
/ / From the result set of data placed in vector REC_Vector
REC_VECTOR.Addelement (rs.getstring (1));
REC_VECTOR.Addelement (rs.getstring (2));
REC_VECTOR.Addelement (Rs.getstring (3));
REC_VECTOR.Addelement (Rs.Getdate (4));
Rec_vector.addelement (new float (rs.getfloat (5));
Vect.addeElement (REC_VECTOR);
// Vector REC_Vector Add to Vector Vect
}
TM.fireTablestructureChanged ();
/ / Update the table, display the contents of vectors
There are two ways to record the precedent, and after the picture is recorded. There are two ways: if the software environment supports JDBC2.0, you can use rsprevoius () and rs.next () to get records, and then through the class jTextfield in setText () Method, display various field values. If JDBC2.0 is not supported, you can use the vector vector to remove the data in the JTABLE. Customize a pointer to record the location. When the pointer is added 1, take out the previous row data into the vector display; the pointer is reduced by 1, remove the next row data display. The display method is the same.