Update data from SQL DB using Java

Here’s a sample code snippet that demonstrates how to update data in a SQL database using Java and JDBC (Java Database Connectivity):

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SQLDataUpdater {
    public static void main(String[] args) {
        // Database credentials
        String url = "jdbc:sqlserver://localhost:1433;databaseName=mydatabase";
        String username = "sa";
        String password = "password";

        // SQL query
        String query = "UPDATE customers SET email = ? WHERE id = ?";

        try {
            // Register JDBC driver
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            // Open a connection
            Connection conn = DriverManager.getConnection(url, username, password);

            // Create a prepared statement
            PreparedStatement pstmt = conn.prepareStatement(query);

            // Set the parameter values
            pstmt.setString(1, "[email protected]");
            pstmt.setInt(2, 1); // Assuming ID 1 needs to be updated

            // Execute the query
            int rowsAffected = pstmt.executeUpdate();

            // Check if the update was successful
            if (rowsAffected > 0) {
                System.out.println("Data updated successfully!");
            } else {
                System.out.println("Failed to update data.");
            }

            // Close the resources
            pstmt.close();
            conn.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Make sure to replace the url, username, and password variables with your actual SQL database connection details. Additionally, modify the query variable to match your desired SQL UPDATE statement.

This code snippet assumes you have the appropriate JDBC driver for your SQL database (e.g., mssql-jdbc.jar for Microsoft SQL Server) included in your classpath. If not, you can download the driver from the respective database vendor’s website or include it as a Maven/Gradle dependency.

The code connects to the SQL database, prepares an UPDATE statement with placeholders, sets the parameter values using the appropriate setter methods (setString, setInt, etc.), and executes the query using executeUpdate. It then checks the number of affected rows to determine if the update was successful. Finally, it closes the resources to free up memory.

Remember to handle exceptions appropriately in your production code and consider using try-with-resources or a similar mechanism to automatically close the resources.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech