Pacific-Design.com

    
Home Index

1. Databases

2. Update

Databases / Update /

/*-----------------------------------------------------------------------------------*/

private void update(Connection conn) throws SQLException {
    String SQL = "UPDATE " + SOURCE_TABLE + " SET processed = ? WHERE processed = ?";
    PreparedStatement prepStmt = conn.prepareStatement(SQL);
    prepStmt.setString(1, "parsed");
    prepStmt.setString(2, "new");
    prepStmt.executeUpdate();
}
/*-----------------------------------------------------------------------------------*/

import java.sql.*;

public class UpdateMySQL
{

    public static void main(String[] args)
    {
        try
        {
            String driver   = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/database";

            Class.forName(driver).newInstance();
            Connection conn = DriverManager.getConnection(url, "user", "password");

            String query = "UPDATE student SET GPA = ? WHERE name = ?";
            PreparedStatement prepStmt = conn.prepareStatement(query);
            prepStmt.setInt   (1, 4.0);
            prepStmt.setString(2, "Kevin Duraj");
            prepStmt.executeUpdate();

            conn.close();
        }
        catch (Exception ex)
        {
            System.err.println(ex.getMessage());
        }
    }
}