Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将图像插入数据库_Java_Jdbc - Fatal编程技术网

Java 将图像插入数据库

Java 将图像插入数据库,java,jdbc,Java,Jdbc,将图像插入数据库显示错误。这是我尝试过的代码 package com.mysql.db.examples; import java.io.*; import java.sql.*; public class BlobInsertTest { public static void main(String a[]){ Connection con = null; PreparedStatement ps = null; InputStream is = nu

将图像插入数据库显示错误。这是我尝试过的代码

package com.mysql.db.examples;

import java.io.*;
import java.sql.*;

 public class BlobInsertTest {

    public static void main(String a[]){

    Connection con = null;
    PreparedStatement ps = null;
    InputStream is = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.
                getConnection("jdbc:mysql://localhost:3306/STUDENT_DB","root","sys");
        ps = con.prepareStatement("insert into STUDENT_PROFILE values (?,?)");
        ps.setInt(1, 101);
        is = new FileInputStream(new File("D:\\supriyo_pic.JPG"));
        ps.setBinaryStream(2, is);
        ps.executeUpdate();

    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally{
        try{
            if(is != null) is.close();
            if(ps != null) ps.close();
            if(con != null) con.close();
        } catch(Exception ex){}
    }
 }
}
它的显示:

Exception in thread "main" java.lang.AbstractMethodError: 
  com.mysql.jdbc.ServerPreparedStatement.setBinaryStream(ILjava/io/InputStream;)V 
  at com.mysql.db.examples.BlobInsertTest.main(BlobInsertTest.java:26)

根据MySQL JDBC驱动程序的版本(是JDBC 3还是JDBC 4类型?),您需要为
setBinaryStream
方法设置不同的参数

详细信息可在此处找到:

因此,您应该使用以下方法进行测试:

...
File file = new File("D:\\supriyo_pic.JPG");
is = new FileInputStream(file);
ps.setBinaryStream(2, is, (int)file.length());
...

存储图像路径而不是图像..如果在setBinaryStream方法中还提供第三个可选参数,会发生什么情况<代码>is=newfileinputstream(新文件(“D:\\supriyo_pic.JPG”),(int)ps.length()我听说如果不提供第三个参数,某些jdbc驱动程序可能会出现问题。它是用于(int)ps.length()的吗??我找不到。。。