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
  • 331
  • 2
  • 13
  • 35
  • whole process is wrong.. what are you trying with the code... – Ramesh Kotha Feb 13 '12 at 07:57
  • i have wriiten in my post it is showing "'><%=rs.getString('objecttype_name')%>" in dropdown ! – Syed Raza Feb 13 '12 at 07:57
  • @ramesh i am trying to get data from mysql to the dropdown use in html and i am using html at server side by appending string builder – Syed Raza Feb 13 '12 at 07:59
  • in html how you will use java... instead of html you have to use jsp.. – Ramesh Kotha Feb 13 '12 at 08:02
  • i am not using html page i am using java class and having string builder in which i am appending html this whole this is in a particular method and then calling this method of class in jsp page – Syed Raza Feb 13 '12 at 08:07

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,266
  • 17
  • 66
  • 90
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,714
  • 2
  • 27
  • 47