Solved

loading an image as a BLOB into MySQL using Java

Posted on 2006-07-10
6
686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 24
by zero exception 10 70
Eclipse for Java EE development 2 43
Running JavaFX on the Raspberry Pi 27 133
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

749 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