Simplest Multiple Column Sorting (Up & Down) in JSP

This is the simplest and fastest code that shows how to implement multiple-column table sorting in JSP (Sort Ascending & Sort Descending).
Excellent for JSP Database beginers
Tanwani Anyangwe

<%@ page import="java.sql.*" %>
<%

//Simplest Multiple Column Sorting (Up & Down) in JSP 
//This is the simplest and fastest code that shows how to implement multiple-column table sorting in JSP (Sort Ascending & Sort Descending). 
//Excellent for JSP Database beginers 
//Tanwani Anyangwe

Connection connection = null;
Statement statement = null;
ResultSet results = null;

try {
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");	
	String url = "jdbc:odbc:nwind";	 	
	String query = "SELECT FirstName, LastName, Title, Address, City, Country FROM Employees";
	
	String sort = request.getParameter("sort");
	String lastsort = request.getParameter("lastsort");	
	String thissort = "";
	
	 if(lastsort==null)lastsort="";
	 if(sort==null)sort="";
	
	 if (!sort.equals("")){
		 if (lastsort.equalsIgnoreCase(sort)){
			thissort = sort + " desc";
		}else  if (lastsort.indexOf(sort + " desc") > -1 ){
			thissort = lastsort.replaceAll(sort + " desc",sort);
		}else  if (lastsort.indexOf(sort) > -1 ){
			thissort = lastsort.replaceAll(sort,sort + " desc");
		}else  if (!lastsort.equals("")){
			thissort = lastsort + "," + sort;
		}else{
			thissort = sort;
		}		
		query += " ORDER BY " + thissort;
	}
	
	//out.println(query);
	out.println("<p><b><font color=blue>ORDER BY</font>:</b> " + thissort + "</p>");
	out.println("<p><a href=\"" +  request.getRequestURI() + "\">Reset Order</a></p>");

	connection = DriverManager.getConnection(url, "", "");
	statement = connection.createStatement();
	results = statement.executeQuery(query);

	int i;
	ResultSetMetaData rsmd = results.getMetaData();
	int numCols = rsmd.getColumnCount();
			
			  
	// sart table
	out.println("<table border=1>");
	out.println("<caption>Employees</caption>");
	
	// get column header info
	out.println("<tr>");
	for (i=1; i <= numCols; i++){   
		String fname = rsmd.getColumnName(i);
		
		out.println("<th><a href=\"" + request.getRequestURI() 
			+ "?sort=" + fname + "&lastsort=" + thissort + "\">" + fname);
		 if(!thissort.equals("")){	
			 if (thissort.indexOf(fname + " desc") > -1){
				out.println(" -");
			}else  if(thissort.indexOf(fname) > -1 ){	
				out.println(" +");
			}
		}
		
		out.println("</a></th>");  	
	}    
	out.println("</tr>");

	//print columns data		  
	while (results.next()){	
	  	out.println("<tr>");		
	  	for (i=1; i <= numCols; i++) {
	  	  String text = results.getString(i);
	  	   if(text==null){text="";}				  
	  	  out.println("<td>" + text + "</td>");
	  	}
	  	out.println("</tr>");		
	}
	out.println("</table>");		

	//close connection
	connection.close();
}
catch (ClassNotFoundException e) {
	System.err.println("Could not load database driver!");
}
catch (SQLException e) {
	System.err.println("Could not connect to the database!");
}
finally {
	try {  if (connection != null) connection.close(); }
	catch (SQLException e) { }
}
%>

1