Hi Anybody can help. How to wirte logic for getting each time 10 records in browser and also how to implement the logic next, previouse, last, first buttons using in JSP. Please anybody can provide the code or template
Thanks
Kumar
Printable View
Hi Anybody can help. How to wirte logic for getting each time 10 records in browser and also how to implement the logic next, previouse, last, first buttons using in JSP. Please anybody can provide the code or template
Thanks
Kumar
Here is a quick and dirty solution, sorry if the code is messy, this is my first project with jsp.
Code:<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="com.mysql.jdbc.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Test</title>
</head>
<body>
<%
java.sql.Connection conn;
String userName = "theo";
String password = "secretpass";
String url = "jdbc:mysql://localhost/theo"; // localhost = my server, theo is
// my database, change as appropriate
// You can invoke the script as "index.jsp?action=filltable" to fill a test table
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url,userName,password);
java.sql.Statement st;
String query;
String action = request.getParameter("action"); //get parameter ?action=
int pageAsInt = 0;
if ((action != null) && action.equals("filltable")) {
// Fill the test table
for (int i = 0; i<= 30;i++) {
query = "INSERT INTO theo VALUES('','Item " + i + "');"; // change table name 'theo' as appropriate for your situation
st = conn.prepareStatement(query);
st.executeUpdate(query);
}
} else {
String p = request.getParameter("page"); // get paramerer ?page=
if (p != null) {
// Strip quotation marks
StringBuffer buffer = new StringBuffer();
for (int index = 0; index < p.length(); index++) {
char c = p.charAt(index);
if (c != '\' ') {
buffer.append(c);
}
}
p = buffer.toString();
pageAsInt = Integer.valueOf(p); converts string p to int pageAsInt
} else {
pageAsInt = 1; // no page provided, starting with 1
}
// because records start with index 0; page should be decremented (for instance, page=2 should start with offset 10 = page - 1 * 10)
query = "SELECT * FROM theo ORDER BY id ASC LIMIT "+ (pageAsInt - 1) * 10 + ",10 ;"; // replace theo with your table name
st = conn.prepareStatement(query);
java.sql.ResultSet result = st.executeQuery(query);
out.println("<table>");
while (result.next()) {
out.println("<tr>");
out.println("<td>" + result.getInt("id") + "</td>");
out.println("<td>" + result.getString("name") + "</td>");
out.println("</tr>");
}
out.println("</table>");
st.close();
// Calculate number of rows in the table
query = "SELECT * FROM theo WHERE 1"; //replace theo with your table name
st = conn.prepareStatement(query);
st.executeQuery(query);
result = st.getResultSet();
int count = 0;
while (result.next()) {
count++;
}
st.close();
int pages = (count / 10) + 1; // 20 results = 0-9, 10-19, 20 thus 3 pages
out.println("<table> <tr>");
// First page
if (pageAsInt > 1) {
out.println("<td> <a href=\"?page=1\"> << </a> </td>");
// Previous page
int previousPage = pageAsInt - 1;
out.println("<td><a href=\"?page='"+ previousPage +"'\"> < </a></td>");
} else {
out.println("<td> Already at first page </td>");
}
out.println("<td><b> Page: "+ pageAsInt + "</b></td>");
if (pages > pageAsInt) {
// Next page
int nextPage = pageAsInt + 1;
out.println("<td><a href=\"?page='"+ nextPage + "'\"> > </a></td>");
// Last Page
out.println("<td><a href=\"?page='"+ pages+"'\"> >> </a></td>");
} else {
out.println("<td>Already at last page</td>");
}
out.println("</tr></table>");
}
conn.close();
%>
</body>
</html>