sql server java类型_使用基本 JDBC 数据类型 - SQL Server | Microsoft Docs
使用基本數據類型Using basic data types
01/29/2021
本文內容
Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server 使用 JDBC 基本數據類型將 SQL ServerSQL Server 數據類型轉換為 Java 編程語言能夠理解的格式,反之亦然。The Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server uses the JDBC basic data types to convert the SQL ServerSQL Server data types to a format that can be understood by the Java programming language, and vice versa. JDBC 驅動程序支持 JDBC 4.0 API,其中包括 SQLXML 數據類型和區域 (Unicode) 數據類型,如 NCHAR 、NVARCHAR 、LONGNVARCHAR 和 NCLOB 。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
下表列出了基本 SQL ServerSQL Server、JDBC 和 Java 編程語言數據類型之間的默認映射:The following table lists the default mappings between the basic SQL ServerSQL Server, JDBC, and Java programming language data types:
SQL Server 類型SQL Server Types
JDBC 類型 (java.sql.Types)JDBC Types (java.sql.Types)
Java 語言類型Java Language Types
bigintbigint
BIGINTBIGINT
longlong
binarybinary
BINARYBINARY
byte[]byte[]
bitbit
BITBIT
booleanboolean
charchar
CHARCHAR
字符串String
datedate
DATEDATE
java.sql.Datejava.sql.Date
datetime3datetime3
TIMESTAMPTIMESTAMP
java.sql.Timestampjava.sql.Timestamp
datetime2datetime2
TIMESTAMPTIMESTAMP
java.sql.Timestampjava.sql.Timestamp
datetimeoffset2datetimeoffset2
microsoft.sql.Types.DATETIMEOFFSETmicrosoft.sql.Types.DATETIMEOFFSET
microsoft.sql.DateTimeOffsetmicrosoft.sql.DateTimeOffset
Decimaldecimal
DECIMALDECIMAL
java.math.BigDecimaljava.math.BigDecimal
FLOATfloat
DOUBLEDOUBLE
doubledouble
imageimage
LONGVARBINARYLONGVARBINARY
byte[]byte[]
intint
INTEGERINTEGER
intint
moneymoney
DECIMALDECIMAL
java.math.BigDecimaljava.math.BigDecimal
ncharnchar
CHARCHAR
NCHAR (Java SE 6.0)NCHAR (Java SE 6.0)
字符串String
ntextntext
LONGVARCHARLONGVARCHAR
LONGNVARCHAR (Java SE 6.0)LONGNVARCHAR (Java SE 6.0)
字符串String
numericnumeric
NUMERICNUMERIC
java.math.BigDecimaljava.math.BigDecimal
nvarcharnvarchar
VARCHARVARCHAR
NVARCHAR (Java SE 6.0)NVARCHAR (Java SE 6.0)
字符串String
nvarchar(max)nvarchar(max)
VARCHARVARCHAR
NVARCHAR (Java SE 6.0)NVARCHAR (Java SE 6.0)
字符串String
realreal
realREAL
FLOATfloat
smalldatetimesmalldatetime
TIMESTAMPTIMESTAMP
java.sql.Timestampjava.sql.Timestamp
smallintsmallint
SMALLINTSMALLINT
shortshort
smallmoneysmallmoney
DECIMALDECIMAL
java.math.BigDecimaljava.math.BigDecimal
texttext
LONGVARCHARLONGVARCHAR
字符串String
timetime
TIME1TIME1
java.sql.Time1java.sql.Time1
timestamptimestamp
BINARYBINARY
byte[]byte[]
tinyinttinyint
TINYINTTINYINT
shortshort
udtudt
VARBINARYVARBINARY
byte[]byte[]
uniqueidentifieruniqueidentifier
CHARCHAR
字符串String
varbinaryvarbinary
VARBINARYVARBINARY
byte[]byte[]
varbinary(max)varbinary(max)
VARBINARYVARBINARY
byte[]byte[]
varcharvarchar
VARCHARVARCHAR
字符串String
varchar(max)varchar(max)
VARCHARVARCHAR
字符串String
xmlxml
LONGVARCHARLONGVARCHAR
LONGNVARCHAR (Java SE 6.0)LONGNVARCHAR (Java SE 6.0)
字符串String
SQLXMLSQLXML
sqlvariantsqlvariant
microsoft.sql.Types.SQL_VARIANTmicrosoft.sql.Types.SQL_VARIANT
對象Object
geometrygeometry
VARBINARYVARBINARY
byte[]byte[]
geographygeography
VARBINARYVARBINARY
byte[]byte[]
1 若要將 java.sql.Time 與時間 SQL ServerSQL Server 類型一起使用,必須將 sendTimeAsDatetime 連接屬性設置為 false。1 To use java.sql.Time with the time SQL ServerSQL Server type, you must set the sendTimeAsDatetime connection property to false.
2 可以編程方式使用 DateTimeOffset 類訪問 datetimeoffset 的值。2 You can programmatically access values of datetimeoffset with DateTimeOffset Class.
3 請注意,從 SQL Server 2016 開始,java.sql.Timestamp 值不能再用于比較 datetime 列中的值。3 Note that java.sql.Timestamp values can no longer be used to compare values from a datetime column starting from SQL Server 2016. 此限制是由于以不同方式將 datetime 轉換為 datetime2 的服務器端更改,從而導致不可相等的值。This limitation is due to a server-side change that converts datetime to datetime2 differently, resulting in non-equitable values. 此問題的解決方法是將 datetime 列更改為 datetime2(3),使用 String 而不是 java.sql.Timestamp,或將數據庫兼容級別更改為 120 或更低。The workaround to this issue is to either change datetime columns to datetime2(3), use String instead of java.sql.Timestamp, or change database compatibility level to 120 or below.
以下幾部分提供了如何使用 JDBC 驅動程序和基本數據類型的示例。The following sections provide examples of how you can use the JDBC Driver and the basic data types. 有關如何在 Java 應用程序中使用基本數據類型的更多詳細示例,請參閱基本數據類型示例。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
如果必須從映射到任意 JDBC 基本數據類型的數據源檢索數據,并以字符串的格式查看這些數據,或者如果不需要強類型的數據,則可以使用 SQLServerResultSet 類的 getString 方法,如下所示: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:
try(Statement stmt = con.createStatement();) {
ResultSet rs = stmt.executeQuery("SELECT lname, job_id FROM employee WHERE (lname = 'Brown')");
rs.next();
short empJobID = rs.getShort("job_id");
}
按數據類型檢索數據Retrieving data by data type
如果必須從數據源檢索數據,并且已知檢索的數據類型,則應使用 SQLServerResultSet 類的任一 get 方法(也稱為 Getter 方法)。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 methods of the SQLServerResultSet class, also known as the getter methods. 通過 get 方法,可以使用列名或列索引,如下所示:You can use either a column name or a column index with the get methods, as in the following:
try(Statement stmt = con.createStatement();) {
ResultSet rs = stmt.executeQuery("SELECT lname, job_id FROM employee WHERE (lname = 'Brown')");
rs.next();
short empJobID = rs.getShort("job_id");
}
備注
使用確定位數的 getUnicodeStream 和 getBigDecimal 方法已遭棄用,不受 JDBC 驅動程序支持。The getUnicodeStream and getBigDecimal with scale methods are deprecated and are not supported by the JDBC driver.
按數據類型更新數據Updating data by data type
如果必須更新數據源中字段的值,請使用 SQLServerResultSet 類的一種 update 方法。If you have to update the value of a field in a data source, use one of the update methods of the SQLServerResultSet class. 在下面的示例中,updateInt 方法與 updateRow 方法結合使用,用于更新數據源中的數據:In the following example, the updateInt method is used in conjunction with the updateRow method to update the data in the data source:
try (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();
int empJobID = rs.getInt(2);
empJobID++;
rs.first();
rs.updateInt(2, empJobID);
rs.updateRow();
}
備注
JDBC Driver 無法更新列名長度超過 127 個字符的 SQL Server 列。The JDBC driver cannot update a SQL Server column with a column name that is more than 127 characters long. 如果嘗試更新名稱長度超過 127 個字符的列,將引發異常。If an update to a column whose name is more than 127 characters is attempted, an exception is thrown.
通過參數化查詢來更新數據Updating data by parameterized query
如果必須通過使用參數化查詢來更新數據源中的數據,可以使用 SQLServerPreparedStatement 類的任一 set 方法(也稱為 setter 方法)來設置參數的數據類型。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 methods of the SQLServerPreparedStatement class, also known as the setter methods. 在下面的示例中,prepareStatement 方法用于預編譯參數化查詢,然后在調用 executeUpdate 方法前,使用 setString 方法來設置參數的字符串值。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.
try(PreparedStatement pstmt = con.prepareStatement("UPDATE employee SET fname = ? WHERE (lname = 'Brown')");) {
String name = "Bob";
pstmt.setString(1, name);
int rowCount = pstmt.executeUpdate();
}
若要詳細了解參數化查詢,請參閱使用包含參數的 SQL 語句。For more information about parameterized queries, see Using an SQL statement with parameters.
向存儲過程傳遞參數Passing parameters to a stored procedure
如果必須向存儲過程傳遞類型參數,則可使用 SQLServerCallableStatement 類的任一 set 方法通過索引或名稱來設置此參數。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 methods of the SQLServerCallableStatement class. 在下面的示例中,prepareCall 方法用于設置對存儲過程的調用,然后在調用 executeQuery 方法之前,使用 setString 方法設置調用的參數。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.
try(CallableStatement cstmt = con.prepareCall("{call employee_jobid(?)}");) {
String lname = "Brown";
cstmt.setString(1, lname);
ResultSet rs = cstmt.executeQuery();
}
備注
在此實例中,將返回一個結果集,包含此存儲過程的運行結果。In this example, a result set is returned with the results of running the stored procedure.
若要詳細了解結合使用 JDBC 驅動程序與存儲過程和輸入參數,請參閱使用包含輸入參數的存儲過程。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
如果必須從存儲過程檢索參數,則必須首先使用 SQLServerCallableStatement 類的 registerOutParameter 方法通過名稱或索引注冊一個輸出參數,然后在調用存儲過程后,將返回的輸出參數分配給合適的變量。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. 在下面的示例中,使用 prepareCall 方法設置對存儲過程的調用,使用 registerOutParameter 方法設置輸出參數,然后在調用 executeQuery 方法前,使用 setString 方法設置調用的參數。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. 使用 getShort 方法來檢索存儲過程的輸出參數返回的值。The value that is returned by the out parameter of the stored procedure is retrieved by using the getShort method.
try(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);
}
備注
除返回的輸出參數外,還可能返回一個結果集,包含此存儲過程的運行結果。In addition to the returned out parameter, a result set might also be returned with the results of running the stored procedure.
若要詳細了解結合使用 JDBC 驅動程序與存儲過程和輸出參數,請參閱使用包含輸出參數的存儲過程。For more information about how to use the JDBC driver with stored procedures and output parameters, see Using a stored procedure with output parameters.
另請參閱See also
總結
以上是生活随笔為你收集整理的sql server java类型_使用基本 JDBC 数据类型 - SQL Server | Microsoft Docs的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java双链表基本方法_Java数据结构
- 下一篇: docker安装mysql远程不能访问,