如何提交从jsp页面下拉列表中选择的选项来执行mysql查询 [英] how to submit the option selected from drop down list in jsp page to perform a mysql query

查看:92
本文介绍了如何提交从jsp页面下拉列表中选择的选项来执行mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是StaffAllocation,我想从数据库中检索信息。我很新,这是我第一个项目。我创建了一个下拉列表,从我的表中检索 staffnames 。现在我想执行一个查询操作,从下拉列表中查看所选 staffnames 的详细信息。以下是我有的编码,这是不正确的:

$ b

 <%@ page language =javacontentType =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; %GT;
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1>
< title>员工资料< / title>
< / head>
< BODY>
< form method = post>
< h3>选择Stafftype:< / h3>
< p>< input type =radioname =Stafftypevalue =Male>男性< /输入>< / p为H.
< p>< input type =radioname =Stafftypevalue =Female>女性< /输入>< / p为H.
< input type =submitvalue =提交>
< / form>
<%
尝试{

Class.forName(com.mysql.jdbc.Driver);
连接连接= 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(你的编码错了);
}
%>

< select> <%while(resultset.next()){%>
< / option><%= resultset.getString(2)%>< / option>
<%}%>

<%

字符串StaffName = request.getParameter(StaffName);
int staffId;
字符串子代码;
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其中StaffName ='request.getParameter('StaffName')');


$ b catch(例外e)
{
out.println(错误输入+ e);
}
%>
< form method =get>
< br>< br>
< input name =Submittype =buttonvalue =Submit>
< / form>
< / body>
< / html>`

表格:
$ (1).startId(2).StaffName(3).Stafftype(男性或女性)

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

解决方案

我已经摆弄示例实现供您参考。你可以这样实现。所有你需要做的就是包含jQuery插件。示例代码lke this,

  $ .ajax({
url:'ur_servlet_url'+ selValue,
键入:POST,
async:false,
success:function(data){
//示例数据
var data =< select id ='child'> ;
< option value ='11'> Value11< / option>< / select>
$(#fillValue)。html(data);
}
});

您需要像选择标记一样编写java响应并将其返回给您的ajax响应。最后你可以像这样填写第二个下拉列表。让我知道这是否有帮助。


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:

<%@ 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)

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

解决方案

I have fiddled sample implementation for your reference. You can implement like that. All you need to do is include jQuery plugin. Sample code lke this,

 $.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.

这篇关于如何提交从jsp页面下拉列表中选择的选项来执行mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