Skip to content

Instantly share code, notes, and snippets.

@revox
Created March 2, 2015 11:45
Show Gist options
  • Save revox/59863880f655795dc752 to your computer and use it in GitHub Desktop.
Save revox/59863880f655795dc752 to your computer and use it in GitHub Desktop.
JDBC basics pagination
import java.sql.*;
public class DBPaginate {
public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection connect =
DriverManager.getConnection("jdbc:mysql://localhost:8889/java_demo","root","root");
String query = "SELECT * FROM messages LIMIT 5 OFFSET 10"; // OR LIMIT 10,5
Statement st = connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(5);
st.setMaxRows(5);
ResultSet resultSet = st.executeQuery(query);
while (resultSet.next())
{
System.out.println(resultSet.getInt("id") + " "
+ resultSet.getString("user") + " : "
+ resultSet.getString("message"));
}
connect.close();
st.close();
resultSet.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment