Dynamic images In JasperReports (sqlite) - java

I have tried various different ways which i read from other questions but none of them seem working for me ...
I am using NetBeans 7.4 as front-end and Sqlite as Backend , iReport 5.5
I am making college ic-ards in which i want photo to be fetched from database ,In Database images are stored as blob , i am storing them as bytes..
My Requirement is getting student images from Database for ICard So i can print them..
Below is code i am using to insert image into Database
File image= new File(logPath2);
FileInputStream fis=new FileInputStream(image);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte buf[]=new byte[1024];
for(int readNum;(readNum=fis.read(buf))!=-1; ) {
bos.write(buf,0,readNum);
}
person_image=bos.toByteArray();
I have already tried various image expressions and class expressions, but none seem working ..
Please note i am talking about sqlite, i know ways to do it by mysql but sqlite, its not rendering. But i don't wanna shift from sqlite to mysql.
Error i get is
Error filling print... Unable to get value for field 'Photo' of class 'java.io.I‌​nputStream' net.sf.jasperreports.engine.JRException: Unable to get value for field 'Photo' o‌​f class 'java.io.InputStream' at net.sf.jasperreports.engine.JRResultSetDataSource.getFieldValue(JRResultS‌​etDataSource.java:319) at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset‌​.java:1356) at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:12‌​57) at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:12‌​33) –

Related

Android. How to store data for database correctly?

I am developing an app which after installation has to insert into database a big massive of data (about 5 Mb). But before to start inserting data, first app has to get these data somewhere. For that I created a class where I store all data into arrays. Below I give an example:
public class Data {
public static String[] table_1 = {"text1","text2","text3","text4"..."text500"};
public static String[] table_2 = {"text1","text2","text3","text4"..."text356"};
public static String[] table_3 = {"text1","text2","text3","text4"..."text485"};
..........................
public static String[] table_35 = {"text1","text2","text3","text4"..."text267"};
}
Than in onCreate(), app loops through all these arrays and inserts data into database. But here I encounter with problem called: error: code too large. I know that cause of problem is Data class because code is limited to 64K bytes. I thought to separate data between two classes, but I don't think its a good idea.
So, my question is how correctly to store data for database. What can you advise in this situation ?
you can load that content from a xml file, and once loaded do your insert statements.
You can use SQLite database .
https://developer.android.com/training/basics/data-storage/databases.html
two ways:
load that content from the internet and store it in your local SQLite database.
Create a File e.g. XML or Txt File with JSON string. Place it inside of your APK and load that file in your application and persist your text. But maybe it is not required to persist that text in your database because you can use your xml file as static data storage too.

java read encoded data from database and create pdf file

I am reading encoded data from database. When I decode data I get something like
%PDF-1.3
23 Obj
xref
10000000 123n
.
.
.
.
%EOF
I am guessing it's a metadata of PDF file with data. My question is how do I create PDF file out of this with readable data only.
Thanks in advance.
First you need to know what the data in DB represents. I would suggest to connect to the database using a general client. For example, for Oracle - SqlDeveloper, for MS Sql Server - Visual Sql Server, etc
Display the table which has a column of type long, clob or blob and try to save it to a file. Name the file with an extension .pdf. Try to open the saved file and see if it gets open correctly by a pdf reader.
If this is a case, saving it from Java is trivial. For example: http://www.astral-consultancy.co.uk/cgi-bin/hunbug/doco.cgi?11120

Sqlite Inserting More Than One Records into database

