943,816 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 2799
  • JSP RSS
Aug 30th, 2007
0

generating dynamic/runtime query

Expand Post »
I want to generate a code that will automatically take the poll-id..of which the user wishes to view the result of that poll. there's the code:using mysql & java & another thing i wish to use this on a jsp page:


JSP Syntax (Toggle Plain Text)
  1. package votepiepack;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.*;
  6. import org.jfree.chart.*;
  7. import org.jfree.data.jdbc.*;
  8. import org.jfree.data.general.*;
  9.  
  10.  
  11. public class votepie {
  12.  
  13. /**
  14.   * @param args
  15.   */
  16.  
  17. private PieDataset readData() {
  18. JDBCPieDataset data = null;
  19. String url = "jdbc:mysql://localhost/vote";
  20. Connection con;
  21. try {
  22. Class.forName("com.mysql.jdbc.Driver");
  23. }
  24. catch (ClassNotFoundException e) {
  25. System.err.print("ClassNotFoundException: ");
  26. System.err.println(e.getMessage());
  27. }
  28. try {
  29. con = DriverManager.getConnection(url, "vote", "vote001");
  30. data = new JDBCPieDataset(con);
  31. String sql = "SELECT option_text, counter FROM VOTE_VOTES WHERE poll-id=1;";
  32. data.executeQuery(sql);
  33. con.close();
  34. }
  35. catch (SQLException e) {
  36. System.err.print("SQLException: ");
  37. System.err.println(e.getMessage());
  38. }
  39. catch (Exception e) {
  40. System.err.print("Exception: ");
  41. System.err.println(e.getMessage());
  42. }
  43. return data;
  44. }
  45. public static void main(String[] args) {
  46. // TODO Auto-generated method stub
  47.  
  48. votepie pd = new votepie();
  49. pd.readData();
  50.  
  51. //creating the chart
  52. JFreeChart chart = ChartFactory.createPieChart(
  53. "Sample Pie Chart",
  54. pd.readData(),
  55. true, // legend?
  56. true, // tooltips?
  57. false // URLs?
  58. );
  59. // create and display a frame...
  60. ChartFrame frame = new ChartFrame("First", chart);
  61. frame.pack();
  62. frame.setVisible(true);
  63.  
  64. }
  65.  
  66. }

as you can see I can view only 1 result at a time.
JSP Syntax (Toggle Plain Text)
  1. String sql = "SELECT option_text, counter FROM VOTE_VOTES WHERE poll-id=1;";
but how is it possible to capture the poll-id on which the user wishes to click & show the result of that poll....is it by using "placeholders" if yes how? or any other, please suggest ...thanks

ps: donno where to post so posting both on mysql & java forums.
Last edited by apontutul; Aug 30th, 2007 at 3:00 am.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
apontutul is offline Offline
38 posts
since Nov 2006
Aug 31st, 2007
0

Re: generating dynamic/runtime query

hope i've made myself clear..please help guys
Reputation Points: 10
Solved Threads: 1
Light Poster
apontutul is offline Offline
38 posts
since Nov 2006
Aug 31st, 2007
0

Re: generating dynamic/runtime query

Click to Expand / Collapse  Quote originally posted by apontutul ...
hope i've made myself clear..please help guys
This is really a question that belongs in the JSP forum. You will need to read the pollId they clicked from the GET or POST request, place that variable in your query, and display the results. It is a very standard thing for web programming.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 31st, 2007
0

Re: generating dynamic/runtime query

Well there are a number of ways to capture the id's. But what is the best way for you is to be decided by you. Here are the ways:

1) URL rewriting. (Exposed to visible eyes. Not a safe bet)
2) Cookies. (Cookies needs to be enabled on the Client Browser).
3) HTTPSession Object. (You can pass the id through the HTTPSession Object: Very Safe)

Please refer some text on how to use them.

Now, as far as you are concerned with sending the id, you should create a public method which will take id as a parameter and you can pass the id from it.
Here's how it would look like:

public class VotePie {

public static PieDataSet readData(int id){
JDBCPieDataset data = null;
String url = "jdbc:mysql://localhost/vote";
Connection con;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url, "vote", "vote001");
data = new JDBCPieDataset(con);
String sql = "SELECT option_text, counter FROM VOTE_VOTES WHERE poll-id="+id;//you will send the parameter value at runtime.
data.executeQuery(sql);
con.close();
} catch (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
} catch (Exception e) {
System.err.print("Exception: ");
System.err.println(e.getMessage());
}
return data;
}

}

Your jsp page from where you need to take the parameters would look something like this. I'll call it inputParam.jsp

<%@page import="VotePie"%>
<!--%Other libraries required by you%-->
<html>
<a href="/jsp/collectVote&1">Choice1</a>
<a href="/jsp/collectVote&2">Choice2</a>
<a href="/jsp/collectVote&3">Choice3</a>
</html>

It will direct you to a new jsp page or Servlet (whatever you prefer. Mostly Servlet is preferred) but restricting to jsp for now.

in the collectVote.jsp you are required to get the input parameter and pass it to the readData method of ....

Here's the code:

String param = (String)request.getParameter();
int id = Integer.parseInt(param);


JDBCPieDataset data = VotePie.readData(id);
request.setParameter("DATA", data);
response.setUrl("theUrlToDisplayPie");

You'll automatically be directed to the new web page.
In this page write the same code you were writing in the main method. Just note that no method name is required.
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: Display Xml In Jsp
Next Thread in JSP Forum Timeline: illegal start of expression & type





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC