0

Code:

package com;

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

public class Sample {

public static void main(String a[]){
    Connection con = null;
    PreparedStatement ps = null;
    InputStream is = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.getConnection("jdbc:oracle:thin:@172.26.132.40:1521:orclilp","aja60core","aja60core");
        ps = con.prepareCall("insert into SAMPLE values (?,?,?)");
        ps.setString(1, "Himanshu");
        ps.setString(2, "Gupta");
        is = new FileInputStream(new File("ajax-logo1.jpg"));
        ps.setBinaryStream(3, is);
        int count = ps.executeUpdate();
        System.out.println("Count: "+count);
    } 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){}
    }
}
}

And I'm getting the following error:

java.io.FileNotFoundException: ajax-logo1.jpg (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.Sample.main(Sample.java:19)
Roberto Anić Banić
  • 1,411
  • 10
  • 21
  • 1
    Duplicate of http://stackoverflow.com/questions/19307622/java-new-file-says-filenotfoundexception-but-file-exists – SatyaTNV Oct 14 '15 at 09:08

3 Answers3

0

The FileNotFoundException exception occurs when a file is not found by the java interpreter

Signals that an attempt to open the file denoted by a specified pathname has failed.

This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.

Make sure the file exists in your application (where the .class files are and not where the .java source files are)

Thats a common mistake

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
0

Keep the "ajax-logo1.jpg" file in the same directory where Sample.java is located (not recommended) or provide actual/relative path of the "ajax-logo1.jpg" file in the File constructor.

2787184
  • 3,749
  • 10
  • 47
  • 81
  • 1
    Not true, unless `Sample.class` is in the same directory than `Sample.java`, which shouldn't. – Albert Oct 14 '15 at 09:29
0

When you simply write the name of the file (as you have done in your code example), Java will look for the file in the root directory of your project i.e. if your project files are in the directory databaseProject and the directory structure looks like this:

`databaseProject
|->src
|->pom.xml (for maven builds)
... `

etc. The java program, by default, will consider your working directory to be databaseProject and look for files it in, unless you give an absolute or relative (from your pwd) path to your files.

A good approach is to place all the images in a folder e.g.

`databaseProject
|->src
|->images
   |->ajax-logo1.jpg
   |-> ....
|->pom.xml (for maven builds)
... `

and then use the path: "/images/<image_name>" to access the images.

mbbce
  • 2,245
  • 1
  • 19
  • 31
  • what is not working, putting the file in a separate directory? or placing it in the root directory of the project? – mbbce Oct 14 '15 at 15:31