List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Update ResultSet in Database in Java?

Update ResultSet in Java

Condition for Updating ResultSet in Database in Java

  • Description: To update a ResultSet in Java, the ResultSet must be created with the ability to support updates by specifying ResultSet.TYPE_SCROLL_INSENSITIVE and ResultSet.CONCUR_UPDATABLE when creating the Statement or PreparedStatement. Once the ResultSet is retrieved, the updateXXX methods, such as updateString, updateInt, or updateDate, are used to modify the values of specific columns in the current row. After making changes, the updateRow() method is called to commit the updates to the database. New rows can be inserted using the moveToInsertRow(), followed by setting column values with updateXXX methods, and saving the new row using insertRow(). This approach allows direct interaction with the database through the ResultSet, ensuring that updates are reflected immediately.
Sample Source Code
  • # UpdateDatabase.java
    package JavaSamples2;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.swing.JOptionPane;
    import javax.swing.table.DefaultTableModel;

    public class UpdateDatabase extends javax.swing.JFrame {

    private final String jdbcUrl = "jdbc:mysql://localhost:3306/Crud";
    private final String username = "root";
    private final String password = "root@123";

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    DefaultTableModel model = null;

    public UpdateDatabase() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
    // Component initialization code goes here
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    int id = Integer.parseInt(jTextField1.getText());
    String name = jTextField2.getText();
    int mark = Integer.parseInt(jTextField3.getText());

    try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection(jdbcUrl, username, password);
    String updateQuery = "UPDATE crud_table SET name = ?, mark = ? WHERE userid = ?";
    PreparedStatement updateStmt = conn.prepareStatement(updateQuery);
    updateStmt.setString(1, name);
    updateStmt.setInt(2, mark);
    updateStmt.setInt(3, id);
    int rowsUpdated = updateStmt.executeUpdate();
    if (rowsUpdated > 0) {
    JOptionPane.showMessageDialog(null, "Student marks updated successfully!");
    }
    String selectQuery = "SELECT * FROM crud_table WHERE userid = ?";
    PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
    selectStmt.setInt(1, id);
    rs = selectStmt.executeQuery();
    model = new DefaultTableModel();
    model.addColumn("User ID");
    model.addColumn("Name");
    model.addColumn("Marks");
    while (rs.next()) {
    Object[] rowData = {
    rs.getInt("userid"),
    rs.getString("name"),
    rs.getInt("mark")
    };
    model.addRow(rowData);
    }
    jTable1.setModel(model);
    } catch (ClassNotFoundException | SQLException e) {
    System.out.println(e.getMessage());
    } finally {
    try {
    if (rs != null) {
    rs.close();
    }
    if (stmt != null) {
    stmt.close();
    }
    if (conn != null) {
    conn.close();
    }
    } catch (SQLException e) {
    System.out.println(e.getMessage());
    }
    }
    }

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(() -> {
    new UpdateDatabase().setVisible(true);
    });
    }

    }
Screenshots
  • STEP 1: Enter the user ID, name, and marks in the fields.
  • Enter user data

  • STEP 2: After clicking "Update", the new data is reflected in the table below.
  • Updated table

  • STEP 3: View the updated mark value (e.g., from 444 to 555).
  • Mark updated

Related Links