473,322 Members | 1,188 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,322 software developers and data experts.

designing dynamic combo

274 100+
Hi,
I am designing a data entry JSP form to add payment information. I want to add a combo box on the form that would have list of the compies present in databse. The combo should be populated based on the data in the databse. Can someone give a source code to do that?
I don't have anything to show you guys. I tried various examples but no luck.
I am using mySQL as databse.
I am using beans to display payment information on the same form. All that working fine. I have no idea how I can create a dynamic combo. Any help will be highly appreciated.
May 29 '08 #1
5 1947
creative1
274 100+
I wrote a servlet and want to call it to display compy names but I am not getting desired result. I get no errors. if you can just tell me if i m going right.... How should I call servlet?
Expand|Select|Wrap|Line Numbers
  1. <td><select name="clientName"
  2.  
  3.                     <option >../servlet/admin.ShowCompanyNames</option>
  4.  </td>
May 29 '08 #2
creative1
274 100+
I am not sure why no one is helping me out. I don't thing populating a combobox at run time would be a problem for experts. In my case, I am a beginner and don't know right method to do this. I am going to post my code so you guys may know what I have in my hand:
I have
connection setup
servlet for querying
JSP for display record
Bean for the Client Information.

Here is my code


For connecting to databse; working fine


Expand|Select|Wrap|Line Numbers
  1. package admin;
  2. import java.sql.*;
  3.  
  4.  
  5. //    http://www.javaservice.net/%7Ejava/bbs/read.cgi?b=javatip&c=r_p&m=devtip&n=1014323889&s=t
  6. public class DBPool {
  7.     private static boolean connect = false;
  8.     private static Connection[] cons = null;
  9.     private static int maxCons = 10;
  10.     //private static Boolean[] consStatus = new Boolean[maxCons];
  11.     private static int conID = -1;
  12.     private static final String dbURL = "jdbc:mysql://localhost:3306/virsarmedia";
  13.     private static final String user = "root";
  14.     private static final String password = ""; 
  15.  
  16.     // This DB Pool needs to be re-written so that uses Vector(or Hashtable)
  17.     // not to share one Connection for many Statements
  18.  
  19.  
  20.  
  21.     static{
  22.         try
  23.         {
  24.             System.out.print("Loading MySQL Driver ... ");
  25.             Class.forName("org.gjt.mm.mysql.Driver");
  26.             System.out.println("OK");
  27.         }catch(ClassNotFoundException e){System.out.println(e.getMessage());}
  28.     }
  29.  
  30.     // create Connection
  31.     public static final synchronized Connection createConn(){
  32.         try{
  33.             return DriverManager.getConnection(dbURL,user,password);
  34.         } catch (SQLException e){
  35.             e.printStackTrace();
  36.             return null;
  37.         }
  38.     }
  39.  
  40.     // get db pool manager
  41.     public static final synchronized Connection getConnMgr(){
  42.         // create connections when it's called at the first 
  43.         if(!connect){
  44.             cons = new Connection[maxCons];
  45.             for(int i=0; i<maxCons; i++){
  46.                 cons[i] = createConn();
  47.                 if(cons[i] == null) return null;
  48.             }
  49.             connect = true;
  50.         }
  51.  
  52.         // round robin returning Connection
  53.         conID++;
  54.         if(conID <= maxCons) conID = 0;
  55.         try{
  56.             if(cons[conID].isClosed()) cons[conID] = createConn();
  57.         } catch(SQLException se){
  58.             System.out.println(se.getMessage());
  59.             cons[conID] = createConn();
  60.         }
  61.         return cons[conID];
  62.     }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.     // destory all connections
  70.     public static final void destory(){
  71.         connect = false;
  72.         for(int i=0; i<maxCons; i++){
  73.             if(cons[i] != null){
  74.                 try{
  75.                     cons[i].close();
  76.                 } catch(SQLException se){
  77.                     cons[i] = null;
  78.                 }
  79.             }
  80.         }
  81.         cons = null;
  82.     }
  83.  
  84.     // get numberOfRows
  85.     public static final int getRows(ResultSet rs){
  86.         int numberOfRows = 0;
  87.         try{
  88.             rs.last();
  89.             numberOfRows = rs.getRow();
  90.             rs.beforeFirst();
  91.         } catch(SQLException se){
  92.             se.printStackTrace();
  93.         }
  94.         return numberOfRows;
  95.     }
  96.  
  97. }

Servlet to retrieve clientNames


Expand|Select|Wrap|Line Numbers
  1. package data;
  2.  
  3. import admin.*;
  4. import business.*;
  5. import java.sql.ResultSet;
  6. import java.sql.*;
  7. import java.util.Vector;
  8. public class ClientDB {
  9.     final static String TBL_USER = "client";
  10. public static int readClientID(Connection connection, Client client) throws SQLException{
  11.         //This method will return 0 if ClientID isn't found.
  12.         String query = "SELECT ClientID FROM Client " +
  13.                        "WHERE ClientID = '" + client.getClientID() + "'";
  14.         Statement statement = connection.createStatement();
  15.         ResultSet record = statement.executeQuery(query);
  16.         record.next();
  17.         int clientID = record.getInt("ClientID");
  18.         record.close();
  19.         statement.close();
  20.         return clientID;
  21.     }
  22. }
  23.  
  24.  
  25.  

