Visual Swing JTable control binding to SQL data sources in two ways in depth analysis

  • 2020-04-01 02:09:14
  • OfStack

In MyEclipse's visual Swing, there is the JTable control.
JTable is used to display and edit regular two-dimensional cell tables.
So how do you bind data from database SQL to JTable?
Here, two approaches are provided.
JTable constructor
By consulting the Java API, you can get two important constructors of JTable:
Data (Object [] [] rowData, Object [] columnNames)
A JTable is constructed to display the values in the two-dimensional array rowData with columnNames called columnNames.
This series (TableModel dm)
Construct a JTable, which is initialized using the data model dm, the default column model, and the default selection model.

Below, we use these two constructors to bind JTable to the SQL database.

Necessary preparations
There is a database for binding.
Drag in a Jtable control in visual Swing.

Method 1: bind with a two-dimensional array
The construction method used in this method is as follows:
Data (Object [] [] rowData, Object [] columnNames)
A JTable is constructed to display the values in the two-dimensional array rowData with columnNames called columnNames.
Building a two-dimensional array rowData
From the previous post, "precompiled," we were able to read the SQL database into a ResultSet.
(link: #) The review is as follows:


 
 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.executeQuery();
   //list = resultSetToList(rs);
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   //close();
  }
  return rSet;
 }

Note: the method name is just different from that of the previous blog.
Now, in order to convert the ResultSet into a two-dimensional array, we write the following method:

 
 public Object[][] resultSetToObjectArray(ResultSet rs) {
  Object[][] data = null;
  try { 
   rs.last();
   int rows = rs.getRow();
   data = new Object[rows][];  
   ResultSetMetaData md = rs.getMetaData();//Gets metadata for the recordset
   int columnCount = md.getColumnCount();//The 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 in the file userdao.java.
First instantiate the UserDAO in the Java file where Swing resides:

 UserDAO userDAO = new UserDAO();

SQL data can be converted into a two-dimensional array:

  Object[][] dataObjects = userDAO.resultSetToObjectArray(userDAO.getResultSet(
      "select id,username,password from t_userr", null));

Build columnNames called columnNames
This one is simple, just write the column name to the String array.

 String[] tableStrings = { "id", "username", "password" };

Model for building JTable:

 jTable1.setModel(new DefaultTableModel(dataObjects, tableStrings));

Overall code view:

Overall code view: < img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/201307310904142.jpg ">
Final JTable rendering:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/201307310904153.jpg ">


Method 2: the construction method used by Model binding is as follows:

This series (TableModel dm)
Construct a JTable, which is initialized using the data model dm, the default column model, and the default selection model.

Set the Model path

Go to the control panel of the JTable control and click model.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/201307310904154.jpg ">

Select the Model from the code.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/201307310904155.jpg ">

Method path to fill in the model: package name.Java file name. Method name.

It is recommended that methods be set to static methods for ease of invocation.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/201307310904166.jpg ">


Write the Model method

The method that writes the Model under the path you just set, notice that the return type is TableModel.

The method is written in much the same way as the 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;
 }

This series effect

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/201307310904167.jpg ">


Related articles: