Java 如何提交从jsp页面下拉列表中选择的选项以执行mysql查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19762001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to submit the option selected from drop down list in jsp page to perform a mysql query
提问by user2951465
My project is StaffAllocation, and I want to retrieve information from the database. I'm very new and this is my very first project. I created a drop down list retrieving staffnames
from one of my table. Now I want to perform a query action to view the details of the selected staffnames
from the drop-down list. The following is the coding which i have, which is not correct:
我的项目是 StaffAllocation,我想从数据库中检索信息。我很新,这是我的第一个项目。我创建了一个staffnames
从我的一个表中检索的下拉列表。现在我想执行一个查询操作来查看staffnames
从下拉列表中选择的详细信息。以下是我的编码,这是不正确的:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.lang.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%ResultSet resultset =null; %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Staff Details</title>
</head>
<BODY>
<form method=post>
<h3>Select Stafftype:</h3>
<p><input type="radio" name="Stafftype" value="Male"> Male</input></p>
<p><input type="radio" name="Stafftype" value="Female"> Female</input></p>
<input type="submit" value="Submit">
</form>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation? user=root&password=success");
Statement statement = connection.createStatement() ;
String Stafftype= request.getParameter("Stafftype");
out.print(Stafftype);
if(Stafftype.contentEquals("Male")){
resultset=statement.executeQuery("select * from tblstaffdetails where Stafftype= 'Male'");
}
else if(Stafftype.contentEquals("Female")){
resultset=statement.executeQuery("select * from tblstaffdetails where Stafftype= 'Female'");
}
else
{
System.out.println("your coding is wrong");
}
%>
<select> <% while(resultset.next()){ %>
<option><%= resultset.getString(2)%></option>
<%} %>
<%
String StaffName= request.getParameter("StaffName");
int staffId;
String subcode;
if(StaffName != null) {
resultset=statement.executeQuery("SELECT a.staffId, a.StaffName, b.subcode FROM tblstaffdetails a LEFT JOIN tblsubhandled b ON a.staffId = b.staffId where StaffName='request.getParameter('StaffName')'");
}
}
catch(Exception e)
{
out.println("wrong entry"+e);
}
%>
<form method = "get">
<br><br>
<input name="Submit" type="button" value="Submit">
</form>
</body>
</html>`
Tables:
表格:
tblstaffdetails -(1).staffId(2).StaffName(3).Stafftype(male or female)
tblstaffdetails -(1).staffId(2).StaffName(3).Stafftype(男或女)
tblsubhandled - (1).staffId(2).subcode
tblsubhandled - (1).staffId(2).subcode
采纳答案by Vinoth Krishnan
I have fiddled sample implementationfor your reference. You can implement like that. All you need to do is include jQuery plugin. Sample code lke this,
我已经摆弄了示例实现供您参考。你可以这样实现。您需要做的就是包含 jQuery 插件。像这样的示例代码,
$.ajax({
url : 'ur_servlet_url' + selValue,
type : "POST",
async : false,
success : function(data) {
//Sample data
var data = "<select id='child'>
<option value='11'>Value11</option></select>"
$("#fillValue").html(data);
}
});
You need to compose your java response like select tag and return it to your ajax response. Finally you can fill the second dropdown like that. Let me know if this helps.
您需要编写像 select 标记一样的 java 响应并将其返回到您的 ajax 响应。最后,您可以像这样填充第二个下拉列表。如果这有帮助,请告诉我。
回答by Govan
<form method="post" action="select.jsp">
<select name="sell">
<option value="Alto">Alto</option>
<option value="Esteem">Esteem</option>
<option value="Honda City">Honda City</option>
<option value="Chevrolet">Chevrolet</option>
</select>
<br>
<input type="Submit" value="Submit">
</form>
<%
String st=request.getSelectedIndex("sell");
if(st!=null){
out.println("You have selected: "+st);
}
%>