Finally JSP file where I want to setup combobox;

Expand|Select|Wrap|Line Numbers
  1. <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN//">
  2. <html>
  3. <link href="../virsar.css" rel="stylesheet" type="text/css" />
  4. <head>
  5.  <title>Virsar Media Administation Menu</title>
  6. </head>
  7.  
  8. <body>
  9.  
  10. <table cellspacing=0 cellpadding=0>
  11.     <td><img src="../images/title.jpg"></td>
  12. </table>
  13.  
  14.  
  15. <table cellspacing=0 cellpadding=0 width=800>
  16.     <td width=20>
  17.  
  18.  
  19. <!-- <br><br><br><br><br><br><br><br><br><br> -->
  20.     </td>
  21.     <!-- <td width=1 bgcolor=black></td> -->
  22.     <td>
  23. <h1>Subscriptions Processing Menu!</h1>
  24.  
  25.  
  26.  
  27. <%-- Following code can be used to Add the record on the front end--%>
  28.  
  29. <script language="JavaScript">
  30.  
  31. function validate(form) {
  32.     if (form.paymentID.value=="") {
  33.         alert("Please fill in PaymentID");
  34.         form.paymentName.focus();
  35.     }
  36.     else if (form.clientID.value=="") {
  37.         alert("Please fill in ClientID");
  38.         form.clientID.focus();
  39.     }
  40.     else if (form.paymentType.value=="") {
  41.         alert("Please fill in Payment Type");
  42.         form.paymentType.focus();
  43.     }
  44.  
  45.  
  46.     else {
  47.         form.submit();
  48.     }
  49. }
  50. </script>
  51.  
  52.  
  53.  
  54. <table cellpadding="5" border=1>
  55.  
  56.   <tr valign="bottom">
  57.     <th>Subscription ID</th>
  58.     <th>Client ID</th>
  59.     <th>Client Name</th>
  60.     <th>Subscription Type</th>
  61.     <th>Amount</th>
  62.     <th>Subscription Start Date</th>
  63.     <th>Subscription Expiry Date</th>
  64.     <th>Description</th>
  65.     <th>State</th>
  66.  
  67.  
  68.        <th>Update Subscription</th>
  69.           <th>Delete Subscription</th>
  70.   </tr>
  71.  
  72.  
  73. <%@ page import="business.*" %>
  74. <% java.util.Vector payments = (java.util.Vector)session.getAttribute("payments");
  75.  
  76. if(payments != null){
  77.  
  78.     for (int i =0; i<payments.size(); i++){
  79.  
  80.         Payment payment = (Payment)payments.get(i);
  81.     if(payment instanceof Payment){
  82.         %>
  83.         <tr valign="top">
  84.             <td><p><%= payment.getPaymentID() %></td>
  85.             <td><p><%= payment.getClientID() %></td>
  86.             <td><p><%= payment.getClientName() %></td>
  87.             <td><p><%= payment.getPaymentType() %></td>
  88.             <td><p><%= payment.getPaymentAmount() %></td>
  89.             <td><p><%= payment.getPaymentStartDate() %></td>
  90.             <td><p><%= payment.getPaymentExpiryDate() %></td>
  91.             <td><p><%= payment.getPaymentDescription() %></td>
  92.             <td><p><%= payment.getPaymentState() %></td>
  93.  
  94.  
  95.  
  96.  
  97.             <td><a href="../servlet/admin.ShowPaymentServlet?paymentID=<%=payment.getPaymentID()%>">Update </a></td>
  98.             <td><a href="../servlet/admin.DeletePaymentServlet?paymentID=<%=payment.getPaymentID()%>">Delete </a></td>
  99.  
  100.         </tr>
  101.         <%
  102.     }
  103.     }
  104. }
  105. %>
  106. </table>
  107.  
  108.  
  109. <form action="../servlet/admin.AddPaymentServlet" method="get">
  110. <table cellspacing="5" border="0">
  111.  
  112.     <tr>
  113.         <td align="right">Subscription ID:</td>
  114.         <td><input type="text" name="paymentID" 
  115.                 value="">
  116.         </td>
  117.     </tr>
  118.     <tr>
  119.         <td align="right"> Client Name :</td>
  120.  
  121.         <%-- ---------------------------------------- this is where I want to set my combo -------------------- --%>
  122.  
  123.  
  124.             <td>
  125.  
  126.                     <input name="clientName" type = "text" value= "../servlet/admin/ShowCompanyNames.getClientID()"></input>
  127.  
  128.             </td>
  129.  
  130.  
  131.         <!--  <td><input type="text" name="clientName" 
  132.                 value="">
  133.         </td> -->
  134.     </tr>
  135.     <tr>
  136.         <td align="right"> ClientID </td>
  137.         <td><input type="text" name="clientID" 
  138.                 value="">
  139.         </td>
  140.     </tr>
  141.  
  142.     <tr>
  143.         <td align="right"> Payment Type :</td>
  144.      <!--   <td><input type="text" name="paymentType" 
  145.                 value="">
  146.         </td>
  147.      -->
  148.            <td>
  149.             <INPUT TYPE=RADIO NAME="paymentType" VALUE="Monthly">Montly
  150.             <INPUT TYPE=RADIO NAME="paymentType" VALUE="Yearly">Yearly
  151.             <INPUT TYPE=RADIO NAME="paymentType" VALUE="One Time">One Time
  152.             <INPUT TYPE=RADIO NAME="paymentType" VALUE="Other">Other<br>
  153.  
  154.             </td>
  155.  
  156.     </tr>
  157.  
  158.        <tr>
  159.         <td align="right"> Payment Amount :</td>
  160.         <td><input type="text" name="paymentAmount" 
  161.                 value="">
  162.         </td>
  163.     </tr>
  164.  
  165.        <tr>
  166.         <td align="right"> PaymentStartDate :</td>
  167.         <td><input type="text" name="paymentStartDate" 
  168.                 value="">
  169.         </td>
  170.     </tr>
  171.  
  172.        <tr>
  173.         <td align="right"> PaymentExpiryDate :</td>
  174.         <td><input type="text" name="paymentTelephone" 
  175.                 value="">
  176.         </td>
  177.     </tr>
  178.  
  179.        <tr>
  180.         <td align="right"> Description :</td>
  181.         <td><input type="text" name="paymentDescription" 
  182.                 value="">
  183.         </td>
  184.     </tr>
  185.  
  186.  
  187.        <tr>
  188.         <td align="right"> Status :</td>
  189.         <td><input type="text" name="paymentState" 
  190.                 value="">
  191.         </td>
  192.     </tr>
  193.  
  194.  
  195.        <td></td>
  196.         <td><input type="button" value="Add Subscription info." 
  197.                    onClick="validate(this.form)"></td>
  198.     </tr>
  199. </table>
  200. </form>
  201.  
  202. <table>
  203. <tr>
  204. <td>
  205.  
  206. <form action="Cadmin.PaymentsServlet" method="get">
  207.     <input type="submit" value="Refresh List">
  208. </form>
  209. </td><td>
  210. <form action="../Admin/index.html">
  211.     <input type="submit" value="Go Back">
  212. </form>
  213. </td>
  214. </table>
  215.  
  216.  </td>
  217. </table>
  218.  
  219. </html>
  220.  
  221.  /body>
  222. </html>
  223.  
  224.  

