Creates a Statement object and calls the Statement.executeUpdate method.
String updateString = "INSERT INTO aDatabase VALUES (some text)"; int count = stmt.executeUpdate(updateString);
You can update a value in a result set by calling the ResultSet.update < type > method on the row where the cursor is
positioned.
The <type> value here is the same used when retrieving a value from the result set.
For example,
rs.first();
updateDouble("balance", rs.getDouble("balance") - 5.00);
The update applies only to the result set until the call to rs.updateRow(), which updates the underlying database.To delete the current row, use rs.deleteRow().
To insert a new row, use rs.moveToInsertRow().
This special type of statement is derived from the more general class, Statement.
If you want to execute a Statement object many
times, it will normally reduce execution time to use a PreparedStatement object instead. The advantage of this is that in most
cases, this SQL statement will be sent to the DBMS right away, where it will be compiled.
As a result, the PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the
PreparedStatement is executed, the DBMS can just run the PreparedStatement 's SQL statement without having to compile it
first.
PreparedStatement updateSales =
con.prepareStatement("UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?");
The first step is to create a CallableStatement object.
This is done with an open Connection object for both the Statement and PreparedStatement objects.
A CallableStatement object contains a call to a stored procedure.
E.g.
CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}");
ResultSet rs = cs.executeQuery();
SQLWarning objects are a subclass of SQLException that deal with database access warnings.
Warnings do not stop the execution of an application the way exceptions do.
Warnings simply alert the user that something did not happen as planned.
A warning can be reported on
SQLWarning warning = stmt.getWarnings();
if (warning != null) {
while (warning != null) {
System.out.println("Message: " + warning.getMessage());
System.out.println("SQLState: " + warning.getSQLState());
System.out.print("Vendor error code: ");
System.out.println(warning.getErrorCode());
warning = warning.getNextWarning();
}
}
Another new feature in the JDBC 2.0 API is the ability to update rows in a result set using methods in the Java programming
language rather than having to send an SQL command.
But before you can take advantage of this capability, you need to create a
ResultSet object that is updatable. In order to do this, you supply the ResultSet constant CONCUR_UPDATABLE to the createStatement
method.
Connection con = DriverManager.getConnection("jdbc:mySubprotocol:mySubName");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = ("SELECT COF_NAME, PRICE FROM COFFEES");
Both Statements and PreparedStatements have an additional constructor that accepts a scroll type and an update type parameter.
The scroll type value can be one of the following values:
In the constructors of Statements and PreparedStatements, you may use
You may verify that your database supports these types by calling
con.getMetaData().supportsResultSetConcurrency(ResultSet.SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
By default, every JDBC statement is sent to the database individually.
To send multiple statements at one time ,
Statement stmt = con.createStatement();
stmt.addBatch("update registration set balance=balance-5.00 where theuser="+theuser);
stmt.addBatch("insert into auctionitems(description, startprice) values("+description+","+startprice+")");
...
int[] results = stmt.executeBatch();
The return result of the addBatch() method is an array of row counts affected for each statement executed in the batch job.
If a problem occurred, a java.sql.BatchUpdateException is thrown.
An incomplete array of row counts can be obtained from BatchUpdateException by calling its getUpdateCounts() method.
The following block of code can be used to store an image:
int itemnumber=400456;
File file = new File(itemnumber+".jpg");
FileInputStream fis = new FileInputStream(file);
PreparedStatement pstmt = con.prepareStatement
("update auctionitems set theimage=? where id= ?");
pstmt.setBinaryStream(1, fis, (int)file.length()):
pstmt.setInt(2, itemnumber);
pstmt.executeUpdate();
pstmt.close();
fis.close();
To retrieve an image:
int itemnumber=400456;
byte[] imageBytes;//hold an image bytes to pass to createImage().
PreparedStatement pstmt = con.prepareStatement("select theimage from auctionitems where id= ?");
pstmt.setInt(1, itemnumber);
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
imageBytes = rs.getBytes(1);
}
pstmt.close();
rs.close();
Image auctionimage = Toolkit.getDefaultToolkit().createImage(imageBytes);
A class can be serialized to a binary database field in much the same way as the image.
You may use the code above to store and retrive an object.
Use getMetaData().getColumnType() method to check data type.
For example to retrieve an Integer, you may check it first:
int count=0;
Connection con=getConnection();
Statement stmt= con.createStatement();
stmt.executeQuery("select counter from Table");
ResultSet rs = stmt.getResultSet();
if(rs.next()) {
if(rs.getMetaData().getColumnType(1) == Types.INTEGER) {
Integer i=(Integer)rs.getObject(1);
count=i.intValue();
}
}
rs.close();
Because java.util.Date represents both date and time.
SQL uses three types to represent date and time.
Use the code below:
Calendar currenttime=Calendar.getInstance();
java.sql.Date startdate= new java.sql.Date((currenttime.getTime()).getTime());
or
SimpleDateFormat template = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date enddate = new java.util.Date("10/31/99");
java.sql.Date sqlDate = java.sql.Date.valueOf(template.format(enddate));