Solved

loading an image as a BLOB into MySQL using Java

Posted on 2006-07-10
6
682 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
Comment Utility
You need to be running from the parent directory of DPToolTesting, or it must be in the classpath

0
 

Author Comment

by:nhay59
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 23

Expert Comment

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

Author Comment

by:nhay59
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now