| | |
2 comboboxes values of 2nd depends on 1st ,OPTION values from Database
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
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
table2
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!
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)
(pk)itemno:text itemname:text (no duplicates)
table2
JSP Syntax (Toggle Plain Text)
(pk)itemno:text(duplicates) (pk)pcode:text 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!
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Can I see you code please? Start to code and post your problems. Definitely, we will help you.
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)
<%@ page language="java" contentType="text/html" import="java.sql.*" %> <html> <head><title>select box demos</title> <script type="text/javascript"> function redirect() { var index=document.myForm.selbox.selectedIndex; document.myForm.selv.value=document.myForm.selbox.options[index].value; document.write(document.myForm.selv.value);// stored selected value in hidden type value } </script> </head> <body> <form name="myForm"> <table> <% try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection("jdbc:odbc:trail"); try { String itname[]={}; int i=0,j=0,m=0; String str1="select * from table1"; PreparedStatement stmt1=conn.prepareStatement(str1); ResultSet rs1=stmt1.executeQuery(); itname=new String[100]; while(rs1.next()) { itname[m]=rs1.getString(2); m++; } for(j=m;j<100;j++) { itname[j]="null"; } out.println("<tr>"); out.println("<td>Item Type:</td>"); out.println("<td>"); %> <select name="selbox" id="sel" onchange=redirect(this.form) > <% while(i<itname.length) { if(itname[i]!="null") out.println("<option value="+itname[i]+">"+itname[i]+"</option>"); i++; } out.println("</select>"); } catch(SQLException e) { out.print("SQL Not Executing"); } } catch(Exception e) { e.printStackTrace(); } %> <input type="hidden" name="selv" > </table> </form> </body> </html>
Last edited by sriups; Jun 12th, 2009 at 3:53 pm.
•
•
Join Date: Feb 2009
Posts: 21
Reputation:
Solved Threads: 1
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
Now your second Jsp page
getProduct.jsp
try it
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)
<html> <head> </head> <body> <script type="text/javascript">function ajaxFunction() {var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { //alert("i m not in ready state"); if(xmlHttp.readyState==0) { //document.myForm.time.value=xmlHttp.responseText; alert("The request is not initialized"); } if(xmlHttp.readyState==1) { //document.myForm.time.value=xmlHttp.responseText; alert("The request has been set up"); } if(xmlHttp.readyState==2) { //document.myForm.time.value=xmlHttp.responseText; alert("The request has been sent"); } if(xmlHttp.readyState==3) { //document.myForm.time.value=xmlHttp.responseText; alert("The request is in process"); } if(xmlHttp.readyState==4) { //document.myForm.time.value=xmlHttp.responseText; //alert("i m in ready state"); document.getElementById("theResponse").innerHTML = xmlHttp.responseText; } } var itemname=document.myForm.itemname.value; var url="getProduct.jsp"; url=url+"?itemname="+itemname+"&sid="+Math.random(); //url=url+"&sid="+Math.random(); xmlHttp.open("GET",url,true); xmlHttp.send(null); } </script> select itemname:<form name="myForm" onBlur="ajaxFunction();" > <select name="itemname"> <option value="computer">computer</option> <option value="cars">Cars</option> </select> <div id="theResponse"> </div> </form></body> </html>
Now your second Jsp page
getProduct.jsp
JSP Syntax (Toggle Plain Text)
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <% // open the database connection and get the ResultSet // according to your itemname // take example // ResultSet rs=select pname from product where itemname='"+request.getParameter(itemname)+"' ; %> <select name="pname"> <% while(rs.next()) { %> <option value="<%=rs.getString(1)%>">"<%=rs.getString(1)%>"</option> <% } %> </select> %> </body> </html>
try it
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.
and a page1.jsp
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)
package com.me; import java.sql.*; import java.util.*; public class DB { static String url="jdbc:odbc:sampledb"; static String username="your_user_name"; static String password="your_password"; static String driver="sun.jdbc.odbc.JdbcOdbcDriver"; static{ try { Class.forName(driver); }catch(Exception ex) {} } public static Connection getCn() throws Exception { return DriverManager.getConnection(url,username,password); } public static int execute(String sql) throws Exception { Connection cn=getCn(); Statement st=cn.createStatement(); int i=st.executeUpdate(sql); st.close(); cn.close(); return i; } public static Object getValue(String sql) throws Exception{ Connection cn=getCn(); Statement st=cn.createStatement(); ResultSet rs=st.executeQuery(sql); Object i=null; if(rs.next()) i=rs.getObject(1); rs.close(); st.close(); cn.close(); return i; } public static String getCombo(String sql,String sel) throws Exception{ Connection cn=getCn(); Statement st=cn.createStatement(); ResultSet rs=st.executeQuery(sql); if(sel==null) sel=""; String result="<option value=''>Select</option>"; String value=""; String text=""; while(rs.next()){ value=rs.getString(1); text=rs.getString(2); if(value.equals(sel)) { result=result + "<option value=\"" + value + "\" selected=\"selected\" >" + text + "</option>"; } else{ result=result + "<option value=\"" + value + "\">" + text + "</option>"; } } rs.close(); st.close(); cn.close(); return result; } }
and a page1.jsp
JSP Syntax (Toggle Plain Text)
<?% page language="java" import="java.sql.*" %> <% String item=request.getParameter("item"); if(item==null) item=""; %> <form name="form1" method="post" action="page1.jsp"> <select name="item" onchange="form1.submit()"> <%=com.me.DB.getCombo("select itemno,itemname from item",item) %> </select> <select name="prod"> <%=com.me.DB.getCombo("select pcode,pname from product where itemno='" + item + "'",item) %> </select> </form>
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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
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.
•
•
•
•
</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.
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
Thank you adatapost , with your motivation i started coding and it's very kind of you for helping me to solve this.
Sure, I Promise.
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.
•
•
•
•
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.
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.
•
•
Join Date: Feb 2009
Posts: 21
Reputation:
Solved Threads: 1
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
now
in the jsp
but it will be in your first jsp page
then use the Ajax in the first jsp page for the second jsp page
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)
request.setAttribute("arr",ArrayList);
now
in the jsp
JSP Syntax (Toggle Plain Text)
<select name="itemname"> <c:forEach items="${arr}" var="show" > <option value=" ${show}">${show}</option> </c:out> </c:forEach> </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.
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- how to read form element values (JavaScript / DHTML / AJAX)
- checking if same values exist in the database and generating reports (VB.NET)
- Retaining textbox values (PHP)
- Get sum of values in a loop? (PHP)
- combobox selected value connected to a SQL statement (VB.NET)
- Retrieve values from sql server 2005 (ASP.NET)
- Reading an Access Database (VB.NET)
- question about php string function (PHP)
- unsigned character pointer to Hex values (C++)
- help with polymorphism and inheritance...getting null values (Java)
Other Threads in the JSP Forum
- Previous Thread: sending an email with pdf file everyday
- Next Thread: web based project topic in java
| Thread Tools | Search this Thread |
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






