可以在query中加入join链接两个表 之后通过返回的recordset做成一个tablemodel 下面是一个之前在网上找到的由recordset转tablemodel的类, import java.sql.*; import javax.swing.table.*; import javax.swing.event.*;/** * This class takes a JDBC ResultSet object and implements the TableModel * interface in terms of it so that a Swing JTable component can display the * contents of the ResultSet. Note that it requires a scrollable JDBC 2.0 * ResultSet. Also note that it provides read-only access to the results **/ public class ResultSetTableModel implements TableModel { ResultSet results; // The ResultSet to interpret ResultSetMetaData metadata; // Additional information about the results int numcols, numrows; // How many rows and columns in the table /** * This constructor creates a TableModel from a ResultSet. It is package * private because it is only intended to be used by * ResultSetTableModelFactory, which is what you should use to obtain a * ResultSetTableModel **/ ResultSetTableModel(ResultSet results) throws SQLException { this.results = results; // Save the results metadata = results.getMetaData(); // Get metadata on them numcols = metadata.getColumnCount(); // How many columns? results.last(); // Move to last row numrows = results.getRow(); // How many rows? }
/** * Call this when done with the table model. It closes the ResultSet and * the Statement object used to create it. **/ public void close() { try { results.getStatement().close(); } catch(SQLException e) {}; } /** Automatically close when we're garbage collected */ protected void finalize() { close(); } // These two TableModel methods return the size of the table public int getColumnCount() { return numcols; } public int getRowCount() { return numrows; } // This TableModel method returns columns names from the ResultSetMetaData public String getColumnName(int column) { try { return metadata.getColumnLabel(column+1); } catch (SQLException e) { return e.toString(); } } // This TableModel method specifies the data type for each column. // We could map SQL types to Java types, but for this example, we'll just // convert all the returned data to strings. public Class getColumnClass(int column) { return String.class; }
/** * This is the key method of TableModel: it returns the value at each cell * of the table. We use strings in this case. If anything goes wrong, we * return the exception as a string, so it will be displayed in the table. * Note that SQL row and column numbers start at 1, but TableModel column * numbers start at 0. **/ public Object getValueAt(int row, int column) { try { results.absolute(row+1); // Go to the specified row Object o = results.getObject(column+1); // Get value of the column if (o == null) return null; else return o.toString(); // Convert it to a string } catch (SQLException e) { return e.toString(); } } // Our table isn't editable public boolean isCellEditable(int row, int column) { return false; } // Since its not editable, we don't need to implement these methods public void setValueAt(Object value, int row, int column) {} public void addTableModelListener(TableModelListener l) {} public void removeTableModelListener(TableModelListener l) {} }
//我的是自己写的, //输入:connection对象,sql语句,由结果各字段名组成的name数组 //输出 JTable的对象 public JTable createTableFromSQL(Connection connection,String sql , Object[] name)throws Exception{ Statement stmt = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,// 对滚动不敏感,结果集可滚来滚去 ResultSet.CONCUR_READ_ONLY);// 只能够读取里边的内容, ResultSet rs = stmt.executeQuery(sql);//开始执行查询语句,结果存储在rs中 rs.last();// 指到定位到最后一行 int row = rs.getRow();// 获得总行数 rs.beforeFirst();// 定位到开始 ResultSetMetaData rsmd1 = rs.getMetaData();// 计算字段个数 int col = rsmd1.getColumnCount(); // 计算字段个数 Object a[][] = new Object[row][col];//确定表格存储数据的大小 JTable jTable = new JTable(a, name);//建立表格 for (int index = 0; rs.next(); index++)//使用查询返回的结果初始化表格 for (int j = 0; j < col; j++) a[index][j] = rs.getString(j + 1); return jTable; }
之后通过返回的recordset做成一个tablemodel
下面是一个之前在网上找到的由recordset转tablemodel的类,
import java.sql.*;
import javax.swing.table.*;
import javax.swing.event.*;/**
* This class takes a JDBC ResultSet object and implements the TableModel
* interface in terms of it so that a Swing JTable component can display the
* contents of the ResultSet. Note that it requires a scrollable JDBC 2.0
* ResultSet. Also note that it provides read-only access to the results
**/
public class ResultSetTableModel implements TableModel {
ResultSet results; // The ResultSet to interpret
ResultSetMetaData metadata; // Additional information about the results
int numcols, numrows; // How many rows and columns in the table /**
* This constructor creates a TableModel from a ResultSet. It is package
* private because it is only intended to be used by
* ResultSetTableModelFactory, which is what you should use to obtain a
* ResultSetTableModel
**/
ResultSetTableModel(ResultSet results) throws SQLException {
this.results = results; // Save the results
metadata = results.getMetaData(); // Get metadata on them
numcols = metadata.getColumnCount(); // How many columns?
results.last(); // Move to last row
numrows = results.getRow(); // How many rows?
}
/**
* Call this when done with the table model. It closes the ResultSet and
* the Statement object used to create it.
**/
public void close() {
try { results.getStatement().close(); }
catch(SQLException e) {};
} /** Automatically close when we're garbage collected */
protected void finalize() { close(); } // These two TableModel methods return the size of the table
public int getColumnCount() { return numcols; }
public int getRowCount() { return numrows; } // This TableModel method returns columns names from the ResultSetMetaData
public String getColumnName(int column) {
try {
return metadata.getColumnLabel(column+1);
} catch (SQLException e) { return e.toString(); }
} // This TableModel method specifies the data type for each column.
// We could map SQL types to Java types, but for this example, we'll just
// convert all the returned data to strings.
public Class getColumnClass(int column) { return String.class; }
/**
* This is the key method of TableModel: it returns the value at each cell
* of the table. We use strings in this case. If anything goes wrong, we
* return the exception as a string, so it will be displayed in the table.
* Note that SQL row and column numbers start at 1, but TableModel column
* numbers start at 0.
**/
public Object getValueAt(int row, int column) {
try {
results.absolute(row+1); // Go to the specified row
Object o = results.getObject(column+1); // Get value of the column
if (o == null) return null;
else return o.toString(); // Convert it to a string
} catch (SQLException e) { return e.toString(); }
} // Our table isn't editable
public boolean isCellEditable(int row, int column) { return false; } // Since its not editable, we don't need to implement these methods
public void setValueAt(Object value, int row, int column) {}
public void addTableModelListener(TableModelListener l) {}
public void removeTableModelListener(TableModelListener l) {}
}
//输入:connection对象,sql语句,由结果各字段名组成的name数组
//输出 JTable的对象
public JTable createTableFromSQL(Connection connection,String sql , Object[] name)throws Exception{
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,// 对滚动不敏感,结果集可滚来滚去
ResultSet.CONCUR_READ_ONLY);// 只能够读取里边的内容,
ResultSet rs = stmt.executeQuery(sql);//开始执行查询语句,结果存储在rs中
rs.last();// 指到定位到最后一行
int row = rs.getRow();// 获得总行数
rs.beforeFirst();// 定位到开始
ResultSetMetaData rsmd1 = rs.getMetaData();// 计算字段个数
int col = rsmd1.getColumnCount(); // 计算字段个数
Object a[][] = new Object[row][col];//确定表格存储数据的大小
JTable jTable = new JTable(a, name);//建立表格
for (int index = 0; rs.next(); index++)//使用查询返回的结果初始化表格
for (int j = 0; j < col; j++)
a[index][j] = rs.getString(j + 1);
return jTable;
}