|
|
CallableStatement or PreparedStatement
void setObject(int parameterIndex, Object x);
void setObject(int parameterIndex, Object x, int targetSqlType);
void setObject(int parameterIndex, Object x, int targetSqlType, int scale);
When no target SQL type is specified to the setObject() method, the Java object is directly converted according to the default mapping as shown in Table 7-3. If a target SQL type is provided, the Java object is first mapped to its default corresponding type and then converted to the specified SQL type (see Table 7-2).
Table 7-3 DEFAULT MAPPING FROM JAVA OBJECT TYPES TO SQL TYPES
|
Java object type
| SQL type
|
String
| VARCHAR or LONGVARCHAR
|
java.math.BigDecimal
| NUMERIC
|
Boolean
| BIT
|
Integer
| INTEGER
|
Long
| BIGINT
|
Float
| REAL
|
Double
| DOUBLE
|
byte[ ]
| VARBINARY or LONGVARBINARY
|
java.sql.Date
| DATE
|
java.sql.Time
| TIME
|
java.sql.Timestamp
| TIMESTAMP
|
|
Multithreading
JDBC is multithread safe. Several threads can call the same java.sql object simultaneously and perform operations asynchronously. For example, multiple statements can be executed concurrently within the same connection. It does not mean that all JDBC drivers are able to manage concurrent executions asynchronously, but even in this case, the developer can assume fully concurrent executions. Indeed, fully JDBC-compliant drivers will serialize calls automatically, even if they do not support asynchronous executions (i.e., where they are not multithread safe themselves). Such behavior is totally transparent to the programmer, even if the driver provides some form of synchronization. In this case, the application threads will run concurrently (a reduced concurrency).
Multithread support is exploitable to execute multiple statements on the same connection and to allow more control over a running execution. Indeed, it is possible to cancel a long running statement in one thread using the Statement.cancel() method of another thread.
Summary
This chapter discussed advanced techniques of SQL and JDBC that add a professional touch to applications, including:
- Handling stored procedures from JDBC
- Dynamic SQL
- Fetching database metadata
- Dynamic data access
The next part of this book contains many working examples, from simple to complex, that cover most of the theory discussed in previous chapters.
|