3
我有一個名爲updategroup.jsp網頁,在下拉列表中選擇基於價值的文本框的值,我有一個組合框稱爲組名,我得到所有可用的組名從數據庫到這下拉框中的基礎上,在下拉框中選擇的價值,我必須來從數據庫中的數據,我應該在顯示文本框和我需要更新的MySQL table.But我的問題是,如果我選擇第一個組合框的值我得到的值每當我在組合框中選擇第二個值時,在textbaoxes中顯示前面的值,並且總的mysql表正在更新。下面是updategroup.jsp代碼:如何讓這個用jsp和mysql
<%@page import="java.sql.*"%>
<%@page import="javax.servlet.*"%>
<%
Connection con=null;
String sqlQuery="";
Statement st=null;
ResultSet rs=null;
String groupname_var="";
String data_firstname="";
String data_lastname="";
String data_middlename="";
String data_add1="";
String data_add2="";
String data_city="";
String data_state="";
String data_country="";
int data_zip=0;
String data_phone="";
String data_mn="";
String data_emailid="";
%>
<html>
<head>
<title>edit user</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<style>
td{FONT: 13px Arial,sans-serif;}
th{FONT:bold 13px Arial, sans-serif;color:white;background-color:#153e7e}
input{FONT: 13px Arial,sans-serif;border: solid 1;}
select{font: 13px Arial,sans-serif}
#button{cursor:hand}
A:hover {COLOR: red}
textarea {
resize: none;
}
td{
border:none;
}
</style>
<script type="text/javascript">
function reset()
{
alert("etst");
document.getElementByID("subzone").value="";
}
</script>
<script type="text/javascript">
window.history.forward();
function noback() { window.history.forward(); }
</script>
<script type="text/javascript">
function myoriginaldata()
{
document.getElementById("fn").value=document.getElementById("hfn").value;
document.getElementById("ln").value=document.getElementById("hln").value;
document.getElementById("mn").value=document.getElementById("hmn").value;
document.getElementById("add1_id").value=document.getElementById("hadd1").value;
document.getElementById("add2_id").value=document.getElementById("hadd2").value;
document.getElementById("city_id").value=document.getElementById("hcity").value;
document.getElementById("state_id").value=document.getElementById("hstate").value;
document.getElementById("country_id").value=document.getElementById("hcountry").value;
document.getElementById("zip_id").value=document.getElementById("hpostalcode").value;
document.getElementById("phone_id").value=document.getElementById("hphoneno").value;
document.getElementById("mobile_id").value=document.getElementById("hmobileno").value;
document.getElementById("email_id").value=document.getElementById("hemailid").value;
}
</script>
</head>
<form method="post" name="frm_addUser" action="./updategroup.jsp">
<table width="40%;" border="0" cellpadding="5" cellspacing="1" bgcolor="" bordercolor="#333366" align="center">
<tbody>
<tr><th colspan="2" align="middle"><font size="4">Edit Group</font></th></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">Group Name</font></td>
<%
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
sqlQuery="select groupname from grouplist";
st=con.createStatement();
rs=st.executeQuery(sqlQuery);
%>
<td><select name="group_name" id="gid" style="WIDTH: 200px" onchange="this.form.submit()">
<option value="">Select groupname</option>
<% while(rs.next()) {%>
<option value="<%=rs.getString("groupname")%>"><%=rs.getString("groupname")%></option>
<% }
rs.close();
st.close();
con.close();
%>
</td>
</tr>
<%
groupname_var=request.getParameter("group_name");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
sqlQuery="select firstname,middlename,lastname,address1,address2,city,state,country,zipcode,phoneno,mobileno,emailid from groupdata where group_name='"+groupname_var+"'";
st=con.createStatement();
rs=st.executeQuery(sqlQuery);
while(rs.next())
{
data_firstname=rs.getString("firstname");
data_lastname=rs.getString("lastname");
data_middlename=rs.getString("middlename");
data_add1=rs.getString("address1");
data_add2=rs.getString("address2");
data_city=rs.getString("city");
data_state=rs.getString("state");
data_country=rs.getString("country");
data_zip=rs.getInt("zipcode");
data_phone=rs.getString("phoneno");
data_mn=rs.getString("mobileno");
data_emailid=rs.getString("emailid");
}
rs.close();
st.close();
con.close();
%>
<input type="hidden" id="hgroupname_id" value="<%=groupname_var%>">
<tr>
<td bordercolor="Gainsboro"><font size="4">First Name </font></td>
<td bordercolor="Gainsboro"><input name="firstname" style="WIDTH: 200px" value="<%=data_firstname%>" id="fn"></td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">Middle Name </font></td>
<td bordercolor="Gainsboro"><input name="middlename" style="WIDTH: 200px" value="<%=data_middlename%>" id="mn"></td></tr>
<tr>
<td colspan="1" bordercolor="Gainsboro"><font size="4">Last Name </font>
<td bordercolor="Gainsboro"><input name="lastname" style="WIDTH: 200px" type="text" value="<%=data_lastname%>" id="ln">
</td>
</tr>
<tr>
<td colspan="1" bordercolor="Gainsboro"><font size="4" color="green"><i>Address</i></font>
</td>
<td bordercolor="Gainsboro">
</td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">Line 1</font></td>
<td bordercolor="Gainsboro"><input name="lineone" style="WIDTH: 200px" value="<%=data_add1%>" id="add1_id"></td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">Line 2</font></td>
<td bordercolor="Gainsboro"><input name="linetwo" style="WIDTH: 200px" value="<%=data_add2%>" id="add2_id"></td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">City</font></td>
<td bordercolor="Gainsboro"><input name="city" style="WIDTH: 200px" value="<%=data_city%>" id="city_id"></td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">State</font></td>
<td bordercolor="Gainsboro"><input name="state" style="WIDTH: 200px" value="<%=data_state%>" id="state_id"></td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">Country</font></td>
<td bordercolor="Gainsboro"><input name="country" style="WIDTH: 200px" value="<%=data_country%>" id="country_id"></td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">Pin/Zip Code </font></td>
<%
if(data_zip!=0)
{ %>
<td bordercolor="Gainsboro"><input name="postalcode" style="WIDTH: 200px" value="<%=data_zip%>" id="zip_id"></td></tr>
<%
}
%>
<tr>
<td bordercolor="Gainsboro"><font size="4">Phone No</font></td>
<td bordercolor="Gainsboro"><input name="phoneno" value="<%=data_phone%>" style="WIDTH: 200px" id="phone_id"></td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">Mobile No</font></td>
<td bordercolor="Gainsboro"><input name="mobileno" style="WIDTH: 200px" value="<%=data_mn%>" id="mobile_id"></td></tr>
<tr>
<td bordercolor="Gainsboro"><font size="4">E-Mail </font></td>
<td bordercolor="Gainsboro"><input name="email" style="WIDTH: 200px" value="<%=data_emailid%>" id="email_id"></td></tr>
<tr>
<td align="center" colspan="2" bordercolor="#f8f8ff">
<input type="submit" value="Update" style="BORDER-RIGHT: green 1px solid; BORDER-TOP:GREEN 1px solid; FONT: GREEN 8pt Tahoma; BORDER-LEFT: GREEN 1px solid; WIDTH: 90px; BORDER-BOTTOM: GREEN 1px solid; HEIGHT: 34px" name="bOk" onclick="return checkInput();">
<input type="reset" value="Cancel" style="BORDER-RIGHT: GREEN 1px solid; BORDER-TOP:GREEN 1px solid; FONT: GREEN 8pt Tahoma; BORDER-LEFT: GREEN 1px solid; WIDTH:90px; BORDER-BOTTOM: GREEN 1px solid; HEIGHT: 34px" name="cancel">
</td></tr>
<%
try
{
String fn_var=request.getParameter("firstname");
String ln_var=request.getParameter("lastname");
String mn_var=request.getParameter("middlename");
String add1_var=request.getParameter("lineone");
String add2_var=request.getParameter("linetwo");
String city_var=request.getParameter("city");
String state_var=request.getParameter("state");
String country_var=request.getParameter("country");
String zipcode_var=request.getParameter("postalcode");
String phone_var=request.getParameter("phoneno");
String mobile_var=request.getParameter("mobileno");
String email_var=request.getParameter("email");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
st=con.createStatement();
%>
<input type="hidden" id="hfn" value="<%=fn_var%>">
<input type="hidden" id="hmn" value="<%=mn_var%>">
<input type="hidden" id="hln" value="<%=ln_var%>">
<input type="hidden" id="hadd1" value="<%=add1_var%>">
<input type="hidden" id="hadd2" value="<%=add2_var%>">
<input type="hidden" id="hcity" value="<%=city_var%>">
<input type="hidden" id="hstate" value="<%=state_var%>">
<input type="hidden" id="hcountry" value="<%=country_var%>">
<input type="hidden" id="hpostalcode" value="<%=zipcode_var%>">
<input type="hidden" id="hphoneno" value="<%=phone_var%>">
<input type="hidden" id="hmobileno" value="<%=mobile_var%>">
<input type="hidden" id="hemailid" value="<%=email_var%>">
<%
if(groupname_var!=null && fn_var!=null && mn_var!=null && ln_var!=null && add1_var!=null && add2_var!=null && city_var!=null && state_var!=null && country_var!=null && zipcode_var!=null && phone_var!=null && mobile_var!=null && email_var!=null)
{
if(!groupname_var.equals("") && !fn_var.equals("") && !ln_var.equals("") && !mn_var.equals("") && !add1_var.equals("") && !add2_var.equals("") && !city_var.equals("") && !state_var.equals("") && !country_var.equals("") && !zipcode_var.equals("") && !phone_var.equals("") && !mobile_var.equals("") && !email_var.equals(""))
{
%>
<script>
myoriginaldata();
</script>
<%
st.executeUpdate("update groupdata set firstname='"+fn_var+"',lastname='"+ln_var+"',middlename='"+mn_var+"',address1='"+add1_var+"',address2='"+add2_var+"',city='"+city_var+"',state='"+state_var+"',country='"+country_var+"',zipcode='"+zipcode_var+"',phoneno='"+phone_var+"',mobileno='"+mobile_var+"',emailid='"+email_var+"' where group_name='"+groupname_var+"'");
}
}
st.close();
con.close();
}
catch(Exception e)
{
}
%>
<script>
document.getElementById("gid").value=document.getElementById("hgroupname_id").value;
</script>
</tbody>
</table>
</form>
</div>
</body>
</html>
please help me to solve this.
感謝您的回覆nipun但如何做到這一點與JSP,因爲我使用JSP –
可以用哪裏是問題的伴侶.. –
谷歌,你會發現許多例子 –