2013-08-22 81 views
0

我正在將一些jdbc代碼從MySql轉換爲SQL Server。嘗試異常:從UNKNOWN到UNKNOWN的轉換不受支持

 query = "Update ReportSetup " 
        + "set N_ID=?, " 
        + "R_Default=?, " 
        + "R_Name=?, " 
        + "R_Module=? " 
        + " where R_ID = ?"; 
     } 

     PreparedStatement stmt = 
       (PreparedStatement) con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); 
     stmt.setInt(1, ri.getNodeID()); 
     stmt.setInt(2, (ri.isDefault()) ? 1 : 0); 
     stmt.setString(3, ri.getName()); 
     Object o = ri.getReportModule(); 
     stmt.setObject(4, o); 

最後一條語句stmt.setObject(4,o)引發異常。

ri.getReportModule returns an instance of a class which implements Externalizable. 

該類的方法的writeExternal()被實現爲

public final void writeExternal(final ObjectOutput objectOutput) throws IOException { 
    for (int i=0; i<pdV.size(); i++) { 
     PropertyDescriptor pd = pdV.elementAt(i); 
     try { 
      Method m = pd.getReadMethod(); 
      Object val = pd.getReadMethod().invoke(this); 
      System.out.print("writing property " + i + ": " + pd.getName() + " = " + val); 
      objectOutput.writeObject(val); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

所討論的數據庫列定義爲 VARBINARY(最大),而不是空

代碼工作以及使用MySql,但我無法弄清楚如何使它與Sql Server一起運行。

任何建議將非常讚賞

回答

2

的問題是,SQL Server的不開心保存序列化(如實施時外部化完成)。 .setObject()失敗。解決方案是使用setBinaryStream()。

 // Sql Server can't do an stmt.setObject(4,o) "Conversion from UNKNOWN to UNKNOWN not supported" 
     // Serialize the object to an output stream and then read it in again into the stmt. 
     Object o = ri.getReportModule(); 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     ObjectOutputStream objectOutput = new ObjectOutputStream(bos); 
     objectOutput.writeObject(o); 
     objectOutput.flush(); 
     InputStream objectInput = new ByteArrayInputStream(bos.toByteArray()); 
     stmt.setBinaryStream(4, objectInput); 

乾杯 基督教

相關問題