Creating a Stored Procedure with a Prepared Statement
First, you must create a stored procedure using a prepared statement. I have not tested your code, but the stored procedure can be created using code like the following.
DROP PROCEDURE IF EXISTS execSql;
DELIMITER //
CREATE PROCEDURE execSql ()
BEGIN
SET @sql = CONCAT( ...
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
Invoking a MySQL Stored Procedure using JDBC
JDBC offers a CallableStatement
class you can use to invoke stored procedures. You may check a tutorial for Calling MySQL Stored Procedures from JDBC.
// use CALL with the name of the stored procedure
String query = "{CALL execSql()}";
// use CallableStatement to obtain the ResultSet
CallableStatement stmt = conn.prepareCall(query)
ResultSet rs = stmt.executeQuery();
Showing the result in a Swing JTable
To display the data, you may create a TableModel
with the ResultSet. There are many classes/libraries that you can use to fill a TableModel with the resultset (such as this DbUtils or this JDBCTableModel).
// execute the query
String query = "{CALL execSql()}";
CallableStatement stmt = conn.prepareCall(query)
ResultSet rs = stmt.executeQuery();
// fill the TableModel with the results
TableModel normalTableModel = DBUtils.resultSetToTableModel(rs);
// create the Swing Table
JTable swingTable = new JTable (transposedTableModel);
Transposing a TableModel
Sometimes you want to transpose (convert rows to columns) the results ot a Query. Although MySQL does not support PIVOT/UNPIVOT, you can transpose the Swing's TableModel. I think transposing the TableModel is the better (simpler) alternative.
Basically, you can create a TableModel wrapper that transpose the results. You may check the Spring TableModel and the JideSoft TransposeTableModel. The code for a TransposedTableModel
may be something like the following...
public TransposedTableModel extends AbstractTableModel {
private TableModel innerTableModel;
public TransposedTableModel(TableModel innerTableModel) {
this.innerTableModel = innerTableModel;
}
@Override
public int getRowCount() {
return innerTableModel.getColumnCount();
}
@Override
public int getColumnCount() {
return innerTableModel.getRowCount();
}
Override
public Object getValue(int row, int column) {
return innerTableModel.getValue(column, row);
}
};
Then, you can use a "normal" SQL, fill a TableModel with the result and the use the transposed TableModel.
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table1");
TableModel normalTableModel = DBUtils.resultSetToTableModel(rs);
TableModel transposedTableModel = new TransposedTableModel(normalTableModel);
JTable swingTable = new JTable (transposedTableModel);