【问题标题】:Exception thrown while retrieving image from mysql database从 mysql 数据库中检索图像时引发异常
【发布时间】:2013-03-18 15:36:14
【问题描述】:

我想以二进制格式存储图像并以二进制格式检索它并以图像格式显示它。我能够以二进制格式存储文件,但是在检索它时出现错误 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");
    }

}

【问题讨论】:

  • 从哪里获得 NPE?堆栈跟踪说明了什么?
  • 你需要学习如何调试你的代码,识别哪些行导致空指针异常
  • @gerrytan 好评的附录:learn to read stack traces。恕我直言,这是 Java 开发的核​​心技能之一。

标签: java mysql image swing awt


【解决方案1】:

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

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

 //stmt is null, right?

【讨论】:

    【解决方案2】:
    connection = getConnection();
     Statement stmt = connection.createStatement();
     ResultSet rs=stmt.executeQuery("select * from trn_imgs where img_title='"+"Honda Car"+"'");
    

    【讨论】:

    • 我需要更多帮助,现在我可以运行但如何显示图像。请告诉我
    • stackoverflow.com/a/15599684/1743852:我之前写过这个答案,所以试试吧,我希望它可以帮助你,如果你遇到任何问题,请告诉我:)
    • 在我的项目中查看我必须获取包括图像在内的用户数据,并且我必须在 jsp 页面中显示它。例如,如果用户 u1 resgiters 一次,那么当他使用他的用户 ID 和密码登录时她的详细信息和他的照片应该显示在主页上。我看到了你的答案,你正在使用摇摆。告诉我如何在jsp页面中获取它
    • 我不知道jsp怎么用,但是我搜索时发现这个代码:<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <img alt="logo" src="<html:rewrite page='/resources/images/header.jpg'/>" width="100%" height="20%"/>,所以你需要图像的路径,所以我建议你需要写图像,看这个链接:stackoverflow.com/a/15511055/1743852
    • @javaL - 随时欢迎您,如果出现任何问题,请随时问我 :)
    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 2014-02-19
    • 2015-07-06
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多