2 回答

TA貢獻1797條經(jīng)驗 獲得超6個贊
如果 JDBC 驅(qū)動程序不支持您嘗試使用的類型 - 它會嘗試將其映射到支持的最接近的可能數(shù)據(jù)類型。如果它不能做到這一點 -SQLException
被拋出。
JDBC 只是一個用于處理數(shù)據(jù)庫的接口。數(shù)據(jù)庫可能不支持 API 的某些部分。在這種情況下會發(fā)生什么完全取決于驅(qū)動程序的實現(xiàn)。這對所有 JDBC 驅(qū)動程序都很常見。

TA貢獻1796條經(jīng)驗 獲得超4個贊
如您所知,每個 RDBMS 都會發(fā)布其 JDBC 實現(xiàn),而 JDBC 會嘗試將 Java 的數(shù)據(jù)類型映射到數(shù)據(jù)庫的數(shù)據(jù)類型。如果沒有合適的映射選項,JDBC 將拋出java.sql.SQLException: Invalid column type
.
根據(jù) MySQL 的文檔,它不支持,但IBM Informix和OracleJAVA_OBJECT
似乎支持它。
同時,如果你想插入一個 Java 對象到一個表中,你可以將它映射到BLOB
.?假設您使用以下命令創(chuàng)建了一個表:
CREATE TABLE java_objects (?
? id INT AUTO_INCREMENT,?
? name varchar(128),?
? object_value BLOB,?
? primary key (id));
您可以使用此示例代碼插入您的對象。
public long insertAnObject(String className, Object javaObject)
{
? ? int id = -1;
? ? try
? ? {
? ? ? ? PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO java_objects(name, object_value) VALUES (?, ?)");
? ? ? ? // set input parameters
? ? ? ? preparedStatement.setString(1, className);
? ? ? ? preparedStatement.setObject(2, javaObject);
? ? ? ? preparedStatement.executeUpdate();
? ? ? ? // get the generated key for the id
? ? ? ? ResultSet resultSet = preparedStatement.getGeneratedKeys();
? ? ? ? if (resultSet.next())
? ? ? ? {
? ? ? ? ? ? id = resultSet.getInt(1);
? ? ? ? }
? ? ? ? resultSet.close();
? ? ? ? preparedStatement.close();
? ? ? ? return id;
? ? } catch (SQLException e)
? ? {
? ? ? ? e.printStackTrace();
? ? }
? ? return id;
}
添加回答
舉報