data should not repeat in the drop down list when it is loading dynamically from database
selDept.jsp
<%@page import="java.sql.*"%>
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp
var xmlHttp
function showEmp(str){
if (typeof XMLHttpRequest != "undefined"){
xmlHttp= new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp==null){
alert("Browser does not support XMLHTTP Request")
return;
}
var url="selEmp.jsp";
url +="?count=" +str;
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange(){
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("emp").innerHTML=xmlHttp.responseText
}
}
</script>
</head>
<body>
<select name='dept' onchange="showEmp(this.value)">
<option value="none">Select</option>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from dept");
while(rs.next()){
%>
<option value="<%=rs.getString("DEPT_NO")%>"><%=rs.getString("DEPT_NAME")%></option>
<%
}
%>
</select>
<br>
<div id='emp'>
<select name='emp' >
<option value='-1'></option>
</select>
</div>
</body>
</html>
selEmp.jsp
<%@page import="java.sql.*"%>
<%
String no=request.getParameter("count");
String buffer="<select name='emp' ><option value='-1'>Select</option>";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from emp where DEPT_NO='"+no+"' ");
while(rs.next()){
buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString("EMP_NAME")+"</option>";
}
buffer=buffer+"</select>";
response.getWriter().println(buffer);
}
catch(Exception e){
System.out.println(e);
}
%>
Data base tables are
dept
CREATE TABLE `dept` (
`DEPT_NO` int(100) default NULL,
`DEPT_NAME` varchar(255) default NULL
);
emp
CREATE TABLE `emp` (
`EMP_NO` int(10) NOT NULL auto_increment,
`EMP_NAME` varchar(100) default NULL,
`DESIGNATION` varchar(100) default NULL,
`JOINING_DATE` date default NULL,
`SALARY` int(100) default NULL,
`DEPT_NO` int(100) default NULL,
`DEPT_NAME` varchar(100) default NULL,
PRIMARY KEY (`EMP_NO`)
);
The above code is working for getting the data dynamically for database but my problem is in the list the same department names are repeatedly adding when ever we insert the same same name (i.e when ever in the registration i give for 4 employees same department name then that name appears for 4 times )actually it should not happen.When once that name is inserted another time it should not come only once it has to come and the persons who are working in that department has to come.
I hope you understand my question.
if the posting of the question is not proper please forgive me.I rather tried to place it right.
Please help me out and send the code as soon as possible.
Thank you in advance
View Answers
Related Tutorials/Questions & Answers:
Advertisements
view data from database using drop down listview
data from database using
drop down list hi i want to view the
data from database by selecting a value in a
drop down list. for an example
drop down list have picture element.when click it select pictures
from the
database Drop down list from databaseDrop down list from database Hi, Can I know how do we get the
drop down list from database? Eg: select country--select state--select district--so... *
from country");
while(rs.next()){
%>
<option value="<
store values of drop down list box in database store values of
drop down list box in database how to store values of
drop down list box in oracle
database in jsp?I have information inserting form where i have date of birth as
drop down list box
JSP Get Data Into Dropdown list From DatabaseJSP Get
Data Into Dropdown
list From Database
In this section we will discuss about how to fetch
data dynamically into the
dropdown
list in JSP... for fetching
data from the
database and set
it into the dropdown
list in JSP
Get values in drop down listGet values in
drop down list Pls provide me jsp code to get values in
drop down list from another table's field.
my project has customer registration and company registration pages.....
when i insert
data in company
dynamic drop down listdynamic
drop down list I want to create 2
drop down list, where it takes value
from database and the two
list are dependent..means if I select a particular value
from one the other
list is developed accordingly..depending
Dependent drop down list dependent
drop down list is used my code is successful but
when i select class
from first
drop down list all the
data get lossed means the value entered in fields before
drop down list get blank.what can i do for this?
This is the code
validation for drop down listvalidation for
drop down list Hi all
I have a form, it contain 12
drop down list. Each
drop down has 1 to 5 values,i need to validate this form. the following condition
should be true.
1.you can choose only 3 times 5 value.
2
Dependant & dynamic drop down listDependant & dynamic
drop down list I don't know this
should be in this or AJAX forum.
I have one dynamic
drop down list from data base (working... on the values selected by user in first
drop down list.
How to achieve this ?
Dynamically Update textbox from databaseDynamically Update textbox
from database I have a
database as shown below.(
database created using SQL Server 2005)
name : george,simon
address... to that typed value
from database has to be displayed in another text box
Excel Cell Drop Down ListExcel Cell
Drop Down List
In this section, you will learn how to validate
data entered in a cell and
provide a
drop down list of possible values to the user... in a excel sheet's
cell and also can provide
drop list of possible value
dynamic drop down list box - Java Beginnersdynamic
drop down list box hi all ,
I want to
dynamically populate a
drop down box
from an sql query in a servlet program, using only html...();
ResultSet rs = st.executeQuery("select *
from Combolist");
List ulist = new
Select functionality of drop down list - Struts(in struts application) in which if user select HIDE
from the
drop down list... have
Drop down list having element Test1,Test2,Test3,HIDE and one Text msg box for entering Age,Now if user select "HIDE"
from drop-
down list the below text
Dependant & dynamic drop down list - Follow upDependant & dynamic
drop down list - Follow up Thanks for your answer. As per your answer
This will select only ONE row (country)
from first
drop down. In my case user can choose more than one row , which is then sent to next