Updatable and Scrollable resultsets
Thu Nov 21 2024 18:07:19 GMT+0000 (Coordinated Universal Time)
Saved by @coding1
SQL CREATE DATABASE sampledb; USE sampledb; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) ); JAVA import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.PreparedStatement; public class UpdatableScrollableResultSetExample { private static final String URL = "jdbc:mysql://localhost:3306/sampledb"; private static final String USERNAME = "your_username"; // Replace with your DB username private static final String PASSWORD = "your_password"; // Replace with your DB password public static void main(String[] args) { Connection connection = null; try { // Establishing the connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); System.out.println("Connected to the database successfully."); // Inserting some sample data insertSampleData(connection); // Using Updatable and Scrollable ResultSet try (Statement stmt = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) { String query = "SELECT * FROM users"; ResultSet rs = stmt.executeQuery(query); // Moving to the last record if (rs.last()) { System.out.println("Last Record: ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Email: " + rs.getString("email")); } // Moving to the first record if (rs.first()) { System.out.println("First Record: ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Email: " + rs.getString("email")); } // Updating the first record if (rs.first()) { System.out.println("Updating record..."); rs.updateString("name", "Updated Name"); rs.updateRow(); System.out.println("Record updated."); } // Displaying updated records System.out.println("Updated Records:"); rs.beforeFirst(); // Move cursor to before the first record while (rs.next()) { System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Email: " + rs.getString("email")); } } } catch (SQLException e) { System.err.println("SQL Exception: " + e.getMessage()); } finally { // Closing the connection try { if (connection != null && !connection.isClosed()) { connection.close(); System.out.println("Database connection closed."); } } catch (SQLException e) { System.err.println("Failed to close the connection: " + e.getMessage()); } } } // Method to insert sample data into the users table private static void insertSampleData(Connection connection) throws SQLException { String insertSQL = "INSERT INTO users (name, email) VALUES (?, ?)"; try (PreparedStatement pstmt = connection.prepareStatement(insertSQL)) { pstmt.setString(1, "John Doe"); pstmt.setString(2, "john@example.com"); pstmt.executeUpdate(); pstmt.setString(1, "Jane Smith"); pstmt.setString(2, "jane@example.com"); pstmt.executeUpdate(); System.out.println("Sample data inserted into users table."); } } }
Comments