hello evryone.
please someone help me. I want to load the data frm sql server database at the button click...how to load the data using resultset rs.getString("column name") in the <option></option> tag using JSP codes??

Recommended Answers

use JSP expressions:

<option><%=varName %></option>

or scriptlets:

<option><% out.print(varName); %></option>

expressions are advised

Jump to Post

anindo87, ...but i'v tried ths code....it isn't working.... hkansal has wrote an expression for you. Post your source code. Use BB code tags.

Jump to Post

hello evryone.
please someone help me. I want to load the data frm sql server database at the button click...how to load the data using resultset rs.getString("column name") in the <option></option> tag using JSP codes??

The answer can be found in

Jump to Post

HTML tag - </select> must be placed outside the while loop.

show.innerHTML = "<label><select name='select'><% while(rs.next()){%><option><%= rs.getString(1) %></option><option><%= rs.getString(2) %></option><option><%= rs.getString(3).substring(0,10) %></option><%}}catch(Exception ex){out.print("Exception is:- "+ex);}%></select></label>";
Jump to Post

All 15 Replies

use JSP expressions:

<option><%=varName %></option>

or scriptlets:

<option><% out.print(varName); %></option>

expressions are advised

use JSP expressions:

<option><%=varName %></option>

or scriptlets:

<option><% out.print(varName); %></option>

expressions are advised

thanx so much for taking interest...but i'v tried ths code....it isn't working....

anindo87, ...but i'v tried ths code....it isn't working.... hkansal has wrote an expression for you. Post your source code. Use BB code tags.

hello evryone.
please someone help me. I want to load the data frm sql server database at the button click...how to load the data using resultset rs.getString("column name") in the <option></option> tag using JSP codes??

The answer can be found in sticky post of JSP section

as suggested by adatapost here i'm posting the code which i used...

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<% 
		Connection con=null;
		ResultSet rs=null;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con=DriverManager.getConnection("jdbc:odbc:p_portal");
			Statement stat=con.createStatement();
			String sql="select * from message_box where name='anindo'";
			rs=stat.executeQuery(sql);
			%>
<script language="javascript">
function check()
{
	var show=document.getElementById("mydivtag");
	show.innerHTML = "<label><select name=select><% while(rs.next()){%><option><%= rs.getString(1) %></option><option><%= rs.getString(2) %></option><option><%= rs.getString(3).substring(0,10) %></option></select></label><%}}catch(Exception ex){out.print("Exception is:- "+ex);}%>";
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="try.jsp">
  <label></label>
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="submit" name="Submit" value="Submit" onclick="check()" />
	  </p>
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   

   </p>
  <div id="mydivtag" ></div>
  <p>&nbsp;</p>
</form>
</body>
</html>

i have used p_portal as the dsn and the database table message_box described as below..

name varchar 50
from varchar 50
subject varchar 1000
date datetime 8

i hope the code will make the things very clear.....

HTML tag - </select> must be placed outside the while loop.

show.innerHTML = "<label><select name='select'><% while(rs.next()){%><option><%= rs.getString(1) %></option><option><%= rs.getString(2) %></option><option><%= rs.getString(3).substring(0,10) %></option><%}}catch(Exception ex){out.print("Exception is:- "+ex);}%></select></label>";

hello adatapost...

i'v modified the code....but still there's a problem

when i'm clicking the button......a drop down menu wid the loaded data is appearing but as soon as the page is fully loaded the drop down menu disappears....i'm not getting hold of this problem....

Close the connection.

rs.close();  // release the result
  st.close();
  cn.close();

indeed , am not using MVC structure but i have done what you want before, you can take a look

<select name="select" size="1" >
<%
 String sql = "select *from  table ";

   Vector v1=database.jdbcMultipleRowQuery(sql);
	for(int i=0;i<v1.size();i++){
	Vector  v2 = (Vector)v1.elementAt(i);
for(int j=0;j<v2.size();j++){

out.print("<option>"+v2.get(0)+"</ption>");		
		
}
}
%>

it may help

indeed , am not using MVC structure but i have done what you want before, you can take a look

<select name="select" size="1" >
<%
 String sql = "select *from  table ";

   Vector v1=database.jdbcMultipleRowQuery(sql);
	for(int i=0;i<v1.size();i++){
	Vector  v2 = (Vector)v1.elementAt(i);
for(int j=0;j<v2.size();j++){

out.print("<option>"+v2.get(0)+"</ption>");		
		
}
}
%>

it may help

thanx CoSIS1 i'l use this code....i guess this will work.....

hello adatapost.....its i closed the connection but still the same problem s occurring........when the page s fully loading the drop down menu disappers.......what xactly s the problem???

anindo87,
Post your latest code (page). Wrap the source code with bb code tags.

hello adatapost...this s the code

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
     <title>Untitled Document</title>
     <%
      Connection con=null;
      ResultSet rs=null;
      try
      {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          con=DriverManager.getConnection("jdbc:odbc:p_portal");
          Statement stat=con.createStatement();
          String sql="select * from message_box where name='anindo'";
         rs=stat.executeQuery(sql);
         %>
      <script language="javascript">
       function check()
       {
          var show=document.getElementById("mydivtag");
          show.innerHTML = "<label><select name=select><% while(rs.next()){%><option><%= rs.getString(1) %></option><option><%= rs.getString(2) %></option><option><%= rs.getString(3).substring(0,10) %></option></select></label><%}}catch(Exception ex){out.print("Exception is:- "+ex);}%>";
       }
        </script>
        </head>
        <body>
        <form id="form1" name="form1" method="post" action="try.jsp">
        <label></label>
      <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="submit" name="Submit" value="Submit" onclick="check()" />
      </p>
      <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </p>
      <div id="mydivtag" ></div>
      <p>&nbsp;</p>
      </form>
       </body>
	   <%
	    con.close();
		rs.close();
      </html>

Use input type button:

<input type="button" name="submit" value="Submit" onclick="check()" />

hello adatapost......thanx for the reply.....the question s solved....thanx very much....

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.