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.