943,654 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 4170
  • JSP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 12th, 2009
0

2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Expand Post »
hi all!
i am new to jsp, i have two comboboxes,in which values of 2nd combo box are linked two first combo box and we get values for both these boxes from database.

table1
JSP Syntax (Toggle Plain Text)
  1. (pk)itemno:text
  2. itemname:text (no duplicates)

table2
JSP Syntax (Toggle Plain Text)
  1. (pk)itemno:text(duplicates)
  2. (pk)pcode:text
  3. pname:text(no duplicates)

i want to display..

combobox1:itemname
combobox2:pname

Note: Both Values of combobox are to be retrieved from database.

when user select itemname corresponding values of pname should be displayed. I went through google and some threads of this forum and found some info but there option values are not retrieved from database(given in code itself), but i'm puzzled from retrieving 1st combo values and select one to retrieve the values of 2nd combo.

Hope i have made my question clear. Thank You all!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sriups is offline Offline
7 posts
since Jun 2009
Jun 12th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Can I see you code please? Start to code and post your problems. Definitely, we will help you.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 12th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Quote ...
Can I see you code please? Start to code and post your problems. Definitely, we will help you.
Many thanks. Well i started coding and succesful in retrieveing first combox values from database and stored the selected value in a hidden type input now i dunno know to proceed frm here.

my problem is that, i will use <form action="servlet "> and retrieve values from database using selected value in combobox1 (getParameter()).now how can i send back these reteieved values of combobox 2 back to jsp...??

my code trail.jsp

JSP Syntax (Toggle Plain Text)
  1. <%@ page language="java" contentType="text/html" import="java.sql.*" %>
  2.  
  3. <html>
  4. <head><title>select box demos</title>
  5. <script type="text/javascript">
  6. function redirect()
  7. {
  8. var index=document.myForm.selbox.selectedIndex;
  9. document.myForm.selv.value=document.myForm.selbox.options[index].value;
  10. document.write(document.myForm.selv.value);// stored selected value in hidden type value
  11. }
  12.  
  13. </script>
  14. </head>
  15. <body>
  16. <form name="myForm">
  17. <table>
  18. <%
  19. try
  20. {
  21. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  22. Connection conn=DriverManager.getConnection("jdbc:odbc:trail");
  23. try
  24. {
  25. String itname[]={};
  26.  
  27. int i=0,j=0,m=0;
  28.  
  29. String str1="select * from table1";
  30. PreparedStatement stmt1=conn.prepareStatement(str1);
  31. ResultSet rs1=stmt1.executeQuery();
  32.  
  33. itname=new String[100];
  34.  
  35. while(rs1.next())
  36. {
  37. itname[m]=rs1.getString(2);
  38. m++;
  39. }
  40.  
  41.  
  42. for(j=m;j<100;j++)
  43. {
  44. itname[j]="null";
  45. }
  46.  
  47.  
  48. out.println("<tr>");
  49. out.println("<td>Item Type:</td>");
  50. out.println("<td>");
  51. %>
  52. <select name="selbox" id="sel" onchange=redirect(this.form) >
  53. <%
  54. while(i<itname.length)
  55. {
  56. if(itname[i]!="null")
  57. out.println("<option value="+itname[i]+">"+itname[i]+"</option>");
  58. i++;
  59. }
  60. out.println("</select>");
  61.  
  62. }
  63. catch(SQLException e)
  64. {
  65. out.print("SQL Not Executing");
  66. }
  67. }
  68. catch(Exception e)
  69. {
  70. e.printStackTrace();
  71. }
  72. %>
  73. <input type="hidden" name="selv" >
  74. </table>
  75. </form>
  76. </body>
  77. </html>
Last edited by sriups; Jun 12th, 2009 at 3:53 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sriups is offline Offline
7 posts
since Jun 2009
Jun 13th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Use Ajax for your reqirement

i am giving solution to your problem

first create 2 jsp for your reqirement

first you show the one list itemname in the first jsp page
when user select the option from the list ,call ajax function in the
first jsp page ,and reterieve the value from the database according
to the itemname
i show the code
this is your first jsp page itemname.jsp

