2013-03-28 3 views
2

이진 형식으로 이미지를 저장하고 바이너리 형식으로 검색하여 이미지 형식으로 표시하려고합니다. 이진 형식으로 파일을 저장할 수 있지만 검색하는 동안 오류 자바 null 포인터 예외가 발생합니다. 오류를 지적하십시오.mysql 데이터베이스에서 이미지를 검색하는 동안 예외가 발생했습니다.

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"); 
    } 

} 
+0

당신이 NPE를받을 수 있나요 여기 코드는? 스택 트레이스는 무엇을 말합니까? – creinig

+1

코드를 디버그하는 방법, null 포인터 예외가 발생하는 행을 식별하는 방법을 알아야합니다. – gerrytan

+1

@ gerrytan의 좋은 설명에 대한 부록 추가 정보 : [스택 추적을 읽는 법을 배우십시오] (http://stackoverflow.com/questions/12688068/how - 읽기 및 이해 - 자바 - 스택 - 추적). IMHO는 자바 개발의 핵심 기술 중 하나입니다. – creinig

답변

2
connection = getConnection(); 
Statement stmt = connection.createStatement(); 
ResultSet rs=stmt.executeQuery("select * from trn_imgs where img_title='"+"Honda Car"+"'"); 
+1

답변을 주셔서 감사합니다 –

+0

당신은 환영합니다 :) –

+0

나는 한 번 더 도움이 필요합니다. 이제 실행할 수는 있지만 image.please를 표시하는 방법은 –

3

뭔가 코드에서 누락되지 않는 :

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

//stmt is null, right? 
관련 문제