Friday, March 30, 2012

How to Do Pagination using Jsp,Servlet ?

For Pagination first of all you need to create a connection using jsp. i use the mysql server for data base.

create a table in mysql.

Create table pagination
(
id int ,
title varchar (50),
Description varchar(50),
)

Step 2:create the Database Connection.

create a connection.jsp file

Simple code:-

String connectionURL = "jdbc:mysql://localhost:3306/DatabaseName";
Connection con = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL, "username", "Password");

save it to connection.jsp

Connection Created

Step 3:- Create a search.jsp file

"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>


display data from the table using jsp



Welcome to music | Movie Search Engine


<%@include file="music.html"%>
/*
String connectionURL = "jdbc:mysql://localhost:3306/";
Connection con = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL, "", "");
*/
<%@ include file="connection.jsp"%>
<%
Statement statement = null;
ResultSet rs = null;

try {


statement = con.createStatement();

String qs=request.getParameter("q");
if(qs==null)
qs="";

String item=request.getParameter("item");
if(item==null){item="0";}
int indexcounter=Integer.parseInt(item);

String query1 = "select count(*) from music_search where title LIKE '%"+qs+"%' order by id desc limit "+indexcounter+", 5";

Statement stat1 = con.createStatement();
ResultSet rs1 = stat1.executeQuery(query1);
rs1.next();
int resultcounter=0;
//resultcounter_blankresult=resultcounter;
resultcounter=rs1.getInt(1);

String QueryString = "SELECT * from music_search where title LIKE '%"+qs+"%' order by id desc limit "+indexcounter+", 5";




rs = statement.executeQuery(QueryString);
%>



<%
while (rs.next()) {

%>


















<%}%>

<%
// close all the connections.
}catch (Exception ex){
%>
<%
out.println("Unable to connect to database.");
}
%>
   
   
   
<%=rs.getString(1)%><%=rs.getString(2)%>


No comments:

Post a Comment