I can display client payment information. My only concern is to populate combobox. All I need is to know how I do that. either using beans or servlet? and syntax.........
please help me it's urgent..
Thanks
May 30 '08 #3
BigDaddyLH
1,216 Expert 1GB
You may have more luck getting a response on a forum dedicated to JSP, like:

http://forum.java.sun.com/forum.jspa?forumID=45
May 30 '08 #4
creative1
274 100+
thanks,
I don;t see any help around. I am still working ion it. Will post if I got any success.
Jun 1 '08 #5
creative1
274 100+
I got combo working values are obtained successfully. Now I want to know how I can bound a text field to hold the ID of the ClientName selected. Any idea?
Can I use LTRIM? if I store client name like this 1 - ABC corp. where 1 is client ID in combobox. Any ideas?
Regards
Jun 2 '08 #6

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

Similar topics

13
by: mr_burns | last post by:
hi, is it possible to change the contents of a combo box when the contents of another are changed. for example, if i had a combo box called garments containing shirts, trousers and hats, when...
3
by: vgrssrtrs | last post by:
<html> <head> <script language="JavaScript"> <!-- /* *** Multiple dynamic combo boxes *** by Mirko Elviro, 9 Mar 2005 *** ***Please do not remove this comment
5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
9
by: Bob Alston | last post by:
In 2002, "GrayJay" posted the following code: I did this in a jazz record catalogue to find composers - On a form "frmComposers" Create a text box - txtFindComposer, and add the following sub...
2
by: Al | last post by:
hi,I am using asp.net(vb) I am trying to create 3 combo boxes which are dependable on each other. when comb1 is selected, it shows the comb2 data based on the selection in combo1 and when combo2...
2
by: Mothi Kannan | last post by:
Hi, In vb.net I have created two combo box, dynamically and filled list in the first combo. How can I fill the Selected index value of first combo in to the second combo The second combo...
2
by: taras.di | last post by:
Hi everyone, I've been reading up on how to create a drop down box who's context is dynamically produced based on the value of a previous select box. I've read a lot about some of the browsers...
1
by: hottoku | last post by:
Hi All, I'm having quite a bit of trouble designing a search tool to work with my database. I have found lots of examples from Microsoft Templates to Allen Browne's sample search form. The...
4
by: PM ArunKumar | last post by:
i have two dropdown list in my form where the values in the list of second drop down changes based on the value i select in the first dropdown.(very similar to country and states list), here after...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.