Saving OLE objects into an Access (mdb) database using Java

How do I do this. Do I really have to use the option "DirectlyFromCachedRowset" in the command "INSERT INTO"? If so, how do I know the size.

I want to save my own created binary data-files. Th only information I got on the file is the name with path.

Thanks!

/Christer
ChristerHAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
you can read binary data from file and write it inside a BLOB.
0
ChristerHAuthor Commented:
Quick again ... 8-) Do you know how? I tried to send the filename ... but no ... perhaps problems with slashes ... \/\/ ..
0
heyhey_Commented:
quick - no - just checking my e-mail while drinking my (noon) coffee.

try this - if it does not work - pleas post your code + full exception stacktrace.

//  ....

  PreparedStatement pstmt = con.prepareStatement ("UPDATE myTable SET binaryData = ? WHERE code = '" + code + "'");
 
  File f = new File(fileName);
  int len = f.length();
  FileInputStream fis = new FileInputStream(f);
  stmt.setBinaryStream(1, fis, len);  
 
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

ChristerHAuthor Commented:
This seems to be working for writing ...

File f = new File(filename);
int  len = (int) f.length();

try {
    FileInputStream fis = new FileInputStream(f);
    pstmt.setBinaryStream(2, (InputStream)fis, (int) len);  
}
catch (FileNotFoundException fe)
{
    System.out.println(fe.toString());
}

But I don't know ... how do I read it back from the DB?
0
heyhey_Commented:
try something like

ResultSet res = stmt.executeQuery("SELECT data from myTable WHERE code = '" + code + "'");
if (!res.next()) return null;

// you can get directly bytes (for small BLOBs)
byte[] bytes = res.getBytes("data");

// or you can use InputStream
InputStream is = res.getInputStream();


// if you want to save the data back to file, you have to keep the file name etc ...
0
ChristerHAuthor Commented:
I tried this:

*** STORING DATA ***

String sStatement = "INSERT INTO TABLE (BLOB)  values (?)";
(rest is two comments up)

*** READING DATA ***

ResultSet rs=null;
rs= stmt.executeQuery("SELECT * FROM TABLE");
if (!res.next()) return null;
InputStream is = rs.getBinaryStream("BLOB");

int i;
ole = is.available(); // throws an blank exception
ole = is.read(); // get the value -1

Hints?
0
heyhey_Commented:
1. try column number isntead of column name
2. try
byte[] = rs.getBytes();
0
heyhey_Commented:
and what's that 'blank exception' ?
0
ChristerHAuthor Commented:
1. byte[] bytes = rs.getBytes(2); // becomes null ...

2. A blank exeption is when this rows just gives the output:
    "IOException: "

    catch (IOException e){
        System.out.println("IOException: "+e.toString());
    }
0
heyhey_Commented:
are you sure that you have successfully uploaded the BLOB data (COMMITED the transaction / whatever)? can you check this with some other tools? it seems to me that the BLOB field contains NULL.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ChristerHAuthor Commented:
I just made a copy of the basebase before adding a 124928 byte BLOB and the size increased from 245760 to 362796 ...
0
ChristerHAuthor Commented:

This works! Thanks alot!

/Christer

public static void load()
{
    Connection con = null;
    Statement stmt;
    ResultSet rs = null;
       
    byte [] b2 = new byte [1000];
       
    try {
        Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");

        // connect to database
        con = DriverManager.getConnection("jdbc:odbc:prtt");
        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM SAMPLE");
           
        int no=0;
        // go thru resultset record by record
        while (rs.next()) {
            String size = rs.getObject( "SIZE").toString();
               
            InputStream is = rs.getBinaryStream("BLOB");
               
            File fout;
            FileOutputStream fos = null;

            fout = new File("MyOutFile.dat");
            try {
                fos = new FileOutputStream(fout);
                   
                int bytesRead = is.read(b2);
                while (bytesRead >= 0)
                {
                    fos.write(b2,0,bytesRead);
                    bytesRead = is.read(b2);
                }
            } finally {
               fos.close();
            }
        }
    }
       
    // handle sql exception
    catch (SQLException e){
        System.out.println("SQLException: Can not connect to database !"+e.toString());
    }
    // handle IO exception
    catch (IOException e){
        System.out.println("IOException: '"+e.toString()+"'");
    }
    // class not found exception
    catch (ClassNotFoundException e){
        System.out.println("ClassNotFoundException: "+e.toString());
    }
}    
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.