The problem that I am having is that I am to create a servlet that will get data from three tables (SQL) and print them out in a html table.
The structure of the tables are as follows:
blog_comments:
row id (auto increments),
blogger id,
entry_tstamp,
t_stamp,
comment,
author_id,
blog_entries:
row id(auto increments),
blogger_id,
timestamp,
entry,
blogger_info:
row id(auto increments),
blogger_id,
name,
email address,
image,
password,
The blog entries are the orignal post and the blog_comments are related to that post. I'm trying to create a table that will show the blogger details followed by the post (from blog_entries) in the first row and in the second row and below, the related comments to that blog.
I am trying to use a while statment (shown below). I had to use a while statement to initally get the inital blog posts and blogger ID and the second while to display comments related to that.
I am having some difficulty as it goes throught the second while loop, it cancels the first while loop (after one increment) and then dumps off the the contents of the second while loop.
Does anyone know if there is a better way of implementing this? Should I be using a comparative loop? And I'm sure that my coding is wrong. Can anyone tell me where I am going wrong?
Thank you.
CODE:
try {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:E:/My Documents/NetBeansProjects/co2509blog.sqlite");
stmt = con.createStatement();
//String temp = "SELECT entry, name, email, image, comment from blog_comments C, blog_entries E, blogger_info I where E.blogger_id = I.blogger_id AND I.blogger_id = C.author_id ORDER BY E.timestamp";
//String temp = "SELECT entry, name, email, image from blog_entries E, blogger_info I where E.blogger_id = I.blogger_id ORDER BY E.timestamp";
//String temp = "SELECT entry, name, email, image, comment from blogger_info I JOIN blog_entries E ON I.blogger_id = E.blogger_id JOIN blog_comments C ON I.blogger_id = C.author_id WHERE E.timestamp = C.entry_tstamp ORDER BY E.timestamp";
String query = "SELECT name, image, email, entry FROM blogger_info I, blog_entries E WHERE I.blogger_id = E.blogger_id ORDER BY E.timestamp";
String query2 = "SELECT name, image, email, comment FROM blogger_info I, blog_entries E, blog_comments C WHERE I.blogger_id = C.author_id AND E.timestamp = C.entry_tstamp ORDER BY E.timestamp";
rs = stmt.executeQuery(query);
//rs2 = stmt.executeQuery(secondQuery);
// displaying records
int rowCount = 0;
int rowCount2 = 0;
out.println("<P ALIGN='center'><TABLE BORDER=5>");
ResultSetMetaData rsmetadata = rs.getMetaData();
int columnCount = rsmetadata.getColumnCount();
// table header
out.println("<TR>");
for (int i = 0; i < columnCount; i++) {
out.println("<TH>" + rsmetadata.getColumnLabel(i + 1) + "</TH>");
}
out.println("</TR>");
{
while (rs.next()) {
rowCount++;
out.println("<TR>");
for (int i = 0; i < columnCount; i++) {
out.println("<TD>" + rs.getString(i + 1) + "</TD>");
}
rowCount2++;
rs2 = stmt.executeQuery(query2);
ResultSetMetaData rsmetadata2 = rs2.getMetaData();
int columnCount2 = rsmetadata2.getColumnCount();
rsmetadata2 = rs2.getMetaData();
out.println("</TR>");
while (rs2.next()) {
rowCount2++;
out.println("<TR>");
for (int j = 0; j < columnCount2; j++) {
out.println("<TD>" + rs2.getString(j + 1) + "</TD>");
}
}
}
out.println("</TR>");
}
out.println("</TABLE></P>");
}