使用BLOB对象将图像插入SQL表中的异常

时间:2013-02-21 11:15:51

标签: java swing io inputstream fileinputstream

我在以下代码中获得了异常

PreparedStatement pstmt;
Connection conn = DriverManager.getConnection(
   "jdbc:jtds:sqlserver://Irfan:1433/Pictures", "sa", "ent@");      

java.sql.Blob bl=GetBolbDataFromDB();
InputStream fs = bl.getBinaryStream();
len = bl.length() ;

query = ("insert into NewPics2 VALUES(?,?,?,?)");
pstmt = conn.prepareStatement(query);
pstmt.setString(1, model);
pstmt.setString(2, catid);
pstmt.setString(3, ornid);
**pstmt.setBinaryStream(4, fs);**

完整的堆栈跟踪如下

Exception in thread "AWT-EventQueue-0" java.lang.AbstractMethodError: net.sourceforge.jtds.jdbc.JtdsPreparedStatement.setBinaryStream(ILjava/io/InputStream;)V
at jewelleryerpapplication.GUI.Orders.CheckPrice.GetPicInfo(CheckPrice.java:1157)
at jewelleryerpapplication.GUI.Orders.CheckPrice.txtModelNoKeyPressed(CheckPrice.java:2300)
at jewelleryerpapplication.GUI.Orders.CheckPrice.access$1500(CheckPrice.java:37)
at jewelleryerpapplication.GUI.Orders.CheckPrice$18.keyPressed(CheckPrice.java:702)
at java.awt.Component.processKeyEvent(Component.java:6463)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
at java.awt.Component.processEvent(Component.java:6282)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1908)

3 个答案:

答案 0 :(得分:1)

简单地说你的jdbc驱动程序没有实现最新版本的jdbc:它没有方法setBinaryStream()。这意味着你不能使用它。您可以使用完全读取的图像尝试setBlob()。

答案 1 :(得分:1)

从堆栈跟踪中,您调用的方法似乎是抽象的。根据文档,JtdsPreparedStatement.setBinaryStream()有三个参数:索引,流和长度。

尝试在int

中添加pstmt.setBinaryStream(4, fs);作为长度参数

答案 2 :(得分:0)

尝试转换

java.sql.Blob bl=GetBlobDataFromDB();
String fs = bl.getBytes();