I make servlet and in servlet we make dropdown menu for display names against id but when we run the servlet drop down menu created but no values displayed form database in dropdown menu so pls help me. sorry for bad english

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.management.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class DeleteProductServlet extends HttpServlet {

      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            DBManager db=new DBManager();
           Connection conn=db.getConnection();
           Statement stmt=conn.createStatement();





          String product1="product";
            out.println("<select name='" + product1 + "'>");
                String sql="SELECT * FROM TBL_PRODUCTS";
                    rs=stmt.executeQuery(sql);
                    while(rs.next())
                    {
                        String id=rs.getString("ID");

                        String name=rs.getString("NAME");
                        out.println("<option  values" +id +name+"</option>");

                    }
                 out.println("</select>");
                    }


 finally {
            out.close();

        }
    }



}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Delete everything you have written.
Start by studying html, because your tags are wrong.

Then put the "database" code in a separate method that returns a list with the values from the DB. Better create a class with attributes ID and NAME and return a list of that object. In that method you MUST close the connection, the statement, and the ResultSet.

Never write html code in a servlet. Create a JSP file and call that method in that jsp and take the list with the data from the DB. Don't mix database with presentation. One method returns the data, then you get to call that method from wherever you want. In this case you call it from the JSP and dislay that data.

You will need to learn about scriplets. Assuming that you have a class called Product with attributes ID and NAME and you have created get/set methods and you have a List with data from the DB, then in the jsp:

<%
ArrayList list = getDataFromDB();
%>

<select name="product">
<option value="">Select a product</option>

<%
for (int i=0;i<list.size();i++) {
   Product prd = (Product)list.get(i);
%>
   <option value="<%=prd.getId()%>" ><%=prd.getName()%></option>
<%
}
%>

</select>

You have the query and you know how to run it, just create a method and call it from there instead from inside the servlet. You should know how to create classes on your own. If you don't then stop learning about web and go back to basics.
You have the class, you have the query, just put the results in a ArrayList. Again if you don't know how to do this stop what you are doing because these are basic stuff.

This article has been dead for over six months. Start a new discussion instead.