gpt4 book ai didi

java - 如何提交从jsp页面中的下拉列表中选择的选项以执行mysql查询

转载 作者:行者123 更新时间:2023-11-30 23:10:58 26 4
gpt4 key购买 nike

我的项目是 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>`

表格:

tblstaffdetails -(1).staffId(2).StaffName(3).Stafftype(男或女)

tblsubhandled - (1).staffId(2).subcode

最佳答案

我摆弄了sample implementation供你引用。你可以这样实现。您需要做的就是包含 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);
}
});

您需要像 select 标记一样编写您的 java 响应并将其返回到您的 ajax 响应。最后你可以像那样填写第二个下拉列表。如果这有帮助,请告诉我。

关于java - 如何提交从jsp页面中的下拉列表中选择的选项以执行mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19762001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com