The following describes how to bind a JTable control to an SQL data source in visual Swing.
In visual Swing of MyEclipse, there is a JTable control.
JTable is used to display and edit regular two-dimensional Unit tables.
So how to bind data in database SQL to JTable?
Here, two methods are provided.
JTable Constructor
By looking at Java APIs, we can get two important JTable constructor methods:
JTable (Object [] [] rowData, Object [] columnNames)
Construct a JTable to display the values in the two-dimensional array rowData. Its column name is columnNames.
JTable (TableModel dm)
Construct a JTable and initialize it using the data model dm, default column model, and default selection model.
Below, we use these two constructor methods to bind the JTable to the SQL database.
Necessary preparations
1. There is a database available for binding.
2. Drag a Jtable control in visual Swing.
Method 1: bind an array using a two-dimensional array
The constructor used by this method is:
JTable (Object [] [] rowData, Object [] columnNames)
Construct a JTable to display the values in the two-dimensional array rowData. Its column name is columnNames.
Construct a two-dimensional array rowData
Through the previous blog post "pre-compilation", we have been able to read the SQL database to the ResultSet.
Analysis of precompiled comprehensive applications in the PreparedStatement class of JDBC
Review:
The Code is as follows:
/** Query and return the record set */
Public ResultSet getResultSet (String SQL, Object [] objArr ){
GetConnection ();
Try {
PStatement = connection. prepareStatement (SQL, ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY );
If (objArr! = Null & objArr. length> 0 ){
For (int I = 0; I <objArr. length; I ++ ){
PStatement. setObject (I + 1, objArr [I]);
}
}
RSet = pStatement.exe cuteQuery ();
// List = resultSetToList (rs );
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
// Close ();
}
Return rSet;
}
[Note] It is only different from the method name of the previous blog ..
To convert the ResultSet into a two-dimensional array, write the following method:
The Code is as follows:
/** Convert the result set to Object [] [] */
Public Object [] [] resultSetToObjectArray (ResultSet rs ){
Object [] [] data = null;
Try {
Rs. last ();
Int rows = rs. getRow ();
Data = new Object [rows] [];
ResultSetMetaData md = rs. getMetaData (); // obtain the metadata of the record set
Int columnCount = md. getColumnCount (); // Number of Columns
Rs. first ();
Int k = 0;
While (rs. next ()){
System. out. println ("I" + k );
Object [] row = new Object [columnCount];
For (int I = 0; I <columnCount; I ++ ){
Row [I] = rs. getObject (I + 1). toString ();
}
Data [k] = row;
K ++;
}
} Catch (Exception e ){
}
Return data;
}
And save the two methods to the file UserDAO. java.
First instantiate UserDAO in the Java file where Swing is located:
The Code is as follows:
UserDAO userDAO = new UserDAO ();
SQL data can be converted into two-dimensional arrays:
The Code is as follows:
Object [] [] dataObjects = userDAO. resultSetToObjectArray (userDAO. getResultSet (
"Select id, username, password from t_userr", null ));
Build column name columnNames
This is relatively simple. You only need to write the column name to the String array.
The Code is as follows:
String [] tableStrings = {"id", "username", "password "};
Create a JTable Model:
The Code is as follows:
JTable1.setModel (new DefaultTableModel (dataObjects, tableStrings ));
Overall code View:
Overall code View:
Final JTable:
Method 2: bind a Model to this method. The following constructor is used:
JTable (TableModel dm)
Construct a JTable and initialize it using the data model dm, default column model, and default selection model.
Set the Model path
Go to the control panel of the JTable control and click model.
Select the Model from the code.
Enter the method path of the model: package name. Java file name. method name.
For convenient calling, we recommend that you set the method to a static method.
Write Model Method
Write the Model Method to the path you just set. Note that the return type is TableModel.
The method is similar to the method above and will not be repeated.
The Code is as follows:
Public static TableModel Member (){
String [] [] playerInfo = new String [80] [8];
BaseDAO bDao = new BaseDAO ();
String SQL = "select id, realName, username, sex, phone, email, vocation, city from jdbctest ";
String [] ss = {};
ArrayList <HashMap <Object, Object> list = bDao. Query (SQL, ss );
// BDao. AllArray (list );
Int I = 0, j = 0;
For (HashMap <Object, Object> maps: list ){
Set <Object> keysObjects = maps. keySet ();
For (Object kObject: keysObjects ){
PlayerInfo [I] [j] = maps. get (kObject). toString ();
J ++;
}
I ++;
J = 0;
}
String [] Names = {"id", "username", "sex", "phone", "vocation", "email", "realName", "city "};
DefaultTableModel dModel = new DefaultTableModel (playerInfo, Names );
Return (TableModel) dModel;
}
JTable Effect