Solved

loading an image as a BLOB into MySQL using Java

Posted on 2006-07-10
6
684 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

831 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