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.