java - 从 mysql 数据库检索图像时抛出异常

标签 java mysql image swing awt

我想以二进制格式存储图像并以二进制格式检索它并以图像格式显示它。我能够以二进制格式存储该文件,但在检索它时出现错误 java 空指针异常。错误之处请指出。这是代码:

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class InsertImageTest {
int len;
    /**
     * This is used to get the Connection
     * 
     * @return
     */
    public Connection getConnection() {
        Connection connection = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "spanwave");
        } catch (Exception e) {
            System.out.println("Error Occured While Getting the Connection: - "
                    + e);
        }
        return connection;
    }

    /**
     * Insert Image
     */
     public Image getImageFile(String fileName) throws Exception {
InsertImageTest ins= new InsertImageTest();
Connection con=ins.getConnection();
Statement stmt=con.createStatement();
          //  String baseName=StoreImage.getBaseName(fileName);
            ResultSet rs=stmt.executeQuery("select * from trn_imgs where img_title='"+"Honda Car"+"'");
            if (!rs.next()) {
              System.out.println("Image:"+"honda car"+" not found");
              return null;
            }
           // int len=rs.getInt(2);

            byte [] b=new byte[len];
            InputStream in = rs.getBinaryStream(3);
            int n=in.read(b);
            System.out.println("n: "+n);
            in.close();
            Image img=Toolkit.getDefaultToolkit().createImage(b);
            System.out.println("Image: "+"honda car"+" retrieved ok, size: "+len);
            return img;
          }
    public void insertImage() throws IOException {
        Connection connection = null;
        PreparedStatement statement = null;
        FileInputStream inputStream = null;

        try {
            File image = new File("calender.png");
            inputStream = new FileInputStream(image);
            len=inputStream.available();
            connection = getConnection();
            statement = connection
                    .prepareStatement("insert into trn_imgs(img_title, img_data) "
                            + "values(?,?)");
            statement.setString(1, "Honda Car");
            statement.setBinaryStream(2, (InputStream) inputStream,
                    (int) (image.length()));

            statement.executeUpdate();
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException: - " + e);
        } catch (SQLException e) {
            System.out.println("SQLException: - " + e);
        } finally {
            try {
                connection.close();
                statement.close();
            } catch (SQLException e) {
                System.out.println("SQLException Finally: - " + e);
            }
        }

    }

    /***
     * Execute Program
     * 
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        InsertImageTest imageTest = new InsertImageTest();
        imageTest.insertImage();
    Image img=  imageTest.getImageFile("calender.png");
    }

}

最佳答案

除非您的代码中缺少某些内容:

 Statement stmt = null;
      //  String baseName=StoreImage.getBaseName(fileName);
        ResultSet rs=stmt.executeQuery("select * from trn_imgs where

 //stmt is null, right?

关于java - 从 mysql 数据库检索图像时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15677758/

相关文章:

java - 将 session ID/ key 作为请求参数传递的安全风险

java - 无法上传 aws 设备场中的项目

java - 未添加观察者

java - 如何在 Java 的 JPA 查询中使用 IN 子句?

php - 如何通过单个查询检查多个 ID 是否存在

mysql - 如何在 Google App Engine 的免费帐户上设置 WordPress 网站

php - 左连接后无法在 for-each 循环中显示列

javascript - 如何制作圆 Angular 图像或圆形图像

c++ - Opencv - 如何合并两个图像

java - 通过 Canvas 缩放后 BufferedImage 变成全黑