1

hi i am trying to fill html drop down with mysql data but i am getting error i am using html on java page by appending string and call that method having string builder in which i appent html calling it on jsp page my code is,

      html.append("<select name='pic'>");
  html.append("<option value='none'>Select</option>  ");
  html.append("<%");
  html.append("Class.forName('com.mysql.jdbc.Driver').newInstance();  ");
  html.append("Connection con = DriverManager.getConnection('jdbc:mysql://192.168.1.104:3306/networkmonitoring','mohsin','123456');");
  html.append("Statement stmt = con.createStatement();  ");
  html.append("ResultSet rs = stmt.executeQuery('Select objecttype_name from network_objecttype');");
  html.append("while(rs.next()){");
      html.append("%>");
      html.append("<option value='<%=rs.getString('objecttype_name')%>'><%=rs.getString('objecttype_name')%></option>");
      html.append("rs.getString(1)");
       html.append("<%");
  html.append("}");
 html.append("%>");
  html.append("</select>");

but i am not able to get data from mysql and just getting this in dropdown,

'><%=rs.getString('objecttype_name')%>

hopes for your reply

Thanks in Advance!

Syed Raza
  • 321
  • 2
  • 12
  • 34

2 Answers2

1

change your html page into jsp and follow this code:

<%@page import="java.sql.*"%>
<html>
<form name="form" method="post" >
<b>Select a country:</b> </td>
<select name="sel"><option value=""><---Select---></option>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection connection= DriverManager.getConnection('jdbc:mysql://192.168.1.104:3306/networkmonitoring','mohsin','123456');");
PreparedStatement psmnt = connection.prepareStatement("select objecttype_name from network_objecttype ");
ResultSet results = psmnt.executeQuery();
while(results.next()){
String name = results.getString('objecttype_name');
String id = results.getString('objecttype_name');
%><option value="<%= name %>"><%=name%></option>
<%} results.close(); psmnt.close(); %>
</select><br>
</form>
Ramesh Kotha
  • 8,206
  • 17
  • 64
  • 89
1

Not sure exactly what are you trying to achieve but it looks like you do smth wrong here. You should either build your drop down on the jsp page like eg:

<select>
<%
while(re.next())
{
String name1 = re.getString(1);

%>
<option value="<%= name1%>"><%= name1%></option>
<% 
}
%>
</select>

or build your drop down code in a servlet (without scriplets) and send it to the browser. Now it looks like you are trying to build a JSP scriplet inside the servlet. Look here for an example on how to build the drop down, you will find many example on the web, there was a similar question to this one here: retrieve dropdown list from mysql database and insert to database in jsp

Community
  • 1
  • 1
Kris
  • 5,600
  • 2
  • 26
  • 46