designing dynamic combo

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • creative1
    Contributor
    • Sep 2007
    • 274

    #1

    designing dynamic combo

    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.
  • creative1
    Contributor
    • Sep 2007
    • 274

    #2
    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?
    Code:
    <td><select name="clientName"
               
                		<option >../servlet/admin.ShowCompanyNames</option>
     </td>

    Comment

    • creative1
      Contributor
      • Sep 2007
      • 274

      #3
      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


      Code:
      package admin;
      import java.sql.*;
      
      
      //	http://www.javaservice.net/%7Ejava/bbs/read.cgi?b=javatip&c=r_p&m=devtip&n=1014323889&s=t
      public class DBPool {
      	private static boolean connect = false;
      	private static Connection[] cons = null;
      	private static int maxCons = 10;
      	//private static Boolean[] consStatus = new Boolean[maxCons];
      	private static int conID = -1;
      	private static final String dbURL = "jdbc:mysql://localhost:3306/virsarmedia";
      	private static final String user = "root";
      	private static final String password = ""; 
      	
      	// This DB Pool needs to be re-written so that uses Vector(or Hashtable)
      	// not to share one Connection for many Statements
      	
      	
      	
      	static{
      		try
      		{
      			System.out.print("Loading MySQL Driver ... ");
      			Class.forName("org.gjt.mm.mysql.Driver");
      			System.out.println("OK");
      		}catch(ClassNotFoundException e){System.out.println(e.getMessage());}
      	}
      	
      	// create Connection
      	public static final synchronized Connection createConn(){
      		try{
      			return DriverManager.getConnection(dbURL,user,password);
      		} catch (SQLException e){
      			e.printStackTrace();
      			return null;
      		}
      	}
      	
      	// get db pool manager
      	public static final synchronized Connection getConnMgr(){
      		// create connections when it's called at the first 
      		if(!connect){
      			cons = new Connection[maxCons];
      			for(int i=0; i<maxCons; i++){
      				cons[i] = createConn();
      				if(cons[i] == null) return null;
      			}
      			connect = true;
      		}
      		
      		// round robin returning Connection
      		conID++;
      		if(conID <= maxCons) conID = 0;
      		try{
      			if(cons[conID].isClosed()) cons[conID] = createConn();
      		} catch(SQLException se){
      			System.out.println(se.getMessage());
      			cons[conID] = createConn();
      		}
      		return cons[conID];
      	}
      
      	
      
      	
      	
      	
      	// destory all connections
      	public static final void destory(){
      		connect = false;
      		for(int i=0; i<maxCons; i++){
      			if(cons[i] != null){
      				try{
      					cons[i].close();
      				} catch(SQLException se){
      					cons[i] = null;
      				}
      			}
      		}
      		cons = null;
      	}
      	
      	// get numberOfRows
      	public static final int getRows(ResultSet rs){
      		int numberOfRows = 0;
      		try{
      			rs.last();
      			numberOfRows = rs.getRow();
      			rs.beforeFirst();
      		} catch(SQLException se){
      			se.printStackTrace();
      		}
      		return numberOfRows;
      	}
      	
      }

      Servlet to retrieve clientNames


      Code:
      package data;
       
      import admin.*;
      import business.*;
      import java.sql.ResultSet;
      import java.sql.*;
      import java.util.Vector;
      public class ClientDB {
      	final static String TBL_USER = "client";
      public static int readClientID(Connection connection, Client client) throws SQLException{
              //This method will return 0 if ClientID isn't found.
              String query = "SELECT ClientID FROM Client " +
                             "WHERE ClientID = '" + client.getClientID() + "'";
              Statement statement = connection.createStatement();
              ResultSet record = statement.executeQuery(query);
              record.next();
              int clientID = record.getInt("ClientID");
              record.close();
              statement.close();
              return clientID;
          }
      }

      Finally JSP file where I want to setup combobox;

      Code:
      <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN//">
      <html>
      <link href="../virsar.css" rel="stylesheet" type="text/css" />
      <head>
       <title>Virsar Media Administation Menu</title>
      </head>
       
      <body>
       
      <table cellspacing=0 cellpadding=0>
      	<td><img src="../images/title.jpg"></td>
      </table>
       
       
      <table cellspacing=0 cellpadding=0 width=800>
      	<td width=20>
       
       
      <!-- <br><br><br><br><br><br><br><br><br><br> -->
      	</td>
      	<!-- <td width=1 bgcolor=black></td> -->
      	<td>
      <h1>Subscriptions Processing Menu!</h1>
       
        
       
      <%-- Following code can be used to Add the record on the front end--%>
       
      <script language="JavaScript">
       
      function validate(form) {
          if (form.paymentID.value=="") {
              alert("Please fill in PaymentID");
              form.paymentName.focus();
          }
          else if (form.clientID.value=="") {
              alert("Please fill in ClientID");
              form.clientID.focus();
          }
          else if (form.paymentType.value=="") {
              alert("Please fill in Payment Type");
              form.paymentType.focus();
          }
          
       
          else {
              form.submit();
          }
      }
      </script>
       
       
        
      <table cellpadding="5" border=1>
       
        <tr valign="bottom">
          <th>Subscription ID</th>
          <th>Client ID</th>
          <th>Client Name</th>
          <th>Subscription Type</th>
          <th>Amount</th>
          <th>Subscription Start Date</th>
          <th>Subscription Expiry Date</th>
          <th>Description</th>
          <th>State</th>
          
               
             <th>Update Subscription</th>
                <th>Delete Subscription</th>
        </tr>
       
        
      <%@ page import="business.*" %>
      <% java.util.Vector payments = (java.util.Vector)session.getAttribute("payments");
       
      if(payments != null){
       
          for (int i =0; i<payments.size(); i++){
          
              Payment payment = (Payment)payments.get(i);
      	if(payment instanceof Payment){
              %>
              <tr valign="top">
                  <td><p><%= payment.getPaymentID() %></td>
                  <td><p><%= payment.getClientID() %></td>
                  <td><p><%= payment.getClientName() %></td>
                  <td><p><%= payment.getPaymentType() %></td>
                  <td><p><%= payment.getPaymentAmount() %></td>
                  <td><p><%= payment.getPaymentStartDate() %></td>
                  <td><p><%= payment.getPaymentExpiryDate() %></td>
                  <td><p><%= payment.getPaymentDescription() %></td>
                  <td><p><%= payment.getPaymentState() %></td>
                  
                  
                  
                          
                  <td><a href="../servlet/admin.ShowPaymentServlet?paymentID=<%=payment.getPaymentID()%>">Update </a></td>
                  <td><a href="../servlet/admin.DeletePaymentServlet?paymentID=<%=payment.getPaymentID()%>">Delete </a></td>
                   
              </tr>
              <%
      	}
          }
      }
      %>
      </table>
       
       
      <form action="../servlet/admin.AddPaymentServlet" method="get">
      <table cellspacing="5" border="0">
          
          <tr>
              <td align="right">Subscription ID:</td>
              <td><input type="text" name="paymentID" 
                      value="">
              </td>
          </tr>
          <tr>
              <td align="right"> Client Name :</td>
              
              <%-- ---------------------------------------- this is where I want to set my combo -------------------- --%>
              
               
                  <td>
                
                  		<input name="clientName" type = "text" value= "../servlet/admin/ShowCompanyNames.getClientID()"></input>
                  		
                  </td>
             
         
              <!--  <td><input type="text" name="clientName" 
                      value="">
              </td> -->
          </tr>
          <tr>
              <td align="right"> ClientID </td>
              <td><input type="text" name="clientID" 
                      value="">
              </td>
          </tr>
          
          <tr>
              <td align="right"> Payment Type :</td>
           <!--   <td><input type="text" name="paymentType" 
                      value="">
              </td>
           -->
                 <td>
                  <INPUT TYPE=RADIO NAME="paymentType" VALUE="Monthly">Montly
      			<INPUT TYPE=RADIO NAME="paymentType" VALUE="Yearly">Yearly
      			<INPUT TYPE=RADIO NAME="paymentType" VALUE="One Time">One Time
      			<INPUT TYPE=RADIO NAME="paymentType" VALUE="Other">Other<br>
       
      		    </td>
              
          </tr>
             
             <tr>
              <td align="right"> Payment Amount :</td>
              <td><input type="text" name="paymentAmount" 
                      value="">
              </td>
          </tr>
          
             <tr>
              <td align="right"> PaymentStartDate :</td>
              <td><input type="text" name="paymentStartDate" 
                      value="">
              </td>
          </tr>
          
             <tr>
              <td align="right"> PaymentExpiryDate :</td>
              <td><input type="text" name="paymentTelephone" 
                      value="">
              </td>
          </tr>
          
             <tr>
              <td align="right"> Description :</td>
              <td><input type="text" name="paymentDescription" 
                      value="">
              </td>
          </tr>
          
          
             <tr>
              <td align="right"> Status :</td>
              <td><input type="text" name="paymentState" 
                      value="">
              </td>
          </tr>
              
          
             <td></td>
              <td><input type="button" value="Add Subscription info." 
                         onClick="validate(this.form)"></td>
          </tr>
      </table>
      </form>
       
      <table>
      <tr>
      <td>
       
      <form action="Cadmin.PaymentsServlet" method="get">
          <input type="submit" value="Refresh List">
      </form>
      </td><td>
      <form action="../Admin/index.html">
          <input type="submit" value="Go Back">
      </form>
      </td>
      </table>
       
       </td>
      </table>
       
      </html>
       
       /body>
      </html>

      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

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        You may have more luck getting a response on a forum dedicated to JSP, like:

        Comment

        • creative1
          Contributor
          • Sep 2007
          • 274

          #5
          thanks,
          I don;t see any help around. I am still working ion it. Will post if I got any success.

          Comment

          • creative1
            Contributor
            • Sep 2007
            • 274

            #6
            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

            Comment

            Working...