Jump to content

JavaEE Byte[] from database - need help

 

I'm trying to display an image that's stored in the database as byte[ ]. I need to retrieve and display it on a java faces page. here is what I have so far.

 

all its showing in the column in just blank.

 

 

Table column from the XHTML page

<b:dataTableColumn>
  <f:facet name="header">
    Image
  </f:facet>	
  <o:graphicImage value="#{imageBean.getBytes(category.categoryID)}" />
</b:dataTableColumn>

controller to get populate the column

@Named
@ApplicationScoped
public class imageBean 
{
	@Inject
    private CategoryService service;

    public byte[] getBytes(int categoryID) 
    {
        return service.getImageById(categoryID);
    }
}

and the Service Class

@Stateful
public class CategoryService 
{
........
.....
....
public byte[] getImageById(int categoryID)
	{
		//BufferedImage img = null;	
		String queryString = "SELECT c FROM Category WHERE c.categoryID =" + categoryID;
		return (byte[]) entityManager.createQuery(queryString).getSingleResult();	<<<<<<<<<< this is where im having problem....	i think	
		 
	}
}

how do  i correctly get the Byte[ ] out of the database? 

Link to comment
Share on other sites

Link to post
Share on other sites

what framework are you using that is storing images in a database?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, SCHISCHKA said:

what framework are you using that is storing images in a database?

sorry my friend, this is for a school project and all i know is that information that i have already given.

 

database was given to us all set up to use, i know its running wildfly server, but not sure whats the database is.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, amintha1992 said:

sorry my friend, this is for a school project and all i know is that information that i have already given.

 

database was given to us all set up to use, i know its running wildfly server, but not sure whats the database is.

i can't test your code. is it failing on the database query or are you having trouble turning the database result to a byte array?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SCHISCHKA said:

i can't test your code. is it failing on the database query or are you having trouble turning the database result to a byte array?

i did some more testing and have narrowed down the problem,

 

the ImageBean and the getImageById is only reached sometimes, when the page is loaded, so that is a problem.

 

and i dont know how to get an array of Bytes out of a data base in java.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, amintha1992 said:

i did some more testing and have narrowed down the problem,

 

the ImageBean and the getImageById is only reached sometimes, when the page is loaded, so that is a problem.

 

and i dont know how to get an array of Bytes out of a data base in java.

getSingleResult() returns java.lang.Object which you can cast to byte[]. I dont see anything wrong with that line. What kind of fail message are you getting?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, SCHISCHKA said:

getSingleResult() returns java.lang.Object which you can cast to byte[]. I dont see anything wrong with that line. What kind of fail message are you getting?

 

not an error, but all im getting from that query is this,(i got it from a prinnt line)

[B@c8a7334] or a slightly different one for all the other records in the table

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, amintha1992 said:

not an error, but all im getting from that query is this,(i got it from a prinnt line)

[B@c8a7334] or a slightly different one for all the other records in the table

I would expect that output from printing an array, the length of the array would be more interesting but i think you are receiving

 

1 hour ago, amintha1992 said:

the ImageBean and the getImageById is only reached sometimes, when the page is loaded, so that is a problem.

 

i think your problem is the XHTML not the java. what happens when you put this on its own without the table?

  <o:graphicImage value="#{imageBean.getBytes(category.categoryID)}" />
 

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, SCHISCHKA said:

I would expect that output from printing an array, the length of the array would be more interesting but i think you are receiving

 

i think your problem is the XHTML not the java. what happens when you put this on its own without the table?


  <o:graphicImage value="#{imageBean.getBytes(category.categoryID)}" />
 
 

yes i tried that, and all its showing is the same part that the print line showed, no picture. i also did some changes to the getImageById in the service class.

here is the code

public byte[] getImageById(int categoryID)
	{
		Category category =  entityManager.find(Category.class, categoryID);
		
		byte[] img = category.getPicture();
		System.out.println(img);
			
		return img;
		
	}

it seems to work just fine and now the only problem is how to get the full byte[ ] and showing it as an image

 thanks for help

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, amintha1992 said:

yes i tried that, and all its showing is the same part that the print line showed, no picture. i also did some changes to the getImageById in the service class.

here is the code


public byte[] getImageById(int categoryID)
	{
		Category category =  entityManager.find(Category.class, categoryID);
		
		byte[] img = category.getPicture();
		System.out.println(img);
			
		return img;
		
	}

it seems to work just fine and now the only problem is how to get the full byte[ ] and showing it as an image

 thanks for help

 

anyway, got to go to bed now. have to go to work in the morning. please leave anything you can to help me out. thank you so much.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, amintha1992 said:

yes i tried that, and all its showing is the same part that the print line showed, no picture. i also did some changes to the getImageById in the service class.

here is the code


public byte[] getImageById(int categoryID)
	{
		Category category =  entityManager.find(Category.class, categoryID);
		
		byte[] img = category.getPicture();
		System.out.println(img);
			
		return img;
		
	}

it seems to work just fine and now the only problem is how to get the full byte[ ] and showing it as an image

 thanks for help

I'm thinking streams and serialisation. ask your teacher how the image is stored because using the byte array of the result isn't working

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, SCHISCHKA said:

I'm thinking streams and serialisation. ask your teacher how the image is stored because using the byte array of the result isn't working

the image is stored as a BLOB on the DB.

 

if only i can get that BLOB from the DB out as a binaryStream then it can be converted in to a byte[] and displayed on the jsf page.

 

where im stuck is getting that binarystream from the DB for the image.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, amintha1992 said:

the image is stored as a BLOB on the DB.

 

if only i can get that BLOB from the DB out as a binaryStream then it can be converted in to a byte[] and displayed on the jsf page.

 

where im stuck is getting that binarystream from the DB for the image.

you can't do a .getBytes() on the returned object?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, SCHISCHKA said:

you can't do a .getBytes() on the returned object?

Didnt try that. ill check.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×