Two Laptop Bag*
The moose likes JSP and the fly likes sort data in jsp page which is being displayed from my mysql database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "sort data in jsp page which is being displayed from my mysql database" Watch "sort data in jsp page which is being displayed from my mysql database" New topic
Author

sort data in jsp page which is being displayed from my mysql database

sanidhya kumar
Greenhorn

Joined: Sep 05, 2012
Posts: 18
how to sort data in jsp page which is being displayed from my mysql database?
i want to click on "ID" tab or "address" tab or "name" tab to sort the data.
here is what m trying...

Ashwini Kashyap
Ranch Hand

Joined: Aug 30, 2012
Posts: 90
Hi Sanidhya,

Answer to your question is that you are using correct piece of code.

"Select * .... ORDER BY columnName ASC/DESC" is the correct way of sorting data.

But what is the problem you are facing here?

Thanks and Regards,
---------------------------------------------------------------------------------------
Ashwini Kashyap | akashyap@infocepts.com | www.infocepts.com
---------------------------------------------------------------------------------------
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

Hi Sanidhya,

I would like to suggest you to use the jqgrid for this type of functionality as user can short any column either it can be name,id,address or description.
Jqgrid will be good option for this.

Regards
Jatan
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Hi Sanidhya,


Some javascript support to sorting (Ascending and Descending).

Go google and search something, you can get solution

Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Sort the data before it gets to the JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sanidhya kumar
Greenhorn

Joined: Sep 05, 2012
Posts: 18
Ashwini hi, actually what i want is....my data(i.e rows) must get sorted only after i click the column names in y jsp page....(hint:The whole seen is exactly same as detail view of a folder in windows, were we can click on column names to sort data acording to size or date modified etc....)but i dont want size n all...i just want it to be arranged in ascending order.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Same answer.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

sanidhya kumar wrote:Ashwini hi, actually what i want is....my data(i.e rows) must get sorted only after i click the column names in y jsp page.


Remember that you aren't clicking on column headings in the JSP. You are clicking in column headings in the HTML which was generated by the JSP. That HTML code is in the browser, too, not on your server.

So you have two options. You can implement some Javascript code which sorts the table when the column heading is clicked. Or you can send a request back to the server to ask for the data to be sent again, sorted in the sequence corresponding to whichever heading was clicked. In the latter case you would use the same JSP to generate the HTML, and the database requests (in various orders) would be done via a servlet which handled the requests.
sanidhya kumar
Greenhorn

Joined: Sep 05, 2012
Posts: 18
check this link, it is written in java servelets. but i want this same code in jsp........please please help all.....

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3652&lngWId=14
sanidhya kumar
Greenhorn

Joined: Sep 05, 2012
Posts: 18
sanidhya kumar wrote:check this link, it is written in java servelets. but i want this same code in jsp........please please help all.....

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3652&lngWId=14



have to submit my assignment by tomorrow........
sanidhya kumar
Greenhorn

Joined: Sep 05, 2012
Posts: 18
finally... got d temporary solution....... project is resolved..thanks all for yoursupport..
what i did is... i used a java script and related it with the table i wanted to sort..


<%--
Document : index
Created on : Sep 4, 2012, 4:55:31 PM
Author : Sanidhya09
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Page For List Sorting</title>
</head>
<body>
<%
try {
String connectionURL = "jdbc:mysql://localhost:3308/sanidhya09";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;

Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL,"root","");
if(!connection.isClosed())
%>

<font size="+3" color="green">

<%
out.println("Successfully connected to " + "MySQL");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * from details");
%>

<script type="text/javascript" src="gs_sortable.1.7.js"></script>
<script type="text/javascript">
<!--
var TSort_Data = new Array ('my_table', 'i','s','s');

//tsRegister();
// -->
</script>

<TABLE id="my_table" cellpadding="15" border="1" style="background-color: #ffffcc;" >
<thead>
<%--<a href="#" OnClick=" return <%rs = statement.executeQuery("SELECT * from details order by id, name, address desc;");%>"</a>--%>
<th style="cursor: pointer;"><a href="" onclick="tsDraw(0,'my_table'); return false">ID</a></th>
<%--<%String string = response.encodeURL("");%>--%>

<th style="cursor: pointer;"><a href="" onclick="tsDraw(1,'my_table'); return false">NAME</a></th>
<th style="cursor: pointer;"><a href="" onclick="tsDraw(1,'my_table'); return false">ADDRESS</a></th>
</thead>
<%
while (rs.next()) {
%>

<TR>
<TD><%=rs.getInt(1)%></TD>
<TD><%=rs.getString(2)%></TD>
<TD><%=rs.getString(3)%></TD>

</TR>
<% } %></TABLE>
<%
rs.close();
statement.close();
connection.close();
}

catch(Exception ex){
%>

</font>

<font size="+3" color="red">
<%
out.println("Unable to connect to database" + ex);
}%>
</font>

</body>
</html>
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

You can use table sorter plugin that uses jquery. It's fast and effective (I tried it in some of my projects).

Vishal.


Programming is about thinking, NOT coding
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sort data in jsp page which is being displayed from my mysql database