Table format output of Query results in Java database programming

xiaoxiao2021-03-06  18

When using Java development database application systems, you often need to display query results on the user interface. Since Sun's JDK1.x Development Kit is not a visualized integrated development environment (IDE), it is not easy to easily display the query results in a table in DBGRID. Therefore, you can only rely on your own code. In practical applications, we can use three classes such as Vector, JTable, AbstractTableModel to better solve this problem. Here, in detail, implement the method. I. Class Vector, Class JTABLE and class AbstractTableModel Description: 1. Class Vector: Class Vector is a historical collection class of Java, belonging to a java.util package. It is packaged with the heterogeneous chain table and array hybrid, with two features: * Vector is heterogeneous, no requirement of each element, and multiple object types can be mixed in vector; * vector is an array hybrid because They can increase during increasing elements. Its isomer is in line with the characteristics of different types of properties in database records, and its dynamics also matches database queries, and the result set records a number of species. The class vector is defined as follows: public class vector extend, cloneable, serializable {...} implements the search, new, delete, etc. of the vector member. Such as: add (object obj) can easily add an object; Get (int index) can easily get an object in the vector; Remove (Object Obj) can easily delete an object in the vector. 2, class JTable: JTable component is a complicated small piece in the Swing component, belonging to the Javax.swing package, which can display data in the form of a two-dimensional table. Class JTable is defined as follows: public class JTable extends JComponent implements TableModelListener, Scrollable, TableColumnModelListener, ListSelectionListener, CellEditorListener, Accessible {...} class JTable has the following characteristics when the display data: * customizable: can customize the display and editing status data; * Isomerism: You can display different types of data objects, even complicated objects such as color, icons; * Simple: You can easily establish a two-dimensional table in a default. Its customizable can meet the requirements of different users and occasions, and isomerism is also characterized by the characteristics of the attribute types of database access results. Class JTABLE provides an extremely rich two-dimensional table operation method, such as setting an editing state, display mode, selecting row column, etc., will not be described here. Before using class JTable display data, custom models, unit drawers, or unit editing must be generated depending on the situation. Class ABSTRACTLISTMODEL is used to customize user's own data model, which is later described later. The TableCellRenderer interface is used to customize the unit drawer, the TableCelleditor interface is used to customize the unit editor, which is mainly used for the processing of color objects, and is not used in the example, not excessive description. 3, class AbstractTableModel: Class AbstractTableModel is an abstract class, not fully implemented, can not instantiate, implement methods in the program during use. It is part of javax.swing.table.

Class definitions are as follows: Public Abstract Class AbstractTableModel Extends Object Implements TableModel, Serializable {...} class AbstractTableModel provides the default implementation of most of the TableModel interface. The TableModel interface defines the basic data structure of the JTABLE. Users must generate their own data models, which can be met by implementing all the methods in the TableModel interface, but the function of the management listener is common for all data models, so the class AbstractTableModel is defined in javax.swing.table. This work. It manages the audience table and provides a convenience of generating TableModeEvents incidents and entrusting the audience. To generate a specific TableModel as the subclass of AbstractTableMode, at least three methods must be achieved: public int getRowcount (); public int getColumnCount (); public object getValueat (int Row, int column); to this, we can build a Simple two-dimensional table (5 × 5), the implementation method is as follows: TableModel Datamodel = new AbstractTableModel () {public int getColumnCount () {return 5;} public int achievecount () {Return 5;} public object getValueat (int Row, int COL) {RETURN NEW INTEGER (ROW * COL);}}; jtable table = new jtable (datamodel); jscrollpane scrollpane = new jscrollpane (table); two databases and connection method: Example adopts Sybase database system, data inventory In the database server. The path is: d: / worker, the database name is: worker.dbf. With the following fields: Field name type WNO (employee) VARCHAR WNAME (employee name) VARCHAR SEX (Sex) VARCHAR BIRTHDAY DATE WAGE FLOAT To connect this database, you need to use the class DriverManager in 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: Step 1: Positioning, loading, and link Sybdriver class; driver = "com.sybase.jdbc.sybdriver"; sybdriver sybdriver = (sybdriver) Class.Forname (DRiver) .newinstance (); second step 2 : Register Sybdriver class; DriverManager.RegisterDriver (Sybdriver); Step 3: A SybConnection object reference.

User = "sa"; password = ""; url = "JDBC: Sybase: TDS: 202.117.203.114: 5000 / wor"; SybConnection Connection = (SybConnection) DriverManager.getConnection (URL, User, Password); after the establishment To make a query and change of the database via the Statement interface. Third, implementation method: limited to space, only given core code, package introduction, interface processing, variable definition, etc., etc. Step 1: Object declaration. AbstractTableModelTM; // 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", "Staff name", "gender", "date of birth", "salary"}; // two-dimensional table name, second step: custom form. 1. Implement the method in the abstract class AbstractTableModel object in TM: Vect = new vector (); // instantiate vector TM = new AbstractTableModel () {public int getColumnCount () {return Title.Length;} // get the table number PUBLIC INT getRowCount () {Return vect.size ();} // get the number of tables PUBLIC OBJECT GETVALUEAT (INT ROW, INT Column) {if (! vect.isempty ()) Return ((Vector) Vect.Elementat (ROW) ) .Elementat (color); Else Return Null;} // acquired attribute value in cell PUBLIC STRING GETCOLUMNNAME (INT Column) {Return Title [Column];} // Setting Table Column Name Public Void SetValueat (Object Value, Int Row, int column) {} // Data model is not edited, this method is set to empty public class getColumnClass (int C) {return getValueat (0, c) .getClass ();} // Number of columns PUBLIC Boolean IsCelleditable (int ROW, INT Column) {RETURN FALSE;} // Setting cell is not edited, for the default implementation}; 2, custom form: JG_TABLE = New JTABLE (TM); // Generate your own data model JG_Table.SetTooltiptext "Show all query results"); // Setting help prompt jg_table.setAutoResizeMode (jtable.auto_resize_off); // Setting Table Adjust Size Mode JG_Table.SetcellSelectionNabled (false); // Set cell selection method JG_Table.SetShowverticalLines (TRUE); // Set whether to display the divided line of the cell line JG_Table.SetShowhorizontallines (TRUE); JSP = New JScrollpane (JG_TABL e); // give the table, the third step of the rolling bumper: display the query results. 1. Connect the database: The second part has been given.

转载请注明原文地址:https://www.9cbs.com/read-41823.html

New Post(0)