import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.sql.*;

public class RefHistTest
{
public static void main(String args[])
{
int iw2, ih2;
Image img2;
int pixels2[];
double[] hist2 = new double[256];
 int y;
ResultSet r;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:CBIRS");
Statement st=c.createStatement();
r=st.executeQuery("select Images from ImageTable");
byte[] bytes=null;
while(r.next())
{
//getting pixels
bytes = r.getBytes(2);
Toolkit toolkit = Toolkit.getDefaultToolkit();
img2 = toolkit.createImage(bytes);
iw2 = img2.getWidth(null);
ih2 = img2.getHeight(null);
pixels2 = new int[iw2*ih2];
PixelGrabber pg  = new PixelGrabber(img2,0,0,iw2,ih2,pixels2,0,iw2);
pg.grabPixels();

//calculating hist array
for (int i=0; i<iw2*ih2; i++) 
{
int p2 = pixels2[i];
int r2 = 0xff & (p2 >> 16);
int g2 = 0xff & (p2 >> 8);
int b2 = 0xff & (p2);
y = (int) (.33 * r2 + .56 * g2+ .11 * b2);
hist2[y]++;
}
for(int i=1; i<256; i++)
{
System.out.println("histogram:" + hist2[i] );
}
}
}

catch(SQLException e)
{
System.out.println("SQL error");
return;
}
catch(InterruptedException e)
{
System.out.println("interrupted");
return;
}
catch(ClassNotFoundException e)
{
System.out.println("class not found");
return;
}
}
}

i am getting SQL error: file not found
plz tell me where's the mistake

Add e.printStackTrace(); in your catch blocks to show the detailed stack trace for your exceptions.

yea not enough info need to track the line throwing the exception and exactly what is the stack trace. but i did find this code

String url = "jdbc:derby:Fred";
Connection con = DriverManager.getConnection(url, "Fernanda", "J8");

this is getConnection using a log in name of Fernanda and a password of J8. Does your database you set up have no name and pass on it? If it does you would need to include them.

Mike

Add e.printStackTrace(); in your catch blocks to show the detailed stack trace for your exceptions.

now i m getting
java.sql.SQLException:[Microsoft][ODBC Driver Manager]Cannot find fiie

Probably the line failing is

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

or it's just not working and connect fails because no driver really loaded.

I still think http://www.daniweb.com/forums/thread148117.html is worth looking at.

It sounds like it's not finding your sql programs driver.

[edit] you can insert print lines between code to track what line it failed out , i.e which print line it doesn't reach and you don't see your text print for that line.

Mike

Probably the line failing is

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

or it's just not working and connect fails because no driver really loaded.

I still think http://www.daniweb.com/forums/thread148117.html is worth looking at.

It sounds like it's not finding your sql programs driver.

[edit] you can insert print lines between code to track what line it failed out , i.e which print line it doesn't reach and you don't see your text print for that line.

Mike

driver is loaded and i have made other programs where connectivity is working fine
but its not working for retriving images from database
is there a possibility that i made mistake while storing images in database??
if yes please give me the method to store images.
i am using access2007

Do you have a DSN entry called "CBIRS"?

Do you have a DSN entry called "CBIRS"?

yes i have
now i am trying FileInputStream() and FileOutputStream() to access images
but in that case i am having errors in getWidth(),getHeight(),PixelGrabber and i need to use these in my program
so is there any alternative?

Are you running this on a 64-bit machine with 32-bit MS Access installed?

Are you running this on a 64-bit machine with 32-bit MS Access installed?

i have solved the privious problems
now i am getting no exceptions but the program is displaying all zero values.
i think the it is not able to recreate the image
can u plz check my code

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.sql.*;

