hi
can anybody send me sample progrm in java for how to insert and retrive image to and from mysql database..
thanks..
byee
Printable View
hi
can anybody send me sample progrm in java for how to insert and retrive image to and from mysql database..
thanks..
byee
Use BLOB datatype and in Db you can use Image Datatype
Yes Binary Large Object is the solution.
Code:Statement stm; // You have to implement the statement to your connection
ResultSet rs = stm.executeQuery(sql_query);
if(rs.next()) {
Blob blob = rs.getBlob(column);
}
Hey I have done this before, so I dug this out of one of the classes I wrote.
Apologies if its a little long winded or whatever, I wrote it over 2 years ago, but it is working within a production environment.
You might need to play around with it a little bit but it should be enough:
Code:
//Writes filedata to database and returns the ID of the new Signature record
private int writeImageFile(byte[] fileData, String fileName, String imageType, String mode, Integer signatureIDIn, HttpServletRequest request) throws Exception {
//If the previous code found a file that can be uploaded then
//save it into the database via a pstmt
String sql = "";
UtilDBquery udbq = getUser(request).connectToDatabase();
Connection con = null;
int signatureID = 0;
PreparedStatement pstmt = null;
try {
udbq.setUsePreparedStatements(true);
con = udbq.getPooledConnection();
con.setAutoCommit(false);
if((!mode.equals("U")) || (mode.equals("U") && signatureIDIn == 0)) {
sql = "SELECT SEQ_SIGNATURE_ID.nextval FROM DUAL";
pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
signatureID = rs.getInt(1);
}
if (fileName != null && imageType != null) {
sql = "INSERT INTO T_SIGNATURE (SIGNATURE_ID, SIGNATURE) values (?,?)";
InputStream is2 = new ByteArrayInputStream(fileData);
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, signatureID);
pstmt.setBinaryStream(2, is2, (int)(fileData.length));
pstmt.executeUpdate();
pstmt.close();
con.commit();
con = null;
}
}
if(mode.equals("U") && signatureIDIn != 0) {
signatureID = signatureIDIn.intValue();
if (fileName != null && imageType != null) {
sql = "UPDATE T_SIGNATURE SET SIGNATURE = ? WHERE SIGNATURE_ID = ?";
InputStream is2 = new ByteArrayInputStream(fileData);
pstmt = con.prepareStatement(sql);
pstmt.setBinaryStream(1, is2, (int)(fileData.length));
pstmt.setInt(2, signatureID);
pstmt.executeUpdate();
pstmt.close();
con.commit();
con = null;
}
}
} catch (Exception e) {
con = null;
throw new Exception(e.toString());
}
return signatureID;
}
hi how can i insert or upload a picture to database using java(hiberante).