I need to insert about 700 records(name,id) into sqlite permanently ,because app will get user's name from the database.
I think ,reading text file is a solution but not know this is the best.
Can you show me other options to insert about 700 records into database?
thanks
The best practice to add multiple inserts into database shown in this video tutorial, you can watch it from 10.15
[Android Sqlite3 video tutoridal][inserting multiple values into database using fast way]
https://www.youtube.com/watch?v=dBnOn17pI7c&list=PLGLfVvz_LVvQUjiCc8lUT9aO0GsWA4uNe&index=14
U have Sqlite browser in order to view sqlite database.Insert data using the browser and u can permanently use that database.
Or try adding data to database using webservices.
It really depends on what you want to do and why you want to do it. That being said, text files can work. I had a similar case where I stored a few thousand items into an SQLite database. I used a text file and a CSVReader to parse the text file.
InputStream is = new ByteArrayInputStream(theContent.getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
CSVReader<String[]> csvReader = new CSVReaderBuilder<String[]>(br).strategy(new CSVStrategy('\t', '\b', '#', true, true)).entryParser(new EntryParser()).build();
while ((nextLine = csvReader.readNext()) != null) {
// Do Parsing work and Store to SQLite Database
}
If you know the data won't change and want the fastest solution, then a text file is sufficient. If the data will change frequently, then you're probably going to want to access a web service to update your data. The speed of this method will be affected by the internet speed of the user.

Blob information modified when retrieving image from DB using plain JDBC

I am working on a small POC (which will be integrated into a bigger application) which consists of
Problem Context
reading an image in a simple java application program,
Convert image into a byte array byte[] imageEncodedBytes = baos.toByteArray()
storing it into a remote DB2 database (technology used was not imp, so I am using plain jdbc for now)
Reading it back from the DB and
converting it back to ensure that the re-creation of the image works. ( I am able to open the new re-created image in any image viewer)
Issues
The issues occur at step 5.
I read the image using a select query into a Result Set.
use the rs.getBlob("ColumnName") to get the blob value.
Fetch the byte array from the blob value using byte[] decodedArray = myBlob.getBytes(1, (int)myBlob.length())
Create the image from the obtained byte array.
At Step 3 the byte array decodedArray obtained from the blob differs from the byte array 'imageEncodedBytes' that I get when I read the image.
As a consequence, the following code to create the image from the byte array decodedArray fails.
ByteArrayInputStream bais = new ByteArrayInputStream(decodedArray);
//Writing to image
BufferedImage imag=ImageIO.read(bais); // Line of failure. No registered provider able to read bais
ImageIO.write(imag, "jpg", new File(dirName,"snap.jpg"));
References and Other data for issue investigation
I have referred the following links for verification
1. Inserting image in DB2
2. This Link here offers insight, but yet I was not able to determine, how to register the ImageReader.
4. When inserting the image to DB2 I am using - the following query
Statement st = conn.createStatement();
st.executeUpdate("INSERT INTO PHOTO (ID,PHOTO_NM,PHOTO_IM, THMBNL_IM) " + "VALUES (1,'blob("+bl+")',blob('"+bl+"')")
As an alternative to fetching blob value from the result set I have also used binaryStream = rs.getBinaryStream("PHOTO_IM") to get the binary stream and then get byte array from the binary stream. even in this case, the decodedArray is different from imageEncodedBytes
Please assist, I may be missing something extremely trivial here, but I am not able to figure out what. Any help/pointers will be greatly helpful. Thanks in advance.
The resolution is sort of a workaround that I have worked on.
I have used the jdbcTemplates to resolve the issue. The lobHandler Object of the jdbc template provides an easy way to manage the blobs.
Steps to resolve using the Spring Lob Handler
) Created a Data Source
) configured the Jdbc template to use the data source
) use the lobHandler code to execute the insert query
Code below
jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("INSERT INTO PHOTO (PHOTO_IM) VALUES (?)",
new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
protected void setValues(PreparedStatement ps, LobCreator lobCreator) {
try {
lobCreator.setBlobAsBinaryStream(ps, 1, bean.getImageOrig(), bean.getImageLength());
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
}
);
references
1. ) Stack Overflow Link - Spring JDBC Template Insert Blob
2. ) Stack Overflow Link - Close InputStream
3. ) Spring Doc for LobHandler

Create Empty CloudBlockBlob in Azure

I'm hoping the answer to this question is quite simple, but I can't get it working after looking at the Azure Java API documentation.
I am trying to create an empty CloudBlockBlob, which will have blocks uploaded to it at a later point. I have successfully uploaded blocks before, when the blob is created upon the first block being uploaded, but I can't seem to get anything other than ("the specified blob does not exist") when I try to create a new blob without any data and then access it. I require this because in my service, a call is first made to create the new blob in Azure, and then later calls are used to upload blocks (at which point a check is made to see if the blob exists). Is it possible to create an empty blob in Azure, and upload data to it later? What have I missed?
I've not worked with Java SDK so I may be wrong but I tried creating an empty blob using C# code (storage client library 2.0) and if I upload an empty input stream an empty blob with zero byte size is created. I did something like the following:
CloudBlockBlob emptyBlob = blobContainer.GetBlockBlobReference("emptyblob.txt");
using (MemoryStream ms = new MemoryStream())
{
emptyBlob.UploadFromStream(ms);//Empty memory stream. Will create an empty blob.
}
I did look at Azure SDK for Java source code on Github here: https://github.com/WindowsAzure/azure-sdk-for-java/blob/master/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/client/CloudBlockBlob.java and found this "upload" function where you can specify an input stream. Try it out and see if it works for you.

Categories

Resources