JSP Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. </head>
  10.  
  11.  
  12.  
  13. <body>
  14. <script type="text/javascript">function ajaxFunction()
  15. {var xmlHttp;
  16. try
  17. { // Firefox, Opera 8.0+, Safari
  18. xmlHttp=new XMLHttpRequest();
  19. }
  20. catch (e)
  21. { // Internet Explorer
  22. try
  23. { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
  24. catch (e)
  25. { try
  26. { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  27. catch (e)
  28. { alert("Your browser does not support AJAX!"); return false; } } }
  29. xmlHttp.onreadystatechange=function()
  30. {
  31. //alert("i m not in ready state");
  32.  
  33. if(xmlHttp.readyState==0)
  34. {
  35. //document.myForm.time.value=xmlHttp.responseText;
  36. alert("The request is not initialized");
  37.  
  38.  
  39.  
  40.  
  41.  
  42. }
  43. if(xmlHttp.readyState==1)
  44. {
  45. //document.myForm.time.value=xmlHttp.responseText;
  46. alert("The request has been set up");
  47.  
  48.  
  49.  
  50.  
  51.  
  52. }
  53.  
  54.  
  55.  
  56. if(xmlHttp.readyState==2)
  57. {
  58. //document.myForm.time.value=xmlHttp.responseText;
  59. alert("The request has been sent");
  60.  
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67. if(xmlHttp.readyState==3)
  68. {
  69. //document.myForm.time.value=xmlHttp.responseText;
  70. alert("The request is in process");
  71.  
  72.  
  73.  
  74.  
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. if(xmlHttp.readyState==4)
  86. {
  87. //document.myForm.time.value=xmlHttp.responseText;
  88. //alert("i m in ready state");
  89.  
  90. document.getElementById("theResponse").innerHTML = xmlHttp.responseText;
  91.  
  92.  
  93.  
  94. }
  95. }
  96. var itemname=document.myForm.itemname.value;
  97. var url="getProduct.jsp";
  98. url=url+"?itemname="+itemname+"&sid="+Math.random();
  99. //url=url+"&sid="+Math.random();
  100.  
  101.  
  102.  
  103. xmlHttp.open("GET",url,true);
  104. xmlHttp.send(null);
  105. }
  106.  
  107. </script>
  108. select itemname:<form name="myForm" onBlur="ajaxFunction();" >
  109. <select name="itemname">
  110. <option value="computer">computer</option>
  111. <option value="cars">Cars</option>
  112.  
  113. </select>
  114.  
  115. <div id="theResponse">
  116.  
  117. </div>
  118. </form></body>
  119. </html>



Now your second Jsp page
getProduct.jsp

JSP Syntax (Toggle Plain Text)
  1.  
  2. <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
  3. <html>
  4. <head>
  5.  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. </head>
  8.  
  9. <body>
  10.  
  11. <%
  12. // open the database connection and get the ResultSet
  13. // according to your itemname
  14. // take example
  15. // ResultSet rs=select pname from product where itemname='"+request.getParameter(itemname)+"' ;
  16.  
  17. %>
  18. <select name="pname">
  19.  
  20. <% while(rs.next())
  21. { %>
  22. <option value="<%=rs.getString(1)%>">"<%=rs.getString(1)%>"</option>
  23.  
  24. <% } %>
  25.  
  26. </select>
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. %>
  36.  
  37.  
  38.  
  39. </body>
  40. </html>



try it
Reputation Points: 2
Solved Threads: 1
Newbie Poster
amarjeetsingh is offline Offline
21 posts
since Feb 2009
Jun 13th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Promise me that you will the follows Peter budo's post.

A code I am posting here is a solution of your current problem. It can't help to solve your Java Web Development problems.

DB.java - Place this java file in WEB-INF/classes folder and compile it.
Java Syntax (Toggle Plain Text)
  1. package com.me;
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6. public class DB
  7. {
  8. static String url="jdbc:odbc:sampledb";
  9. static String username="your_user_name";
  10. static String password="your_password";
  11. static String driver="sun.jdbc.odbc.JdbcOdbcDriver";
  12. static{
  13. try {
  14. Class.forName(driver);
  15. }catch(Exception ex) {}
  16. }
  17. public static Connection getCn() throws Exception {
  18. return DriverManager.getConnection(url,username,password);
  19. }
  20. public static int execute(String sql) throws Exception {
  21. Connection cn=getCn();
  22. Statement st=cn.createStatement();
  23. int i=st.executeUpdate(sql);
  24. st.close(); cn.close();
  25. return i;
  26. }
  27. public static Object getValue(String sql) throws Exception{
  28. Connection cn=getCn();
  29. Statement st=cn.createStatement();
  30. ResultSet rs=st.executeQuery(sql);
  31. Object i=null;
  32. if(rs.next())
  33. i=rs.getObject(1);
  34. rs.close();
  35. st.close(); cn.close();
  36. return i;
  37. }
  38.  
  39. public static String getCombo(String sql,String sel) throws Exception{
  40. Connection cn=getCn();
  41. Statement st=cn.createStatement();
  42. ResultSet rs=st.executeQuery(sql);
  43. if(sel==null) sel="";
  44.  
  45. String result="<option value=''>Select</option>";
  46. String value="";
  47. String text="";
  48. while(rs.next()){
  49. value=rs.getString(1); text=rs.getString(2);
  50.  
  51. if(value.equals(sel)) {
  52. result=result + "<option value=\"" + value + "\" selected=\"selected\" >" + text + "</option>";
  53.  
  54. }
  55. else{
  56. result=result + "<option value=\"" + value + "\">" + text + "</option>";
  57.  
  58. }
  59. }
  60. rs.close(); st.close(); cn.close();
  61. return result;
  62. }
  63. }

and a page1.jsp
JSP Syntax (Toggle Plain Text)
  1. <?% page language="java" import="java.sql.*" %>
  2.  
  3. <%
  4. String item=request.getParameter("item");
  5. if(item==null) item="";
  6.  
  7. %>
  8. <form name="form1" method="post" action="page1.jsp">
  9. <select name="item" onchange="form1.submit()">
  10. <%=com.me.DB.getCombo("select itemno,itemname from item",item) %>
  11. </select>
  12.  
  13. <select name="prod">
  14. <%=com.me.DB.getCombo("select pcode,pname from product where itemno='" + item + "'",item) %>
  15. </select>
  16. </form>
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 13th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Quote ...
Use Ajax for your reqirement

i am giving solution to your problem

first create 2 jsp for your reqirement

first you show the one list itemname in the first jsp page
when user select the option from the list ,call ajax function in the
first jsp page ,and reterieve the value from the database according
to the itemname
i show the code
this is your first jsp page itemname.jsp
Thank You amarjeetsingh for spending your valuable time on answering this.

well i heared AJAX will solve the problem but i have no time (deadline of this project) to learn AJAX. So, looking for other alternative, anyway i managed to get through your code and going to work on it.


Quote ...
</script>
select itemname:<form name="myForm" onBlur="ajaxFunction();" >
<select name="itemname">
<option value="computer">computer</option>
<option value="cars">Cars</option>

</select>

Seems like you have given values directly , i want these values from database too. Wat's the job of this AJAX Script u given? .
Does it gets values for itemname from database?

Once again Many Thanks.Added Reputation.
Last edited by sriups; Jun 13th, 2009 at 8:09 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sriups is offline Offline
7 posts
since Jun 2009
Jun 13th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Thank you adatapost , with your motivation i started coding and it's very kind of you for helping me to solve this.

Quote ...
Promise me that you will the follows Peter budo's post.
Sure, I Promise.

Quote ...
A code I am posting here is a solution of your current problem. It can't help to solve your Java Web Development problems.
what kind of java web development problems?

I have seen your code and not familiar with some lines anyway got some sense of it, i will get back to you as soon as i run this code.

Thanks for Spending your valuable time.Added Reputation.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sriups is offline Offline
7 posts
since Jun 2009
Jun 13th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

you want to show the itemname from the database
it is very easy,

in your servlet
by open a dao connection
get all the data from the database
set into the ArrayList
then in the servlet
set the ArrayList
JSP Syntax (Toggle Plain Text)
  1. request.setAttribute("arr",ArrayList);

now
in the jsp

JSP Syntax (Toggle Plain Text)
  1. <select name="itemname">
  2. <c:forEach items="${arr}" var="show" >
  3. <option value=" ${show}">${show}</option>
  4.  
  5.  
  6. </c:out>
  7.  
  8.  
  9. </c:forEach>
  10.  
  11. </select>



but it will be in your first jsp page

then use the Ajax in the first jsp page for the second jsp page
Last edited by amarjeetsingh; Jun 13th, 2009 at 9:27 am.
Reputation Points: 2
Solved Threads: 1
Newbie Poster
amarjeetsingh is offline Offline
21 posts
since Feb 2009
Jun 16th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Hai adatapost compiled ur java code but no results it gives Http status 500 error...anyway Thanks 4 ur effort.

Just now finished tutorial on AJAX now i'm moving to amarjeetsingh's AJAX code...will be back after testing it!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sriups is offline Offline
7 posts
since Jun 2009
Jun 28th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

hi amarjeetsingh i have coded as you said

2 Jsp pages
itemname.jsp
getproduct.jsp

the problem here is the value of itemname selected was not sent to secondpage (request.getparameter not working) this is wat i get when i run the code
http://i43.tinypic.com/dwqve1.jpg

i am giving you the 2 jsp pages and access database Zipped and i put them in attachment down here, Could u please help me to run this code, i would be very greatful to you.

Thank You,
sriups
Attached Files
File Type: zip working.zip (12.6 KB, 49 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sriups is offline Offline
7 posts
since Jun 2009

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: sending an email with pdf file everyday
Next Thread in JSP Forum Timeline: how can i add css in jsp website in dreamwiever 8





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


Follow us on Twitter


© 2011 DaniWeb® LLC