OceanBase JDBC supports modifying a database through DML and DDL operations.
DML operations
To perform a DML INSERT or UPDATE operation, create a Statement or PreparedStatement object. You can use the PreparedStatement object to run a statement with a set of input parameters. The prepareStatement method of the JDBC Connection object can define a statement, bind variables to parameters, and return the JDBC PreparedStatement object with a statement definition.
The PreparedStatement object uses the setXXX method to bind data to a statement that is ready to be sent to the database.
Example: Use the PreparedStatement object to perform the INSERT operation to add two rows of data to the customers table.
PreparedStatement ps = null;
try{
ps = conn.prepareStatement ("insert into customers (customerID, name) values (?, ?)");
ps.setInt (1, 150); // The first quotation mark (?) corresponds to the customerID.
ps.setString (2, "Adam"); // The second quotation mark (?) corresponds to the name.
ps.execute ();
ps.setInt (1, 207);
ps.setString (2, "Alice");
ps.execute ();
}
finally{
if(ps!=null)
ps.close();
}
DDL operations
To perform a DDL operation, create a Statement or PreparedStatement object.
Example: Using the Statement object to create a table in the database
// Create the customers table and the customerID and name columns.
String query;
Statement st=null;
try{
query="create table customers " +
"(customerID int, " +
"name varchar(50))";
st = conn.createStatement();
st.executeUpdate(query);
}
finally{
// Close the Statement object.
st.close();
}
If the DDL operation needs to be performed again, a new DDL statement must be prepared.
Example: Preparing a DDL statement before the DDL operation is performed again
//
PreparedStatement ps = null;
PreparedStatement ts = null;
try{
ps = conn.prepareStatement ("insert into customers(customerID, name) values (?, ?)");
// Add new customer Adam numbered 150.
ps.setInt (1, 150); // The first quotation mark (?) corresponds to the customerID.
ps.setString (2, "Adam"); // The second quotation mark (?) corresponds to the name.
// Insert data.
ps.execute ();
ts = conn.prepareStatement("truncate table customers");
ts.executeUpdate();
// Add new customer Alice numbered 207.
ps.setInt (1, 207); // The first quotation mark (?) corresponds to the customerID.
ps.setString (2, "Alice"); // The second quotation mark (?) corresponds to the name.
// Insert data.
ps.execute ();
ts.close();
ts = conn.prepareStatement("truncate table customers");
ts.executeUpdate();
}
finally{
if(ps!=null)
// Close the Statement object.
ps.close();
}