comboboxes for storing in database
how to access values selected from connected comboboxes for storing in database...i'm attatching a code i got from net for connected comboboxes...from this how could i access country and state....please help me....
1)state.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp
var xmlHttp
function showCity(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="city.jsp";
url +="?count=" +str;
xmlHttp.onreadystatechange = stateChange1;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange1(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("city").innerHTML=xmlHttp.responseText
}
}
</script>
</head>
<body>
<select name='state' onchange="showCity(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 state");
while(rs.next()){
%>
<option value="<%=rs.getString("stateid")%>"><%=rs.getString("state")%></option>
<%
}
%>
</select>
<br>
<div id='city'>
<select name='city' >
<option value='-1'></option>
</select>
</div>
</body>
</html>
2)city.jsp:
<%@page import="java.sql.*"%>
<%
String state=request.getParameter("count");
String buffer="<select name='city'><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 city where stateid='"+state+"' ");
while(rs.next()){
buffer=buffer+"<option value='"+rs.getString(2)+"'>"+rs.getString(3)+"</option>";
}
buffer=buffer+"</select>";
response.getWriter().println(buffer);
}
catch(Exception e){
System.out.println(e);
}
%>
View Answers
March 15, 2012 at 11:07 AM
1)country.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp
var xmlHttp
function showState(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="state.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("state").innerHTML=xmlHttp.responseText
}
}
</script>
</head>
<body>
<select name='country' onchange="showState(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 country");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
<%
}
%>
</select>
<br>
<div id='state'>
<select name='state' >
<option value='-1'></option>
</select>
</div>
</body>
</html>
2)state.jsp:
<%@page import="java.sql.*"%>
<%
String country=request.getParameter("count");
String buffer="<select name='state' ><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 state where countryid='"+country+"' ");
while(rs.next()){
buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>";
}
buffer=buffer+"</select>";
response.getWriter().println(buffer);
}
catch(Exception e){
System.out.println(e);
}
%>
For the above code, we have created two database tables:
CREATE TABLE `country` (
`countryid` bigint(255) NOT NULL auto_increment,
`countryname` varchar(255) default NULL,
PRIMARY KEY (`countryid`));
CREATE TABLE `state` (
`stateid` bigint(255) NOT NULL auto_increment,
`countryid` int(255) default NULL,
`state` varchar(255) default NULL,
PRIMARY KEY (`stateid`));
Related Tutorials/Questions & Answers:
Uploading a Software and storing in the databaseUploading a Software and
storing in the database I want to upload a software(may be of maximum 20mb) through JSP, and store it in the
database.
The coding present in the site for uploading and
storing in the
database Advertisements
storing xml into database - XMLstoring xml into
database hi i have an xml file it contains elements with attributes as well as nested elements so how to go forward .......with it i know how to persist a simple xml file into data base but i m finding some
getting and storing dropdown list in database in jspgetting and
storing dropdown list in
database in jsp i have a drop down list to select book from
database. i'm able to retrieve dropdown list from... lyk this. but it
storing null value in the
database. :\
what to do
complex xml parsing and storing in database - XMLcomplex xml parsing and
storing in database Hi Experts ,i want to parse my xml document and store it in mysql
database. This is my code. How to parse this complex data.
EDI_DC40
800
0000000000557748
Storing records of a file inside database table in javaStoring records of a file inside
database table in java Here is my requirement,
I have a file which contains some number of records like... the records inside the
database except headings (Here sid,sname,age are headings
Error in Storing date - Development processError in
Storing date Hi, U have given this code for
storing date in
database. But this is not working . i want it with Statement...,
MS Access
Database sets the date in DD/MM/YYYY format. Take field id of text
storing data into flat filesstoring data into flat files how can i retrive data from
database and store data in flat files
Hi Friend,
Try the following code:ADS_TO_REPLACE_1
import java.io.*;
import java.sql.*;
import java.util.*;
class
Remote file storing in mobileRemote file
storing in mobile Hai.. i want to do my semester project..i have selected a topic "Remote file
storing in mobile"..it is in the form like any Java enabled GPRS based mobile phone users can store their images,video
Remote file storing in mobileRemote file
storing in mobile Hai.. i want to do my semester project..i have selected a topic "Remote file
storing in mobile"..it is in the form like any Java enabled GPRS based mobile phone users can store their images,video
Storing and Reading data Storing and Reading data Hello, I'm developing a GUI application... that inserts jtable data to
database.
import javax.swing.*;
import javax.swing.table.... is an example that retrieves data from
database and stored into jtable.
import
storing data in xml - XMLstoring data in xml Can u plz help me how to store data in xml using java Hi Friend,
Try the following code:
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import
storing images in directory,saving path in db2storing images in directory,saving path in db2 i am working in a web portal that makes a simple networking site. i want to store the profiles images in a folder and its path(relative/absolute) in my DB2
database. and when
Reading Text file and storing in mapReading Text file and
storing in map Hi
I have multiple text files. I want to read thoses files and store those records in map.
Map will be of "LinkedHashMap<String, Map<String, String>>" type.
Please let me know
databasedatabase the code for initializing the
database connection
databasedatabase i want to let the user select the name of
database and then delete that
database.......im doing project in swings netbeans
Why string is storing null value - IoCWhy string is
storing null value I am reading lines from a file...! then it is
storing it in a temp variable.... but when i am running the program it is also
storing null value in temp. WHY so
databasedatabase use of hyperlink to show the data from
database databasedatabase i need to insert time in my
database. i need a code to insert time in my
database. its very urgent
databasedatabase im doing my project in netbeans swings...wn a user wants to create a
database from the gui...i want to display an error msg if a
database with that name already exists and if it does not exist new
database should
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase I wanted to know if it is possible to create a
database in mysql by letting the user enter the name of the
database in swing gui..im doing my project in netbeans...Thank You
databasedatabase I wanted to know if it is possible to establish
database connection on a remote pc with mysql using java swings from netbeans and then create a
database on the remote pc....
Kindly help me
DATABASEDATABASE How can i get combo box values from
database??
or how can i get values in the drop down menu of the html which is similar to dat of combo box in java - from
database DatabaseDatabase How to represent data from
database in a tree?
Please visit the following link:
Retrieve data from
database in tree using Java Swing
DatabaseDatabase I am working in jsf2.Fro a demo i created
database in ms access.no wi want to retrive a record from
database for a particular data.I need help as I am stuck
DatabaseDatabase from java code i have to retrieve some data from a
database, which is present in a different timezone.
Scenario :- My
database... in
database timezone is 9PM 15APR2012. I want to retrieve something from
database DatabaseDatabase Can i get a code for
database connectivity
Myeclipse 3.3 to MySql manager 2005
databasedatabase how we ca upload the data in the
database file(ms access
DATABASEDATABASE I can't send different information from different form into a single table in my
database databasedatabase dateofbirth is not save into the
database while i am enter from html browser.it shows as null.whats the reason
databasedatabase sir..
how we can uplaod the
database in the data file(ms access)please send me a solution
databasedatabase hi i want to develop one patient
database can any one provide me the guide
databasedatabase In my project where u helped me to display the databases present im mysql
database on the Jlist and then the respective tables in that
database on another jlist and then the column names on another jlist
I dont want
databasedatabase In my project,i want the user to specify the name of the
database he wants to create in mysql and then also the name of the table along with the column names he wants in that
database....Im doing my project in netbeans
databasedatabase Im doing my project in java swings...netbeans
I wanted java source code to establish connection to postgresql_8.1 using jdbc and create a user defined
database and a table in that
database and insert values
Plz help me
databasedatabase Can you please help me to establish
database connection to ms sql 2000 from java swing in netbeans