1

我已经在 SQL Server 2008 中声明了一个 UDT(用户定义类型),比如 CustomType。我正在将此数据类型用作存储过程的 IN 参数。我想从 Java 类调用这个过程。为此,我做了以下工作:

创建了一个名为 CustomType 的类,它实现了 SQLData

public class CustomType implements SQLData {

String name ;
int year ;
String sql_type = "CustomType_t";

public String getSQLTypeName() throws SQLException {
return sql_type;
}

public void readSQL(SQLInput arg0, String type) throws SQLException {
sql_type = type;
name = arg0.readString() ;
year = arg0.readInt() ;

}


public void writeSQL(SQLOutput arg0) throws SQLException {
arg0.writeString(name) ;
arg0.writeInt(year) ;

}



}

我的主要 Java 类如下所示:

public class Proc2Test {


public static void main(String[] args) {
msmsqlTest() ;
}

public static void msmsqlTest()
{

Connection con = null ;
CallableStatement cStatement = null ;
ResultSet rs = null ;
String url = "jdbc:sqlserver://localhost:1433;databaseName=testDB;username=abc;password=xyz";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
try
{
Class.forName(driver);
con = DriverManager.getConnection(url);
java.util.Map map = con.getTypeMap();
map.put("CustomType",
Class.forName("com..test.CustomType"));


con.setTypeMap(map);



cStatement = con.prepareCall("{call abc2(?,?)}") ;

CustomType x = new CustomType() ;

x.name = "ABC" ;
x.year = 20 ;

cStatement.setObject("para1", x, java.sql.Types.OTHER);
cStatement.registerOutParameter(2,Types.INTEGER) ;
rs = cStatement.executeQuery();
}
catch(Exception ex)
{
ex.printStackTrace() ;
}


}
}

当我运行此代码时,出现以下异常:

com.microsoft.sqlserver.jdbc.SQLServerException: This operation is not supported.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.NotImplemented(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.setTypeMap(Unknown Source)
at com.mindcraft.test.Proc2Test.msmsqlTest(Proc2Test.java:97)
at com.mindcraft.test.Proc2Test.main(Proc2Test.java:22)

此异常发生在 con.setTypeMap(map)

我的问题是如何调用输入参数为自定义数据库类型的 SQL Server 存储过程?如何使用 SQL Server 2008 的自定义数据库类型映射 POJO 并将其发送到存储过程?SQL Server 2008 JDBC驱动有没有实现Connection.setTypeMap()方法?

4

0 回答 0