Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sanidhya kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanidhya,


Some javascript support to sorting (Ascending and Descending).

Go google and search something, you can get solution
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64983
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sort the data before it gets to the JSP.
 
sanidhya kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64983
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same answer.
 
Paul Clapham
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Vishal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic