?
Solved

populate a dropdown from a database

Posted on 2009-12-22
5
Medium Priority
?
303 Views
Last Modified: 2013-11-24
found below is my java, js, and html can you tell me if I am configured correctly
package com.testajax;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;

public
    class CountryServlet4 extends HttpServlet {
	
	public void doGet (HttpServletRequest req, HttpServletResponse res)
	    throws ServletException, IOException {
	    res.setContentType("text/html");
	    
	    ServletOutputStream out = res.getOutputStream();
	    out.println("<html>");
	    out.println("<head><title>Hello World</title></head>");
	    out.println("<body>");
	    
	    try {
		// The newInstance() call is a work around for some
		// broken Java implementations
	        out.println("asdf");
	        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
	    }
	    catch (Exception E) {
		out.println("Unable to load driver.");
		E.printStackTrace();
	    }
	    
	    out.println("<br><hr>");
	    
	    try {
		Connection Conn = 
		    DriverManager.getConnection("hidden","hidden","hidden");
	        
		// Do something with the Connection
		Statement Stmt = Conn.createStatement();
		ResultSet RS = Stmt.executeQuery("SELECT * from ORG_TAB");
		
		while (RS.next()) {
		    out.println(RS.getString(1));
		}
		// Clean up after ourselves
		RS.close();
		Stmt.close();
		Conn.close();
		
	    }
	    catch (SQLException E) {
		out.println("SQLException: " + E.getMessage());
		out.println("SQLState:     " + E.getSQLState());
		out.println("VendorError:  " + E.getErrorCode());
	    }
	    
	    out.println("<h1>Hello World. Sun... 1.4</h1>");
	    out.println("</body></html>");
	}
	
	public String getServletInfo() {
	  return "Create a page that says <i>Hello World</i> and send it back";
	}
    }



var req; 
  function showCustomer(str){ 
  alert("String that is passed in is :::"+str);
	   
   
	var url="T/4?countryId="+str; 
	alert("URL Referenced IS :::"+url);
    
    if (window.XMLHttpRequest){ 
		//alert("111");
      req = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject){ 
				//alert("222");
      req = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    req.open("Get",url,true); 

alert("Faith is a geek ");
    req.onreadystatechange = callback; 
    req.send(null);
  } 

  function callback(){ 
//alert("In Call Back Function aaa" +req.status+"\n"+req.readyState);
    if (req.readyState==4){ 
      if (req.status == 200){
    //  alert("In Call Back Function bbb" +req.status+"\n"+req.readyState);
  //document.companydd.value="a"; //Update the HTML Form element 
  //document.getElementById('New_NAME').value='b'
 //  document.getElementById('New_NAME').value='b'

      	//var xmlText=req.responseText;
      	//alert(xmlText).value
        //var decimal = document.getElementById('companydd'); 
        decimal = req.responseText; 
		//alert(decimal).value;
      } 
    }
   // clear();
  } 
  function clear(){ 
    var key = document.getElementById("customers"); 
    key.value=""; 
  }
  function focusIn(){ 
    document.getElementById("customers").focus();
  }






<html> 
<head> 
<script src="Customer4.js"></script> 
<title>Hello World</title> 
</head> 
<body> 
 
<form method="post"  action="page_to_post_information.html" name="f1"> 
Select a Customerb: 
<select name="customers" onchange="showCustomer(this.value)"> 
<option value="-">--</option> 
<option value="1">Ame</option> 
<option value="David">David</option> 
<option value="1">Happy </option> 
</select> 
 
 
 
<select name="companydd"> 
         
</select> 
</form> 
 
<b><span id="companyname"></span></b><br /><b><span id="companyname"></span></b><br /><b><span id="companyname"></span></b><br /> 
<span id="contactname"></span><br /> 
<span id="address"></span> 
<span id="city"></span><br/> 
<span id="country"></span> 
 
 
 
</body> 
</html>

Open in new window

0
Comment
Question by:gazdzid
  • 2
4 Comments
 
LVL 11

Expert Comment

by:asafadis
ID: 26108885
You tell us... is it working?   If not, what's happening?  Any error message?
0
 
LVL 20

Accepted Solution

by:
Sathish David  Kumar N earned 2000 total points
ID: 26110476
Wrong

1. ur not return that  Result set value . ur only printing !!

2. I dont know y use like this decimal = req.responseText;

3.in ur Jsp u never user decimal ;

4. <select name="companydd">
         
</select>

u didnt mention the code any where for companydd

Then how the value will set !!
0
 
LVL 3

Author Comment

by:gazdzid
ID: 26112553
asafadis:
Sorry that I did not tell you,  It is not working

dravidnsr:

Thank you for your reply,
Onchange of "customers" I want the javascript to call the customer4.java returning vales of select statement to thdropdown labeled "companydd".

Please note at this point in time I do realize that the value passed to js is not used but in the future it will be.  currently, I am only trying to dynamically populate a dropdow using a select statement..
0
 
LVL 3

Author Closing Comment

by:gazdzid
ID: 31669226
sorry, clening up old questions
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question