Wednesday, October 1, 2014

2.3 Data Types in JDBC


2.3 Data Types in JDBC

-          Generally, Oracle datatypes and host language datatypes are not the same.
-          Values are passed from Oracle to Java and vice versa, they need to be cast from one datatype to the other. 
-          The JDBC driver can automatically convert values of Oracle datatypes to values of some of the Java datatypes. 
-          The "Using Prepared Statements" section will cover conversions in the other direction.
-          The JDBC driver converts the Java data type to the appropriate JDBC type before sending it to the database.
-          It uses a default mapping for most data types.
-          For example, a Java int is converted to an SQL INTEGER.
-          Default mappings were created to provide consistency (reliability/stability)  between drivers.
-          The following table summarizes the default JDBC data type
-          That the Java data type is converted to when we call :
1)      the setXXX() method of the PreperedStatement or
2)      CallableStatement object or
3)      the ResultSet.
4)      updateXXX() method.

-          the setXXX() method of the PreparedStatement or CallableStatement object or the ResultSet.updateXXX() method.


SQL
JDBC/Java
setXXX
updateXXX
VARCHAR
java.lang.String
setString
updateString
CHAR
java.lang.String
setString
updateString
LONGVARCHAR
java.lang.String
setString
updateString
BIT
Boolean
setBoolean
updateBoolean
NUMERIC
java.math.BigDecimal
setBigDecimal
updateBigDecimal
TINYINT
Byte
setByte
updateByte
SMALLINT
Short
setShort
updateShort
INTEGER
Int
setInt
updateInt
BIGINT
Long
setLong
updateLong
REAL
Float
setFloat
updateFloat
FLOAT
Float
setFloat
updateFloat
DOUBLE
Double
setDouble
updateDouble
VARBINARY
byte[ ]
setBytes
updateBytes
BINARY
byte[ ]
setBytes
updateBytes
DATE
java.sql.Date
setDate
updateDate
TIME
java.sql.Time
setTime
updateTime
TIMESTAMP
java.sql.Timestamp
setTimestamp
updateTimestamp
CLOB
java.sql.Clob
setClob
updateClob
BLOB
java.sql.Blob
setBlob
updateBlob
ARRAY
java.sql.Array
setARRAY
updateARRAY
REF
java.sql.Ref
SetRef
updateRef
STRUCT
java.sql.Struct
SetStruct
updateStruct

-         JDBC 3.0 has enhanced support for BLOB, CLOB, ARRAY, and REF data types.
-         The ResultSet object now has updateBLOB(), updateCLOB(), updateArray(), and updateRef() methods that enable we to directly manipulate the respective data on the server.
-         The setXXX() and updateXXX() methods enable we to convert specific Java types to specific JDBC data types.
-         The methods, setObject() and updateObject(), enable we to map almost any Java type to a JDBC data type.
-         ResultSet object provides corresponding getXXX() method for each data type to retrieve column value.
-         Each method can be used with column name or by its ordinal position.

-        Date & Time Data Types:
-          The java.sql.Date class maps to the SQL DATE type, and the java.sql.
-          Time and java.sql.Timestamp classes map to the SQL TIME and SQL TIMESTAMP data types, respectively.
-          Following examples shows how the Date and Time classes format standard Java date and time values to match the SQL data type requirements.


-          import java.sql.Date;
-          import java.sql.Time;
-          import java.sql.Timestamp;
-          import java.util.*;
-           
-          public class SqlDateTime
-          {
-             public static void main(String[] args)
-             {
-                //Get standard date and time
-                java.util.Date javaDate = new java.util.Date();
-                long javaTime = javaDate.getTime();
-                System.out.println("The Java Date is:" +
-                       javaDate.toString());
-           
-                //Get and display SQL DATE
-                java.sql.Date sqlDate = new java.sql.Date(javaTime);
-                System.out.println("The SQL DATE is: " +
-                       sqlDate.toString());
-           
-                //Get and display SQL TIME
-                java.sql.Time sqlTime = new java.sql.Time(javaTime);
-                System.out.println("The SQL TIME is: " +
-                       sqlTime.toString());
-                //Get and display SQL TIMESTAMP
-                java.sql.Timestamp sqlTimestamp =
-                new java.sql.Timestamp(javaTime);
-                System.out.println("The SQL TIMESTAMP is: " +
-                       sqlTimestamp.toString());
-               }//end main
-          }//end SqlDateTime
 
-         C:\>javac SqlDateTime.java
-          C:\>
 
-         C:\>java SqlDateTime
-          The Java Date is:Tue Aug 18 13:46:02 GMT+04:00 2009
-          The SQL DATE is: 2014-06-24
-          The SQL TIME is: 13:46:02
-          The SQL TIMESTAMP is: 2014-06-24 13:46:02.828
-          C:\>
 
-          Avoid using getXXX( ) methods that return primitive data types.
-          Use wrapper classes for primitive data types.

No comments:

Post a Comment