public class RefHistTest
{
public static void main(String args[])
{
int iw2, ih2;
Image img2;
int pixels2[];
double[] hist2 = new double[256];
 int y;
ResultSet r;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:ImageData");
Statement st=c.createStatement();
r=st.executeQuery("select Images from ImageOLE");
byte[] bytes=null;
while(r.next())
{
//getting pixels
bytes = r.getBytes("Images");
Toolkit toolkit = Toolkit.getDefaultToolkit();
img2 = toolkit.createImage(bytes);
iw2 = img2.getWidth(null);
ih2 = img2.getHeight(null);
pixels2 = new int[iw2*ih2];
PixelGrabber pg  = new PixelGrabber(img2,0,0,iw2,ih2,pixels2,0,iw2);
pg.grabPixels();

//calculating hist array
for (int i=0; i<iw2*ih2; i++) 
{
int p2 = pixels2[i];
int r2 = 0xff & (p2 >> 16);
int g2 = 0xff & (p2 >> 8);
int b2 = 0xff & (p2);
y = (int) (.33 * r2 + .56 * g2+ .11 * b2);
hist2[y]++;
}
for(int i=1;i<256;i++)
{
System.out.println("value" +hist2[i]);
}
}
r.close();
st.close();
c.close();
}

catch(SQLException e)
{
System.out.println("sql error");
System.out.println(e.getMessage());
System.out.println("Error Code:"+e.getErrorCode());
System.out.println("SQL State:"+e.getSQLState());
e.printStackTrace();
return;
}
catch(InterruptedException e)
{
System.out.println("interrupted");
return;
}
catch(ClassNotFoundException e)
{
System.out.println("class not found");
return;
}
}
}[LIST=1]
[/LIST]

Are you running this on a 64-bit machine with 32-bit MS Access installed?

no i have a 32bit system

Are you running this on a 64-bit machine with 32-bit MS Access installed?

import java.io.*;
import java.sql.*;
import java.util.Properties;
import java.awt.*;
import javax.swing.*;

public class ImageDemo
{
public static void main(String args[])
{
try
{

String url="jdbc:odbc:ImageData";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection(url);
Statement stmt = connection.createStatement();
File file=new File("c:\\bg.jpg");
FileInputStream in=new FileInputStream(file);

PreparedStatement ps=connection.prepareStatement("insert into ImageOLE (ID, Images) values

(?,?);");
ps.setInt(1,1234);
ps.setBinaryStream(2,in,(int)file.length());
ps.execute();
ps.close();
}

catch( SQLException sqle )
{
System.out.println(sqle.getMessage());
System.out.println("Error Code:"+sqle.getErrorCode());
System.out.println("SQL State:"+sqle.getSQLState());
sqle.printStackTrace();
}
catch( Exception e )
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

i am recieving SQLException:General error
error code:0
sql state:s1000

i am not getting it????

Which line is the error occurring? Getting the connection or executing the statement?

Which line is the error occurring? Getting the connection or executing the statement?

that error is rectified
now how can i store different images in different rows

Reset your statement parameters and execute the statement - just like inserting any data into a table.

Reset your statement parameters and execute the statement - just like inserting any data into a table.

its not working for me nothing is getting inserted after 1st row
can u give me an example?

Reset your statement parameters and execute the statement - just like inserting any data into a table.

import java.io.*;
import java.sql.*;
import java.util.Properties;
import java.awt.*;
import javax.swing.*;

public class ImageDemo
{
public static void main(String args[])
{
try
{

String url="jdbc:odbc:ImageData";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection(url);

File file=new File("c:\\jobs.jpg");
FileInputStream in=new FileInputStream(file);
PreparedStatement ps=connection.prepareStatement("insert into ImageOLE values(?,?)");
ps.setInt(1,1235);
ps.setBinaryStream(2,(InputStream)in,(int)(file.length()));
ps.executeUpdate();
ps.close();
}

catch( SQLException sqle )
{
System.out.println(sqle.getMessage());
System.out.println("Error Code:"+sqle.getErrorCode());
System.out.println("SQL State:"+sqle.getSQLState());
sqle.printStackTrace();
}
catch( Exception e )
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

i an getting no error or exception but its not doing anything

its not working for me nothing is getting inserted after 1st row
can u give me an example?

ok i got my mistake
i didn't write connection.close();

now my problem is entirely solved thanks Ezzaral for being so patient and thanks Daniweb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.