The UPDATE proclamation is utilized to refresh information in a table. Apache Derby gives two sorts of updates (linguistic structure) to be specific looked through update and situated update.
The looked through UPDATE proclamation refreshes all the predetermined sections of a table.
Syntax
Following is the grammar of the UPDATE question −
ij> UPDATE table_name
SET column_name = value, column_name = value, ...
WHERE conditions;
The WHERE provision can utilize the examination administrators, for example, =, !=, <, >, <=, and >=, just as the BETWEEN and LIKE administrators.
Example
Assume you have a table Employee in the information base with the 4 records as demonstrated beneath −
ID |NAME |SALARY |LOCATION - -
ID |NAME |SALARY |LOCATION
----------------------------------------------------------
1 |Amit |30000 |Hyderabad
2 |Kalyan |40000 |Vishakhapatnam
3 |Renuka |50000 |Delhi
4 |Archana |15000 |Mumbai
The accompanying SQL UPDATE articulation refreshes the area and pay of a worker whose name is Kaylan.
ij> UPDATE Employees SET Location = 'Chennai', Salary = 43000 WHERE Name =
'Kalyan';
1 rows inserted/updated/deleted
1 columns embedded/refreshed/erased
In the event that you get the substance of the Employees table, you can notice the progressions done by the UPDATE inquiry.
ij> select * from Employees;
ID |NAME |SALARY |LOCATION
----------------------------------------------------------
1 |Amit |30000 |Hyderabad
2 |Kalyan |43000 |Chennai
3 |Renuka |50000 |Delhi
4 |Archana |15000 |Mumbai
4 rows selected
Update Data using JDBC program
This segment discloses how to refresh the current records of a table in the Apache Derby data set utilizing JDBC application.
In the event that you need to demand the Derby network worker utilizing network customer, ensure that the worker is going. The class name for the Network customer driver is org.apache.derby.jdbc.ClientDriver and the URL is jdbc:derby://localhost:1527/DATABASE_NAME;create=true;user=USER_NAME;passw ord=PASSWORD"
Follow the means offered underneath to refresh the current records of a table in Apache Derby.
Step 1: Register the driver
To speak with the data set, most importantly, you need to enroll the driver. The forName() strategy for the Class acknowledges a String esteem speaking to a class name loads it in to the memory, which naturally enlists it. Register the driver utilizing this strategy.
Step 2: Get the connection
All in all, the initial step we do to convey to the data set is to interface with it. The Connection class speaks to the actual association with an information base worker. You can make an association object by summoning the getConnection() technique for the DriverManager class. Make an association utilizing this strategy.
Step 3: Create a statement object
You need to make a Statement or PreparedStatement or, CallableStatement objects to send SQL explanations to the data set. You can make these utilizing the strategies createStatement(), prepareStatement() and, prepareCall() separately. Make both of these articles utilizing the suitable technique.
Step 4: Execute the query
In the wake of making an assertion, you need to execute it. The Statement class gives different techniques to execute a question like the execute() strategy to execute an explanation that profits more than one outcome set. The executeUpdate() technique executes inquiries like INSERT, UPDATE, DELETE. The executeQuery() technique brings information back. Use both of these strategies and execute the assertion made already.
Example
Following JDBC model shows how to refresh the current records of a table in Apache Derby utilizing JDBC program. Here, we are interfacing with a data set named sampleDB (will make in the event that it doesn't exist) utilizing the inserted driver.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateData {
public static void main(String args[]) throws Exception {
//Registering the driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//Getting the Connection object
String URL = "jdbc:derby:sampleDB;create=true";
Connection conn = DriverManager.getConnection(URL);
//Creating the Statement object
Statement stmt = conn.createStatement();
//Creating a table and populating it
String query = "CREATE TABLE Employees("
+ "Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
+ "Name VARCHAR(255), Salary INT NOT NULL, "
+ "Location VARCHAR(255), "
+ "PRIMARY KEY (Id))";
String query = "INSERT INTO Employees("
+ "Name, Salary, Location) VALUES "
+ "('Amit', 30000, 'Hyderabad'), "
+ "('Kalyan', 40000, 'Vishakhapatnam'), "
+ "('Renuka', 50000, 'Delhi'), "
+ "('Archana', 15000, 'Mumbai'), "
+ "('Trupthi', 45000, 'Kochin'), "
+ "('Suchatra', 33000, 'Pune'), "
+ "('Rahul', 39000, 'Lucknow'), "
+ "('Trupti', 45000, 'Kochin')";
//Executing the query
String query = "UPDATE Employees SET Location = 'Chennai', Salary = 43000 WHERE
Name = 'Kalyan'";
int num = stmt.executeUpdate(query);
System.out.println("Number of records updated are: "+num);
}
}
Output
On executing the above program, you will get the accompanying yield −
Number of records updated are: 1