Hello all, I am having problems with some queries in a servlet. Please note, my experience with mysql queries are very limited. This program exercise is for more of the servlet process than queries. With that being said, here is the description of the queries I am to perform:
given a user id and a month (from text fields) produce a list containing student name, list of files they own from atmp_cs368 and htmp_cs368 (largest to smallest) including total number of files and number of bytes used in the given month specified. If no month specified, then all months. If no user id specified, all files. If neither specified, all users and all months.
Here is the schema:
mysql> select * from roster_cs368; +--------+-----------+-----------+ | userId | firstName | lastName | +--------+-----------+-----------+ | apn7cf | Allen | Newton | | atggg3 | andrew | goebel |
userId is the primary key
sql> select * from htmp_cs368; +------------+----------+------------+----------+----------+-------+------+-------+----------------------+ | filePerms | numLinks | userId | idGroup | fileSize | monthIn | day | time | fileName | +------------+----------+------------+----------+----------+-------+------+-------+----------------------+ | drwx------ | 2 | schulte | faculty | 289 | Nov | 7 | 2011 | Java | | -rw-r--r-- | 1 | schulte | faculty | 136 | Apr | 29 | 2012 | LD | | drwxr-xr-x | 3 | schulte | faculty | 177 | Mar | 20 | 2012 | Upgrade |
no primary key here
sql>select * from atmp_cs368; +------------+----------+--------------+----------+----------+-------+------+-------+-----------------------------+ | filePerms | numLinks | userId | idGroup | fileSize | monthIn | day | time | fileName | +------------+----------+--------------+----------+----------+-------+------+-------+-----------------------------+ | drwxr-xr-x | 2 | remierm | 203 | 245 | Sep | 17 | 14:40 | 148360_sun_studio_12 | | drwx---rwx | 31 | antognolij | sasl | 2315 | Oct | 24 | 12:28 | 275 | | -rwx------ | 1 | kyzvdb | student | 36 | Sep | 19 | 13:05 | 275hh |
no primary key here as well
And here is the portion of my servlet code that has the queries in it:
try { String userId = request.getParameter("userId"); String monthIn = request.getParameter("monthIn"); String query_one = "select r.lastName, r.firstName, count(t.fileName)," + " sum(t.fileSize) from htmp_cs368 t join roster_cs368" + " r on t.userId=r.userId where t.userId=? and t.monthIn=?" + " group by t.userId"; String query_two = "select r.lastName, r.firstName, t.fileName," + " t.fileSize from htmp_cs368 t join roster_cs368 r on" + " t.userId=r.userId where t.userId=? and t.monthIn=?" + " order by fileSize desc"; String query_three = "select r.userId, files.* from roster_cs368" + " as r join (select * from htmp_cs368 where userId =" + " ? and monthIn = ? union select *" + " from atmp_cs368 where userId = ? and monthIn" + " = ?) as files on files.userId = r.userId" + " order by files.fileSize desc;"; if (userId == null && monthIn == null) { pstmt = connection.prepareStatement( query_three ); pstmt.setString( 1, userId); pstmt.setString(2, monthIn); rs = pstmt.executeQuery( ); while (rs.next()) { out.println("ID: " + rs.getString(1)); out.println("Month: " + rs.getString(2)); out.println(""); } } else if (userId == null) { pstmt = connection.prepareStatement( query_two ); pstmt.setString( 1, userId); pstmt.setString(2, monthIn); rs = pstmt.executeQuery( ); while (rs.next()) { out.println("ID: " + rs.getString(1)); out.println("Month: " + rs.getString(2)); out.println(""); } } else { pstmt = connection.prepareStatement( query_one ); pstmt.setString( 1, userId); pstmt.setString(2, monthIn); rs = pstmt.executeQuery( ); while (rs.next()) { out.println("ID: " + rs.getString(1)); out.println("Month: " + rs.getString(2)); out.println(""); } } statement.executeUpdate("drop table roster_cs368, atmp_cs368, htmp_cs368"); } catch(Exception ex) { out.println("Error: " + ex.getMessage()); } }
Now I have tested the program with simple queries and it works just fine. Creates the tables, loads them and even "outputted" those simple queries. The problem with the above queries is I get nothing. A blank html page. I am hoping someone with mysql query experience can show me what I did wrong and what I can do to fix them. I appreciate your time.