Created
March 2, 2015 11:45
-
-
Save revox/59863880f655795dc752 to your computer and use it in GitHub Desktop.
JDBC basics pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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