在JDBC

时间:2017-05-28 10:38:08

标签: java mysql jdbc

我有以下SQL语句,它有许多子查询:

SELECT name as 'Name', val as 'Comments', lcnt as 'Likes' FROM (
SELECT name, userEmail, COUNT(userEmail) as lcnt, val FROM metum_app.like as L
    INNER JOIN (
        SELECT B.email, B.name, val FROM (
            SELECT U.email, U.name, cnt as val
                FROM metum_app.user as U
                    INNER JOIN (
                        SELECT userEmail as em, photoNumber pn, COUNT(userEmail) as cnt
                            FROM metum_app.comment as C GROUP BY userEmail
                        ) as T 
                    ON U.Email = T.em
            ) as B
        WHERE val IN (SELECT MAX(cnt) -- I had to do this because OMG DBs ARE STUPID
                FROM metum_app.user as W
                    INNER JOIN (
                        SELECT userEmail as em, photoNumber pn, COUNT(userEmail) as cnt
                            FROM metum_app.comment as C GROUP BY userEmail
                        ) as Y 
                    ON W.Email = Y.em
        )
    ) as W ON W.Email = L.userEmail
) as X

这是我的应用程序未能提供正确结果的唯一声明。我将此作为字符串加载到程序中,并按原样执行:

public ResultSet query(Connection connection, String s) throws SQLException {
    PreparedStatement st = connection.prepareStatement(s);
    st.execute();
    return st.getResultSet();
}

其他任何工作正常,但这不是。我尝试过添加" allowMultiQueries = true"无济于事。我也查看了相关问题,但无法将其转化为工作状态。大多数类似的问题似乎只是问了一堆INSERT语句,而不是像这样的语句。

从ResultSet生成表的代码:

// from https://stackoverflow.com/questions/11734561/how-to-view-database-resultset-in-java-swing
public class GUI_DatabaseResultTable {
    private JScrollPane table = null;

    public GUI_DatabaseResultTable(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++)
            columnNames.add(metaData.getColumnLabel(column));

        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }

        table = new JScrollPane(new JTable(new DefaultTableModel(data, columnNames)));
    }

    public void show(GUI_SubWindow toShow, int i) {
        JFrame newframe = new JFrame();
        newframe.setSize(toShow.getWidth(), toShow.getHeight());
        newframe.add(table);
        newframe.addWindowListener(new GUI_WindowClose_DatabaseResultTable(toShow, i));
        newframe.setVisible(true);
    }
}

感谢任何帮助。

编辑:运行此语句时,不会抛出SQLExceptions或任何类型的错误。结果我得到一张空白的桌子。上述声明适用于MySQL Workbench,因此确认其工作正常。

0 个答案:

没有答案