By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

how to sort the table using combo box values

P: 10
hi...

I want to sort the table using combobox values. I give the code here.

address.html:

<html>
<head>
<title>Add a new entry</title>
</head>
<body>
<h1>Please enter the new address information</h1>
<form method="POST" action="datajsp.jsp">
Name: <input type="text" name="name" size="20"><br>
Street: <input type="text" name="street" size="20"><br>
City: <input type="text" name="city" size="20"><br>

Country: <input type="text" name="country" size="20"><br>
Telephone: <input type="text" name="tel" size="20">
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>


datajsp.jsp:


<%@page import="java.sql.*"%>
<html>
<head>
<title>Add a new Address</title>


<script type="text/javascript">

function sort()
{
var k=document.getElementById("mylist").value;
alert(k);
}
</script>



</head>
<body>
<form name=f1 action="http://localhost:8080/examples/datadisplay.jsp">
<h1>New Address Creation using executeUpdate()</h1>
<%
Connection conn = null;
PreparedStatement stmt = null;
try {
Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (Exception e) {
System.out.println("Error occurred " + e);
}
try {
conn = DriverManager.getConnection("jdbc:odbc:data");

}
catch (Exception e) {
System.out.println("Error occurred " + e);
}
try {
stmt = conn.prepareStatement("INSERT INTO address(name, street, city, country, telephone) VALUES (?, ?, ?, ?, ?)");
stmt.setString(1, request.getParameter("name"));
stmt.setString(2, request.getParameter("street"));
stmt.setString(3, request.getParameter("city"));

stmt.setString(4, request.getParameter("country"));
stmt.setString(5, request.getParameter("tel"));
stmt.executeUpdate();
stmt.close();
conn.close();
}
catch (Exception e) {
System.out.println("Error occurred " + e);
}
finally {
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {}
try {
if (conn != null)
conn.close();
} catch (Exception e) {}
}

%>
The new address has been created.
<br><br>


<select id="mylist">
<option value="name" >name</option>
<option value="street">street</option>
<option value="city">city</option>
<option value="country">country</option>
<option value="telephone">telephone</option></select>
<br><br><font style="margin-left:100px; "></font>
<input type="submit" value="display" onClick="sort();">
</form>
</body>
</html>

datadisplay.jsp:



<%String s="";
s=request.getParameter("k");%>
<%@ page import = "java.sql.*"%>
<html>
<body>
<%
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "", "");

Statement statement = connection.createStatement();

String name=request.getParameter("name");
String street=request.getParameter("street");
String city=request.getParameter("city");
String country=request.getParameter("country");
String telephone=request.getParameter("telephone");
ResultSet rs=statement.executeQuery("select * from address order by '"+s+"'");

%>
<TABLE BORDER="1">
<TR>

<TH>Name</TH>
<TH>Street</TH><TH>City</TH><TH>Country</TH><TH>TelePhone</TH>

</TR>
<%
try
{
while(rs.next()){


%>
<TR>
<TD> <%= rs.getString(1) %></td>
<TD> <%= rs.getString(2) %></TD>
<TD> <%= rs.getString(3) %></TD>
<TD> <%= rs.getString(4) %></TD>
<TD> <%= rs.getString(5) %></TD>
</TR>
<% }

}
catch(Exception e)
{
System.out.println(e);
}
%>
</TABLE>
</body>
</html>

In this the table displayed correctly.but i select one value from the combobox and click the display button but it was not the table is sorted.

this is very urgent for me. Please anyone can help me..

Thanks in advance.
Jul 6 '07 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
1.)Please use code tags everytime when posting code.
function sort()
{
var k=document.getElementById("mylist").value;
alert(k);
}
</script>.
Is this all the sort function that you have? Surely you don't expect that the sort the table.
Jul 6 '07 #2

P: 10
1.)Please use code tags everytime when posting code.

Is this all the sort function that you have? Surely you don't expect that the sort the table.
Thanks for your reply...

no i want to get that k value in next page. For all the sorting i do in the database query.

so please help me how to get that combobox value(that mean k in sort()) in the query.

ResultSet rs=statement.executeQuery("select * from address order by '"+s+"'");

Thanks in Advance.
Jul 6 '07 #3

10K+
P: 13,264
Thanks for your reply...

no i want to get that k value in next page. For all the sorting i do in the database query.

so please help me how to get that combobox value(that mean k in sort()) in the query.

ResultSet rs=statement.executeQuery("select * from address order by '"+s+"'");

Thanks in Advance.
Put the value in a hidden input field on the form that's submitted. Then in the next page you get that value using

Expand|Select|Wrap|Line Numbers
  1. String value = request.getParameter("paramName");
Jul 6 '07 #4

100+
P: 201
Hi,

In datadisplay.jsp page you are not getting value of k.So it is not working
Try following code

String k =request.getParameter("k");
Jul 6 '07 #5

P: 10
Put the value in a hidden input field on the form that's submitted. Then in the next page you get that value using

Expand|Select|Wrap|Line Numbers
  1. String value = request.getParameter("paramName");
Thank you very much. It is working fine.
Jul 6 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.