This week's book giveaway is in the Design forum.
We're giving away four copies of Mastering Non-Functional Requirements and have Sameer Paradkar on-line!
See this thread for details.
Win a copy of Mastering Non-Functional Requirements this week in the Design forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JDBC and mySQL : Error Getting a BLOB object to my database  RSS feed

 
VarunS Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting following error when I run this program:
Exception in thread "main" java.io.FileNotFoundException: sample_resume.pdf (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at WriteBlobDemo1.main(WriteBlobDemo1.java:15)
C:\Users\Varun\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)


What is the problem in this code?please suggest
FullScreenshot.jpg
[Thumbnail for FullScreenshot.jpg]
trying to get a BLOB object to mysl datbase, have a look
 
Claude Moore
Ranch Hand
Posts: 865
8
IBM DB2 Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's difficult to say, without being able to read your code. Can't you post the source code - or, at least, a part of it - instead of a picture ? It' grainy and I can't read anything....
Anyway the stacktrace you posted seems to point out that the problem is that your program isn't able to read file sample_resume.pdf.
 
Paul Clapham
Sheriff
Posts: 22215
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the file doesn't exist at the location you specified. Presumably it's not in your current working directory (which is what your code specifies) but somewhere else. You should provide the full path to the file.
 
VarunS Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

public class WriteBlobDemo1 {
public static void main(String[] args)throws Exception{
             FileInputStream input = null;
             String sql = "update myemployees set resume=? where email='john.doe@foo.com'";
             Connection mycon = null;
             PreparedStatement stmt = null;
        try{
        Class.forName("com.mysql.jdbc.Driver");
        mycon = DriverManager.getConnection("jdbc:mysql://localhost:3306/varun_blob_clob","root","tiger");
      
        stmt = mycon.prepareStatement(sql);
       
        File myFile = new File("sample_resume.pdf");
        input = new FileInputStream(myFile);
        stmt.setBinaryStream(1, input);
        System.out.println("Reading input stream:- " +myFile.getAbsolutePath());
       
        stmt.executeUpdate(sql);
       
        System.out.println("Storing the file in database: " +myFile);
        System.out.println(sql);
      
        System.out.println("Successfully completed");
       
        mycon.close();}catch(Exception exc){System.err.println(exc+"Error");}
      
    }
   
}
 
VarunS Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah actually by giving full path of the File , it gor stored in the database.
But why do I have to give the full path name if i have used the method getAbsolutePath()?
 
Paul Clapham
Sheriff
Posts: 22215
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you look at the result of getAbsolutePath? You seem to expect it to magically find the file no matter where in the file system it actually is. But it doesn't work that way. If you use a relative path (as you did) then it looks in your current working directory. It's quite likely that when you run that code via Netbeans, you don't know what the current working directory is. Looking at the result of getAbsolutePath will tell you that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!