The Microsoft SQL Server JDBC Driver uses the JDBC basic data types to convert the SQL Server data types to a format that can be understood by the Java programming language, and vice versa. Starting with the Microsoft SQL Server JDBC Driver version 2.0, the JDBC driver provides support for the JDBC 4.0 API, which includes the SQLXML data type, and National (Unicode) data types, such as NCHAR, NVARCHAR, LONGNVARCHAR, and NCLOB.
Data Type Mappings
The following table lists the default mappings between the basic SQL Server, JDBC, and Java programming language data types:
SQL Server Types | JDBC Types (java.sql.Types) | Java Language Types |
---|---|---|
bigint |
BIGINT |
long |
timestamp binary |
BINARY |
byte[] |
bit |
BIT |
boolean |
char |
CHAR |
String |
decimal money smallmoney |
DECIMAL |
java.math.BigDecimal |
float |
DOUBLE |
double |
int |
INTEGER |
int |
image varbinary(max) |
LONGVARBINARY |
byte[] |
varchar(max) text |
LONGVARCHAR |
String |
nchar |
CHAR NCHAR (Java SE 6.0) |
String |
nvarchar |
VARCHAR NVARCHAR (Java SE 6.0) |
String |
nvarchar(max) ntext |
LONGVARCHAR LONGNVARCHAR (Java SE 6.0) |
String |
numeric |
NUMERIC |
java.math.BigDecimal |
real |
REAL |
float |
smallint |
SMALLINT |
short |
datetime smalldatetime |
TIMESTAMP |
java.sql.Timestamp |
varbinary udt |
VARBINARY |
byte[] |
varchar |
VARCHAR |
String |
tinyint |
TINYINT |
short |
uniqueidentifier |
CHAR |
String |
xml |
LONGVARCHAR SQLXML (Java SE 6.0) |
String SQLXML |
time |
TIME (1) |
java.sql.Time (1) |
date |
DATE |
java.sql.Date |
datetime2 |
TIMESTAMP |
java.sql.Timestamp |
datetimeoffset (2) |
microsoft.sql.Types.DATETIMEOFFSET |
microsoft.sql.DateTimeOffset |
(1) To use java.sql.Time with the time SQL Server type, you must set the sendTimeAsDatetime connection property to false.
(2) You can programmatically access values of datetimeoffset with DateTimeOffset Class.
The SQL Server sqlvariant data type is not currently supported by the JDBC driver. If a query is used to retrieve data from a table that contains a column of the sqlvariant data type, an exception will occur.
The following sections provide examples of how you can use the JDBC Driver and the basic data types. For a more detailed example of how to use the basic data types in a Java application, see Basic Data Types Sample.
Retrieving Data as a String
If you have to retrieve data from a data source that maps to any of the JDBC basic data types for viewing as a string, or if strongly typed data is not required, you can use the getString method of the SQLServerResultSet class, as in the following:
String SQL = "SELECT TOP 10 * FROM Person.Contact"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(SQL); while (rs.next()) { System.out.println(rs.getString(4) + " " + rs.getString(6)); } rs.close(); stmt.close();
Retrieving Data by Data Type
If you have to retrieve data from a data source, and you know the type of data that is being retrieved, use one of the get<Type> methods of the SQLServerResultSet class, also known as the getter methods. You can use either a column name or a column index with the get<Type> methods, as in the following:
ResultSet rs = stmt.executeQuery("SELECT lname, job_id FROM employee WHERE (lname = 'Brown')"); rs.next(); short empJobID = rs.getShort("job_id"); rs.close(); stmt.close();
Updating Data by Data Type
If you have to update the value of a field in a data source, use one of the update<Type> methods of the SQLServerResultSet class. In the following example, the updateInt method is used in conjunction with the updateRow method to update the data in the data source:
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery("SELECT lname, job_id FROM employee WHERE (lname = 'Brown')"); rs.next(); short empJobID = rs.getInt(2); empJobID++; rs.first(); rs.updateInt(2, empJobID); rs.updateRow(); rs.close(); stmt.close();
Updating Data by Parameterized Query
If you have to update data in a data source by using a parameterized query, you can set the data type of the parameters by using one of the set<Type> methods of the SQLServerPreparedStatement class, also known as the setter methods. In the following example, the prepareStatement method is used to pre-compile the parameterized query, and then the setString method is used to set the string value of the parameter before the executeUpdate method is called.
PreparedStatement pstmt = con.prepareStatement("UPDATE employee SET fname = ? WHERE (lname = 'Brown')"); String first = "Bob"; pstmt.setString(1, first); int rowCount = pstmt.executeUpdate(); pstmt.close();
For more information about parameterized queries, see Using an SQL Statement with Parameters.
Passing Parameters to a Stored Procedure
If you have to pass typed parameters into a stored procedure, you can set the parameters by index or name by using one of the set<Type> methods of the SQLServerCallableStatement class. In the following example, the prepareCall method is used to set up the call to the stored procedure, and then the setString method is used to set the parameter for the call before the executeQuery method is called.
CallableStatement cstmt = con.prepareCall("{call employee_jobid(?)}"); String lname = "Brown"; cstmt.setString(1, lname); Resultset rs = cstmt.executeQuery(); rs.close(); cstmt.close();
For more information about using the JDBC driver with stored procedures and input parameters, see Using a Stored Procedure with Input Parameters.
Retrieving Parameters from a Stored Procedure
If you have to retrieve parameters back from a stored procedure, you must first register an out parameter by name or index by using the registerOutParameter method of the SQLServerCallableStatement class, and then assign the returned out parameter to an appropriate variable after you run the call to the stored procedure. In the following example, the prepareCall method is used to set up the call to the stored procedure, the registerOutParameter method is used to set up the out parameter, and then the setString method is used to set the parameter for the call before executeQuery method is called. The value that is returned by the out parameter of the stored procedure is retrieved by using the getShort method.
CallableStatement cstmt = con.prepareCall("{call employee_jobid (?, ?)}"); cstmt.registerOutParameter(2, java.sql.Types.SMALLINT); String lname = "Brown"; cstmt.setString(1, lname); Resultset rs = cstmt.executeQuery(); short empJobID = cstmt.getShort(2); rs.close(); cstmt.close();
For more information about how to use the JDBC driver with stored procedures and output parameters, see Using a Stored Procedure with Output Parameters.