The JDBC basic data types are the data types that were introduced in the JDBC 1.0 core API. The Microsoft SQL Server 2005 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.
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 nchar |
CHAR |
String |
decimal money smallmoney |
DECIMAL |
java.math.BigDecimal |
float |
DOUBLE |
double |
int |
INTEGER |
int |
image |
LONGVARBINARY |
byte[] |
text ntext |
LONGVARCHAR |
String |
numeric |
NUMERIC |
java.math.BigDecimal |
real |
REAL |
float |
smallint tinyint |
SMALLINT |
short |
datetime smalldatetime |
TIMESTAMP |
java.sql.Timestamp |
varbinary |
VARBINARY |
byte[] |
varchar nvarchar |
VARCHAR |
String |
uniqueidentifier |
CHAR |
String |
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, you should 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, you should 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.