0

I have the following SQL statement that has many subqueries:

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

This is the only statement that my application is failing to give proper results. I'm loading this as a string into the program, and executing it as such:

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

Anything else works fine, but this doesn't. I've tried adding "allowMultiQueries=true" to no avail. I also looked at relevant questions, but couldn't manage to transform it into a working state. Most of the similar questions seem to ask just a bunch of INSERT statements, and not statements like these.

Code that generates table from 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);
    }
}

Any help is appreciated.

EDIT: No SQLExceptions or any kind of error is thrown when this statement is run. I get a blank table as a result. The said statement works in MySQL Workbench, so it's confirmed to be working fine.

James Z
  • 12,209
  • 10
  • 24
  • 44
SenselessCoder
  • 1,139
  • 12
  • 27
  • 3
    I've used JDBC to run `SELECT` queries substantially longer than this without problems. It may help you get useful answers if you [edit] your question to explain what goes wrong exactly. Do you get a SQL error? Do you get an empty result set? Is the result set you get somehow wrong? Also, what DBMS are you using? You've given us two tags [mysql] and [sql-server]. – O. Jones May 28 '17 at 10:42
  • What about the code handling the resultset? A – Jan May 28 '17 at 10:43
  • 1
    You don't need multi queries. This is just one query .. a complex one, but just one. – O. Jones May 28 '17 at 10:43
  • Since your question doesn't mention SQL Server anywhere, I removed it. – James Z May 28 '17 at 12:37
  • Try using `executeQuery()` instead of `execute()`. – Mark Rotteveel May 28 '17 at 13:25
  • @MarkRotteveel it actually used to be executeQuery before I posted here, then I tried execute but that also didn't work. – SenselessCoder May 28 '17 at 19:00

0 Answers0