Mega Code Archive

 
Categories / Java / Database SQL JDBC
 

Move to absolute or relative row

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Main {   public static void main(String[] args) throws Exception {     Class.forName("com.mysql.jdbc.Driver");     Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");     Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);     ResultSet resultSet = statement.executeQuery("SELECT * FROM products");     // Move to the second row     resultSet.absolute(2);     System.out.println("You are now in: " + resultSet.getRow());     // Move 2 records forward from the current position (fourth row)     resultSet.relative(2);     System.out.println("You are now in: " + resultSet.getRow());     // Move to the last row in the result set     resultSet.absolute(-1);     System.out.println("You are now in: " + resultSet.getRow());     // Move 3 records backward from the current position (second row)     resultSet.relative(-3);     System.out.println("You are now in: " + resultSet.getRow());     connection.close();   } }