Hi guys

I am trying to write some code here in jsp. I want to select some employee using its attributes. The first option is to select the criteria on which I want to base my search e.g. EMP ID. When I select this it should load for me all the data in that particular column in the database to the adjacent textbox. If I select EMP NAME, It should load for me all the data in the empname column in the database and display it in adjacent textbox and so on.

Here is my code and any help will be appriciated.Database name: employ, table name:emp and the fields are:

    <html>
    `    <body bgcolor="#d0d0d0">`
            <br></br>
            <br></br>
            <br></br>
            <h2 align="center">Enter Farmer details</h2>
            <form name="searchfarmer" action="search" method="post">
                <table border="2" width="300" align=center bgcolor="#e9fh">

                            <%
                                        Connection con = null;
                                        PreparedStatement stmt = null;
                                        Statement st = null;
                                        ResultSet rs = null;
                                        String search = request.getParameter("search");
                                        String field = "";
                                        String category = "";




                                        try{
                                            Class.forName("com.mysql.jdbc.Driver");
                                            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employ", "root", "");
                                            st=con.createStatement();
                                            String id=session.getValue("login").toString();
                                            String str = "Select * from emp where search ="+search;
                                            rs =st.executeQuery(str);

                                             //  if () {
                                                   field=rs.getString(1);
                                            while(rs.next() ){
                                              field = rs.getString(1);
                                              out.println(field);  

                                            }
    //                                        elseif{
    //                                            rs = st.executeQuery("Select empno from emp");
    //                                        }
                                        }
                                        catch(Exception e){e.printStackTrace();

                                        }
                                    %>
                                    <tr><td>
                            <b>Select Category:</b>
                            <select name="search">
                                <option value="">
                                <option value="code">EMP NO
                                <option value="name">EMP NAME
                                <option value="category">CATEGORY

                        </td>
                        <td>
                            <b>Fields:</b>
                            <select name="search">
                                <option value=field>


                        </td></tr>
                </table>
                <center>
                    <input type="submit" value="Search"/>
                    <input type="reset" value="Reset"/>
            </form>
        </body>
    </html>

Recommended Answers

Well, don't use scriptlets. Either use a Bean, or google for the sql jstl tag package.

Jump to Post

All 2 Replies

Well, don't use scriptlets. Either use a Bean, or google for the sql jstl tag package.

saying "it should do so and so" without mentioning what it is doing currently (does it run? does it return a (wrong) value? is an exception thrown? .... "
is a bit pointless. we don't have your setup (config, db, ... ) so we can't just copy paste your code and test it, even if we wanted to.
for jsp related material, you could post your next questions in the jsp forum

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.