Sobes.tech
Middle

Перечислите основные типы данных, используемые в JDBC, и объясните, как они связаны с типами данных Java.

sobes.tech AI

Answer from AI

SQL Data Type Java Data Type Description
CHAR, VARCHAR, LONGVARCHAR String Character strings
NUMERIC, DECIMAL BigDecimal Fixed-point numbers
BIT, BOOLEAN boolean Logical values
TINYINT byte Integer from -128 to 127
SMALLINT short Integer from -32768 to 32767
INTEGER int Integer
BIGINT long Large integer
REAL float Single-precision floating-point number
FLOAT, DOUBLE double Double-precision floating-point number
BINARY, VARBINARY, LONGVARBINARY byte[] Binary data
DATE java.sql.Date Date (year, month, day)
TIME java.sql.Time Time (hour, minute, second)
TIMESTAMP java.sql.Timestamp Date and time with milliseconds
BLOB java.sql.Blob Large binary object
CLOB java.sql.Clob Large character object
ARRAY java.sql.Array Array of values
STRUCT java.sql.Struct Structured type
REF java.sql.Ref Reference to a structured type
DATALINK java.net.URL Link to an external resource
JAVA_OBJECT Object Serialized Java object (depends on implementation)
ROWID java.sql.RowId Row identifier (depends on DBMS)
NCHAR, NVARCHAR, LONGNVARCHAR String National character strings
NCLOB java.sql.NClob National character large object
SQLXML java.sql.SQLXML XML data

JDBC (Java Database Connectivity) specifies standard SQL data types in the java.sql.Types class. These data types are represented as numeric constants. JDBC drivers map these SQL data types to appropriate Java data types when reading data from the database (e.g., using getXXX() methods of ResultSet) and when writing data to the database (e.g., using setXXX() methods of PreparedStatement).

The mapping is determined by how the specific database's JDBC driver implements the mapping between the database's internal data types and the standard java.sql.Types, and then to Java data types. This mapping is documented in the JDBC specification and may vary slightly between drivers, although the main mappings listed in the table are standard.

Example of retrieving data from ResultSet:

// Assuming rs is a ResultSet
String name = rs.getString("column_name_varchar");
int id = rs.getInt("column_name_integer");
BigDecimal value = rs.getBigDecimal("column_name_numeric");
Date date = rs.getDate("column_name_date");

Example of setting parameters in PreparedStatement:

// Assuming pstmt is a PreparedStatement
String name = "Test Name";
int id = 123;
BigDecimal value = new BigDecimal("100.50");
Date date = new Date(System.currentTimeMillis());

pstmt.setString(1, name); // 1 is the parameter index (starting from 1)
pstmt.setInt(2, id);
pstmt.setBigDecimal(3, value);
pstmt.setDate(4, date);

JDBC strives to ensure maximum compatibility, but for vendor-specific or specialized data types, special approaches or the use of getObject() / setObject() methods with subsequent casting or instanceof checks may be necessary.

Перечислите основные типы данных, используемые в… - sobes.tech