Solved

loading an image as a BLOB into MySQL using Java

Posted on 2006-07-10
6
685 Views
Last Modified: 2008-02-26
Hi,

I'm trying to load an image as a BLOB into a MySQL database. The database is set up fine, and working correctly. However, when I try to add images to the required table, the Java class throws the following error when I run it from a command line,

Exception in thread "main" java.lang.NoClassDefFoundError: DPToolTesting/LoadImage/java

I run this program with the following command from a terminal,

java DPToolTesting.LoadImage test.jpg 1 "Test Image"

The code for the Java class is as follows,

package DPToolTesting;

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

public class LoadImage
{
  public static void main( String[] args )
  {
    if( args.length < 3 )
    {
      System.out.println( "Usage: LoadImage <image-filename> <album-id> <name>" );
      System.exit( 0 );
    }

    String filename = args[ 0 ];
    int albumId = Integer.parseInt( args[ 1 ] );
    String name = args[ 2 ];
    Connection conn = null;
    PreparedStatement ps = null;
    try
    {
      Class.forName( "com.mysql.jdbc.Driver" );
      String url = "jdbc:mysql://localhost/signlists";
      String username = "root";
      String password = "";
      conn = DriverManager.getConnection( url, username, password );
      ps = conn.prepareStatement(
        "INSERT INTO Image (album_id, image_name, image_full) VALUES( ?, ?, ? )" );

      ps.setInt( 1, albumId );
      ps.setString( 2, name );

      // Insert the image into the second Blob
      File image = new File( filename );
      FileInputStream fis = new FileInputStream( image );
      ps.setBinaryStream( 3, fis, ( int )image.length() );

      // Execute the INSERT
      int count = ps.executeUpdate();
      System.out.println( "Rows inserted: " + count );
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if( ps != null ) ps.close();
        if( conn != null ) conn.close();
      }
      catch( Exception ee )
      {
        ee.printStackTrace();
      }
    }
  }
}

I've probably missed something obvious, but I can't seem to see where the problem might be.

Any help or advice would be appreciated.

Thanks
0
Comment
Question by:nhay59
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17073611
You need to be running from the parent directory of DPToolTesting, or it must be in the classpath

0
 

Author Comment

by:nhay59
ID: 17074206
Hi,

I've used 'cd' to change to the parent directory at the terminal, and run the same command. It is still showing the same error,

Exception in thread "main" java.lang.NoClassDefFoundError: DPToolTesting/LoadImage

I really can't see why this error is occurring just because I run it from the terminal.

Any help or advice appreciated.

Thanks
0
 

Author Comment

by:nhay59
ID: 17074439
Hi,

I've solved the above error by moving the file etc to the default home directory and compiling and running from there. I just need to solve the accompanying SQL errors now.

Thanks
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17074468
> I just need to solve the accompanying SQL errors now.
What errors are you getting?
0
 

Author Comment

by:nhay59
ID: 17074503
Hi,

Thanks for all the replies. I've now solved the SQL errors as well. Everything is working fine now.

Thanks to all for their help.












0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17076585
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question