如何使用jsp中的下拉列表,文本框和搜索按钮获取数据

时间:2010-12-11 20:24:50

标签: javascript mysql jsp

我想使用jsp,javascript和mysql显示数据。我的问题是如果我选择一个条目,从下拉列表中选择'title'并在文本框中给出标题名称'partial dif',然后单击搜索按钮,它应该显示所有以字母'....开头的标题名称”。请帮帮我............

1 个答案:

答案 0 :(得分:1)

假设您有名为form.jsp,form1.jsp ...的文件以及包含列类型和名称的表your_table。

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>form.jsp</title>
    </head>
    <body>
        <form action="form1.jsp" method="post" name=frm>
        <select name="list">
            <option>title</option>
            <option>author</option>
            <option>publication</option>
        </select>
        Name<input type="text" name=txt value=""/>
        <input type="submit" vaue="GO"/>
        </form>
    </body>
</html>

form2.jsp

    <%String list1=request.getParameter("list");
         String name=request.getParameter("txt");
         name1=name.substring(0,3);
          Connection con;
                 PreparedStatement ps,ps1,ps2;
                 ResultSet rs,rs2;
       try{
                 String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
                 String url="jdbc:odbc:rail";
            String username="root";
            String password="root";
            Class.forName(driverName);
            con=DriverManager.getConnection(url,username,password);
             ps=con.prepareStatement("Select name from your_table where type=? and name like ?%);
             ps.setString(1,list1);
             ps.setString(2,name1);
             rs=ps.executeQuery();
             while(rs.next())
             {
               out.println(rs.getString(1));
               }

            }
         catch(Exception e)
         {out.println(e);}   
%>