473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I pass selected value from combobox to SQL statement?

I created JSP page containing a form with two comboboxes, how can I get the selected value from the first combobox to pass it to SQL statement used by the second combobox?

here is my code with notes:
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" contentType="text/html; charset=windows-1256"
  2.     pageEncoding="windows-1256"%>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5.  
  6. <%@ page  import="java.io.IOException"%>
  7. <%@ page import="java.sql.Connection"%>
  8. <%@ page import="java.sql.DriverManager"%>
  9. <%@ page import="java.sql.PreparedStatement"%>
  10. <%@ page import="java.sql.ResultSet"%>
  11.  
  12. <%@ page import="javax.servlet.http.*" %>
  13. <%@ page import="javax.servlet.http.HttpServletRequest" %>
  14. <%@ page import="javax.servlet.http.HttpServletResponse" %>
  15.  
  16. <html>
  17. <head>
  18. <meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
  19.  
  20. </head>
  21. <body>
  22.     <%!
  23.         private Connection connect = null;
  24.         private PreparedStatement pstmnt;
  25.         private String sql;
  26.                 private String sql1;
  27.         private ResultSet rs;
  28.         private String cc= "";
  29.         String cat_e="";
  30.                             %>    
  31.  
  32.         <div align="center"><b>Subscription Form</b></div><br/>
  33.     <form name="subfrm" align="center" action="SaveSubscription"> 
  34.          Event Category: <select name="evnt_cat"> 
  35.                                      <option value="0">--Select Category--</option>
  36.          <%
  37.                             try {
  38.                                    connect = DriverManager.getConnection("jdbc:odbc:MyEvent","","");
  39.                                    sql = "SELECT * FROM Event";
  40.                                    pstmnt = connect.prepareStatement(sql);
  41.                                    rs = pstmnt.executeQuery();
  42.                                    while(rs.next()) {
  43.                                        cat_e = rs.getString("Event_Category");
  44.                             %>
  45.                                        <option value="<%=cat_e%>"><%=cat_e%></option>
  46.                              <%      } 
  47.  
  48.                                    rs.close();
  49.                                    pstmnt.close();
  50.                                    connect.close();
  51.                                  }
  52.                              catch( Exception e ) {
  53.                                System.out.println(e);
  54.  
  55.                              }
  56.                             %>
  57.                         </select><br/><br/>
  58.  
  59.         Event Sub_Category: <select name="evnt_subcat">
  60.                                     <option value="0">--Select Subcategory--</option>
  61.                             <%
  62.                             try {
  63.                                    connect = DriverManager.getConnection("jdbc:odbc:MyEvent","","");
  64.                                    sql1 = "SELECT EventDetails.Event_Title FROM Event INNER JOIN EventDetails ON Event.Event_ID = EventDetails.Event_ID WHERE Event.Event_Category = ?";
  65.                                    pstmnt = connect.prepareStatement(sql1);
  66.                                    pstmnt.setString(1,"The selected value from the 1st combobox");<--- Note: here, I need to put the value selected from the first combobox.
  67.                                    rs = pstmnt.executeQuery();
  68.                                    while(rs.next()) {
  69.                                        cc = rs.getString("Event_Title");
  70.                             %>
  71.                                        <option value="<%=cc%>"><%=cc%></option>
  72.                              <%      } 
  73.  
  74.                                    rs.close();
  75.                                    pstmnt.close();
  76.                                    connect.close();
  77.                                  }
  78.                              catch( Exception e ) {
  79.                                System.out.println(e);
  80.  
  81.                              }
  82.                             %>
  83.                         </select><br/><br/>
  84.          <input name="subusr" value="Subscribe" type="submit" align="center"/>
  85.      </form>
  86. </body>
  87. </html>
Feb 11 '11 #1
3 4651
acoder
16,027 Expert Mod 8TB
There are a number of ways. How do you want the interaction to take place? Do you want the 2nd combo box to be changed after submit with a page reload, or does it have to be instant?
Feb 15 '11 #2
Well, I need to do the following:
once I select value from the first combobox it should be passed to the SQL used by the second combobox to be filled.

Can this be done before pressing submit?

Thanks for your reply :)
Feb 15 '11 #3
acoder
16,027 Expert Mod 8TB
Yes, it can, but you will need to use Ajax. If you're not familiar with that, read up on a basic tutorial or two - see the sticky thread at the top of this forum.
Feb 15 '11 #4

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

Similar topics

8
by: TNGgroup | last post by:
Hi, Question, I have a simple form and want to submit a selected value of a combo box. so my combo box is named "selectname" and my form has action="file.asp?" what do I have to put after the...
2
by: Brian Henry | last post by:
say I have a databound combo box, which allows for text entry still (DropDownStype = DropDown in this case). now, If the user doesn't enter manually an item in the data bound list, nothing...
1
by: Ben | last post by:
I have a formview with a few dropdownlists (software version, database version, etc). When a software version is selected, the database version dropdownlist updates itself accordingly. When in...
0
by: Luqman | last post by:
My combobox is bound to sqldatasource, what I want is, when I change the combobox selected value, the sql datasource should be positioned to that value, so that all textboxes bound to that sql data...
0
by: Kysonel | last post by:
Hello ! I have been trying to use the datagridComboColumnStyle that was decribed in this thread : ...
1
by: sdshah5 | last post by:
hi, i want to know how to pass selected value from dropdownlist of child page to a multiline textbox on the parent page. in my code it is giving me the "id" of the selected dropdown value in the...
1
by: julianomartins | last post by:
Hi friends, how I make to recoup the value of database and to keep selected in combobox to edit my form? in script below it is selected field of the table that possesss id of the first item of...
4
by: hhak | last post by:
Hi all, Im pretty new to C#, im trying to assign a combobox selected value to a int variable. int number; combobox has items added (1,2,3,4,...9) when i select a number from the combobox i...
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
1
by: Joel1334 | last post by:
Hi! How can I change forecolor of a selected value in combobox? when I select a value in a combobox and press a button to "activate" what I've selected I want the text to be green and then...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.