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!

5
  • whole process is wrong.. what are you trying with the code... Commented Feb 13, 2012 at 7:57
  • i have wriiten in my post it is showing "'><%=rs.getString('objecttype_name')%>" in dropdown ! Commented Feb 13, 2012 at 7: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 Commented Feb 13, 2012 at 7:59
  • in html how you will use java... instead of html you have to use jsp.. Commented Feb 13, 2012 at 8: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 Commented Feb 13, 2012 at 8:07

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.