469,330 Members | 1,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

Access Data from Database By Submitting The Combo Box Values

i am making a program in a JSP. i have used some java script code in this program.

by this program i can find out the result for between years for any class.
i have three combo box for class_name, from_year, to_year. for class_name, Its value is 1 to 12. when i select class 11 or 12, an combox appear for subject having values Arts, comerce and science will be appeared.

in database for class 1 to 10 having subject values is AllSubject. and for class 11 or 12 having subject values arts, commerce and science.

my problem how i make the query which send for class 1 to 10 , assume subject values is all and for class 11 or 12 subject values arts, commerce and science with others combox values.

Here is JSP's files:

display.jsp:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <body>
  4. <form name="form" >
  5. <table align="center">
  6.  
  7. <script language="javascript">
  8. function sendData(){
  9. var sch=document.form.school.value;
  10. var cla=document.form.class_name.value;
  11. var y1=document.form.from_year.value;
  12. var y2=document.form.to_year.value;
  13. window.open("show.jsp?class_name="+cla+"&&from_year="+y1+"&&to_year="+y2+"&&school="+sch);
  14. alert(sch+" "+cla+ " "+y1+" "+y2);
  15. }
  16.  
  17. function hide(){
  18. if (document.getElementById){
  19. document.getElementById('combo').style.visibility = 'hidden';
  20. }
  21. }
  22. function call(class_name){
  23. var val = class_name.options[class_name.selectedIndex].text;
  24. if((val=='11')||(val=='12')){
  25. document.getElementById('combo').style.visibility = 'visible';
  26. var arr = new Array();
  27. arr[11] = new Array("Art","Commerce","Science");
  28. arr[12] = new Array("Art","Commerce","Science");
  29. var comboValue = class_name.value;
  30. document.forms["form"].elements["combo2"].options.length=0;
  31. for (var i=0;i<arr[comboValue].length;i++)
  32. {
  33. var option = document.createElement("option");
  34. option.setAttribute('value',i+1);
  35. option.innerHTML = arr[comboValue][i];
  36. document.forms["form"].elements["combo2"].appendChild(option);
  37. }
  38. }
  39. else{
  40. document.getElementById('combo').style.visibility = 'hidden';
  41. }
  42. }
  43. </script>
  44. <body onload="hide();">
  45. <p></p><br><br><br><br><br><br>
  46. <tr><td style="width: 11px"> <span>Class</td> <td><select name="class_name" onchange="call(this); >
  47. <%
  48.  
  49. for( int i=1;i<=12;i++){
  50. %>
  51. <option value="<%=i%>"><%=i%></option>
  52. <%
  53. }
  54. %>
  55. </select><td>
  56. <select id="combo" name="combo2">
  57. </select></td>
  58.  
  59. <tr><td style="width: 11px"> <span>From Year</td><td><select name="from_year">
  60. <%
  61. for(int j=2000;j<=2010;j++){
  62. %>
  63. <option value="<%=j%>"><%=j%></option>
  64. <%
  65. }
  66. %>
  67. </select></td></tr>
  68. <tr><td style="width: 11px"> <span>To Year</td><td><select name="to_year">
  69. <%
  70. for(int k=2000;k<=2010;k++){
  71. %>
  72. <option value="<%=k%>"><%=k%></option>
  73. <% } %>
  74. </select></td></tr><td></td>
  75. <tr><td style="width: 11px"> <span><input type="button" value="Result" onclick="sendData();"></td></tr>
  76. <tr><td><input type="hidden" name="school" value="<%=request.getParameter("school")%>"></td></tr>
  77. </table>
  78. </form>
  79. </body>
  80. </head>
  81. </html>
  82.  
show.jsp:
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" import="java.lang.*" import="java.sql.*" import="java.io.*" import="java.net.*"%>
  2. <html>
  3. <body>
  4. <%
  5. String school=request.getParameter("school");
  6. String class_name=(String)request.getParameter("class_name");
  7. String from_year=(String)request.getParameter("from_year");
  8. String to_year=(String)request.getParameter("to_year");
  9. %>
  10. <table align="center" border="1" align="center">
  11. <br>
  12. <tr align="center">
  13. <td width="150"height="30"><font size="+1">Year</td></font>
  14. <td width="150"height="30"><font size="+1">Class</td></font>
  15. <td width="150"height="30"><font size="+1">Subject</td></font>
  16. <td width="150"height="30"><font size="+1">Total Students</td></font>
  17. <td width="150"height="30"><font size="+1">Passout Students</td></font>
  18. <td width="150"height="30"><font size="+1">Marks Obtained 60 To 80</td></font>
  19. <td width="150"height="30"><font size="+1">Marks Obtained over 80</td></font>
  20. <td width="150"height="30"><font size="+1">Overall Result</td></font>
  21. <td width="150"height="30"><font size="+1">School</td></font>
  22. </tr>
  23.  
  24. <%
  25. Connection con = null;
  26. try {
  27. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
  28. con = DriverManager.getConnection("jdbc:odbc:sql;user=sa;password=1234");
  29. System.out.println("Connection created");
  30. Statement st=con.createStatement();
  31. System.out.println("st going to execute");
  32. String query="SELECT * FROM Result WHERE year BETWEEN '"+from_year+"' AND '"+to_year+"' and school='"+school+"'";
  33. System.out.println("now data are selecting");
  34. ResultSet rs=st.executeQuery(query);
  35. System.out.println("now data in rs.....");
  36. System.out.println("now going to rs block............");
  37. while(rs.next())
  38. {
  39. %>
  40. <tr align="center">
  41. <td width="150"height="30"><%out.println(rs.getString(1));%>
  42. <td width="150"height="30"><%out.println(rs.getString(2));%>
  43. <td width="150"height="30"><%out.println(rs.getString(3));%>
  44. <td width="150"height="30"><%out.println(rs.getString(4));%>
  45. <td width="150"height="30"><%out.println(rs.getString(5));%>
  46. <td width="150"height="30"><%out.println(rs.getString(6));%>
  47. <td width="150"height="30"><%out.println(rs.getString(7));%>
  48. <td width="150"height="30"><%out.println(rs.getInt(8));%>
  49. <td width="150"height="30"><%out.println(rs.getString(9));%>
  50. </tr>
  51. <%
  52. }
  53. rs.close();
  54. con.close();
  55. }catch(Exception e){out.println(e);}
  56. %>
  57. </table>
  58. </body>
  59. </html>
  60.  
combox box for subject remain hide when i select class 1 to 10. but if i select class 11 or 12 then
value for subject is Arts, Commerce and Science. how i make query from database base on the subject.

please help.
Apr 11 '10 #1
3 2125
acoder
16,027 Expert Mod 8TB
Unless you need the user to select one of the three schools/subjects, you can do this on the server-side rather than using JavaScript.
Apr 29 '10 #2
@acoder
thanks for reply, It will be appreciable if you will do some more focus.
Apr 29 '10 #3
acoder
16,027 Expert Mod 8TB
It is quite difficult to follow exactly what you're after.

Could you explain what the following means:
my problem how i make the query which send for class 1 to 10 , assume subject values is all and for class 11 or 12 subject values arts, commerce and science with others combox values.
If you need to formulate a query, that is in your JSP code, not JavaScript.
Apr 30 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

30 posts views Thread by Andante.in.Blue | last post: by
3 posts views Thread by Pasquale | last post: by
7 posts views Thread by Ruben Baumann | last post: by
1 post views Thread by Johnny | last post: by
52 posts views Thread by Neil | last post: by
2 posts views Thread by Michael Bialowas | last post: by
3 posts views Thread by Brian Graham | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.