Document directory
- Jtable Constructor
- Necessary preparations
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.
The jtable constructor can obtain two important constructor methods of jtable by referring to Java APIs: 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 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.
Build a two-dimensional array rowdata through the previous blog post "pre-compilation", we have been able to read the SQL database to the resultset. For more information, see http://blog.csdn.net/jueblog/article/details/9622159:
/** Query and return 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:
/** 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 (); // get the metadata int columncount = md of the record set. 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:
UserDAO userDAO = new UserDAO();
SQL data can be converted into two-dimensional arrays:
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.
String[] tableStrings = { "id", "username", "password" };Create a jtable model:
jTable1.setModel(new DefaultTableModel(dataObjects, tableStrings));
Overall code View:
Final jtable:
Method 2: bind with Model
The constructor used in this method is 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 to enter 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. The method for writing the Model Method to the specified path. Note that the return type is tablemodel. The method is similar to the method above and will not be repeated.
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