By dropdownlist retrive data from database and show in textbox in jsp.
Hello Sir,
I am doing project in jsp.I have to retrive data from database, when I select value from dropdownlist, and that data I want to show in textbox. Please suggest some ajax code that will work. your suggestion will be helpful for me .And thanks in advance.
View Answers
December 20, 2010 at 5:58 PM
Hi Friend,
Try the following code:
1)selectname.jsp:
<html>
<head>
<script type="text/javascript">
function showEmp(emp_value){
if(document.getElementById("address").value!="-1"){
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null){
alert ("Browser does not support HTTP Request")
return
}
var url="getvalue.jsp"
url=url+"?name="+emp_value
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
else{
alert("Please Select Employee Id");
}
}
function stateChanged(){
document.getElementById("email").value ="";
document.getElementById("address").value ="";
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
var showdata = xmlHttp.responseText;
var strar = showdata.split(":");
if(strar.length==1){
document.getElementById("address").focus();
alert("Please Select Employee Id");
document.getElementById("email").value =" ";
document.getElementById("address").value =" ";
}
else if(strar.length>1) {
var strname = strar[1];
document.getElementById("address").value= strar[1];
document.getElementById("email").value= strar[2];
}
}
}
function GetXmlHttpObject(){
var xmlHttp=null;
try{
xmlHttp=new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<form name="employee">
<br><br>
<table border="0" width="400px" align="center" bgcolor="#CDFFFF">
<div id="mydiv"></div>
<tr><td><b>Select Employee Name</b></td><td>
<select name="name" onchange="showEmp(this.value);">
<option value="-1">Select</option>
<option value="Angelina">Angelina</option>
<option value="Martina">Martina</option>
<option value="Julia">Julia</option>
<option value="Angel">Angel</option>
</select>
</td></tr>
<tr><td ><b>Employee Address:</b></td><td>
<input type="text" name="address" id="address" value=""></td></tr>
<tr><td><b>Employee Email:</b></td><td>
<input type="text" name="email" id="email" value=""></td></tr>
</table>
</form>
<table border="0" width="100%" align="center">
<br>
<br>
</table>
</body>
</html>
2)getvalue.jsp:
<%@page import="java.sql.*"%>
<%
String emp_id = request.getParameter("name").toString();
String data="";
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
int sumcount=0;
Statement st;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
String query = "select * from employee where name='"+emp_id+"'";
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next())
{
data = ":" + rs.getString("address") +":"+ rs.getString("email");
}
out.println(data);
}
catch (Exception e) {
e.printStackTrace();
}
%>
3)For the above code, we have created following table:
CREATE TABLE `employee` (
`id` bigint(255) default NULL,
`name` varchar(255) default NULL,
`address` varchar(255) default NULL,
`contactNo` int(255) default NULL,
`email` varchar(255) default NULL
)
Thanks
January 20, 2012 at 12:27 PM
sorry this code is not working, i can't select an employ id...? can u help me.. ?
Ads
Related Tutorials/Questions & Answers:
How to show data from database in textbox in jsp
How to
show data from database in
textbox in
jsp How to
show data from database in
textbox in
jsp
Here is an example that retrieve the particular record
from the
database and display it in
textbox using
JSP.
<
Advertisements
how to get data from database into dropdownlist in jsp
how to get
data from database into
dropdownlist in jsp Can anybody tell me what is the problem in this code as i am not able to fetch the
data from... tutorial go through the link
JSP Get
Data Into Dropdown list
From Database
 
retrive data from database?
retrive data from database? hellow
i have a
database sheet name..... now i want
retrive sn,roll no and name and
textbox
like....,rllno,and name
retrive
retrive data from database using jsp in struts?
retrive data from database using
jsp in struts? *search.jsp*
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<... searchProduct(SearchDTO sdto) {
String query="select *
from product
retrive data from database
retrive data from database hi..
i made a application form. it's have attribute s.no,name,roll no and i enter a few records. now i want to view all record not in
database access sheet i want to view it at any another
url parameter using retrive data from database in jsp
url parameter using
retrive data from database in jsp The user can... clicks the Preview button, you have to create a dynamic
jsp which should read the contents
from the db based on the event id. But this
jsp url should be a public url
url parameter using retrive data from database in jsp
url parameter using
retrive data from database in jsp the user can... clicks the Preview button, you have to create a dynamic
jsp which should read the contents
from the db based on the event id. But this
jsp url should be a public
GWT -- retrive the data from Database
GWT --
retrive the
data from Database the user can create an event... button, you have to create a dynamic
jsp which should read the contents
from the db based on the event id. But this
jsp url should be a public url. Means anyone
Getting Textbox data from database
Getting
Textbox data from database When i m trying to get
data in
textbox as readonly
from database i m getting following error.and my code is shown...
data from database and display it on the textboxes.
<%@page language="java
Getting Textbox data from database
;Here is a
jsp code that retrieves
data from database and display...Getting
Textbox data from database When i m trying to get
data in
textbox as readonly
from database i m getting following error.and my code is shown
retrive data from oracle to jsp
retrive data from oracle to jsp i am a beginer in
jsp so please help... cost and manager name and storing it in the
data base.
in search proj fiel...();
psmt= conn.prepareStatement("select *
from CR_EMPLOYEE_DETAILS
how to insert data into database using jsp & retrive
how to insert
data into
database using
jsp & retrive Hello,
I have created 1 html page which contain username, password & submit button. in my oracle10G
database already contain table name admin which has name, password
Use of Select Box to show the data from database
Use of Select Box to
show the
data from database
 ...
from database
This example will describe you the use of Select Box in a
JSP page to
show
the
data fetched
from the
database. We are using Servlet to get
data