Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
我正在尝试在MySQL中将行动态转换为列并在Java / Swing中显示结果。

我用于将行转换为列的代码如下

SET @cols = NULL;
SET @sql = NULL;

SELECT
  GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(Name = ''',
      Name, ''', Amount , NULL)) AS ', '''', Name , '''')
  ) INTO @cols
FROM table1;


SET @sql = CONCAT('SELECT
  Date, ', @cols , ', SUM(Amount) AS Total
FROM table1
GROUP BY Date;');

prepare stmt
FROM @sql;

execute stmt;


尽管我在MySQL中获得了结果,但我不知道如何使用该代码并在Java Swing中显示结果。

最佳答案

使用准备好的语句创建存储过程

首先,您必须create a stored procedure using a prepared statement。我尚未测试您的代码,但是可以使用如下代码创建存储过程。

DROP PROCEDURE IF EXISTS execSql;
DELIMITER //
CREATE PROCEDURE execSql ()
BEGIN

  SET @sql = CONCAT( ...

  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
END //
DELIMITER ;


使用JDBC调用MySQL存储过程

JDBC提供了一个CallableStatement类,可用于调用存储过程。您可以查看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();


在Swing JTable中显示结果

要显示数据,您可以使用ResultSet创建一个TableModel。您可以使用许多类/库来用结果集填充TableModel(例如this DbUtilsthis 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);


转置TableModel

有时您想要转置查询的结果(将行转换为列)。尽管使用MySQL does not support PIVOT/UNPIVOT,但您可以转置Swing的TableModel。我认为转置TableModel是更好(更简单)的选择。

基本上,您可以create a TableModel wrapper进行转置结果。您可以检查Spring TableModelJideSoft TransposeTableModelTransposedTableModel的代码可能类似于以下内容...

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);
  }
};


然后,您可以使用“普通” SQL fill a TableModel with the result,并使用转置后的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);

10-08 05:21