Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

loading an image as a BLOB into MySQL using Java

Posted on 2006-07-10
6
Medium Priority
?
694 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

618 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