gpt4 book ai didi

java - 将图像插入数据库

转载 作者:行者123 更新时间:2023-12-01 13:47:31 25 4
gpt4 key购买 nike

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

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 或 4 类型?),您需要为 setBinaryStream 方法设置不同的参数。

详细信息可以在这里找到:http://www.herongyang.com/JDBC/MySQL-BLOB-setBinaryStream.html

所以,你应该用这个来测试:

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

关于java - 将图像插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20252968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com