Hello I have been struggling for days now trying to figure out how I can display an image on a jLabel from a blob column in oracle database but all efforts seems not sufficient to produce desired results. The problem is that the Label displays a dark colour stuff like background. I've spent so much time googling and also searching here on the stack overflow but, the available resources aren't putting me through Please help me out ! The variable image is the jLabel I'm using to set the image. And I forgot to mention that while researching I realised that I can do a result2.getBlob(); but my it throws a UnsupportedOperationException(my driver doesn't support that). at the result2.getBlob(); line. So I had to omit it
private void studentIdItemStateChanged(java.awt.event.ItemEvent evt) {
String id = (String)studentId.getSelectedItem();
try{
con = DriverManager.getConnection("jdbc:odbc:test", "SYSTEM", "VICTOR");
stmt = con.createStatement();
result = stmt.executeQuery("select * from STUDENTDBASE.STUDENT where Student_Id = '" + id + "' ");
result = stmt.getResultSet();
while (result.next()){
surname.setText(result.getString("Surname"));
firstName.setText(result.getString("First_Name"));
otherNames.setText(result.getString("Other_Names"));
phoneNumber.setText(result.getString("Mobile_Number"));
dob.setText(result.getString("Date_Of_Birth"));
gender.setText(result.getString("Gender"));
address.setText(result.getString("Address"));
state.setText(result.getString("State"));
lga.setText(result.getString("Lga"));
}
result.close();
PreparedStatement ps;
con = DriverManager.getConnection("jdbc:odbc:test", "SYSTEM", "VICTOR");
ps = (PreparedStatement) con.prepareStatement("select Image from STUDENTDBASE.STUDENT_Images where STUDENT_ID = '" + id + "' ");
result2 = ps.executeQuery();
while (result2.next()){
InputStream in = result2.getBinaryStream("Image");
BufferedImage im = ImageIO.read(in);
BufferedImage outimage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = outimage.createGraphics();
float xScale = (float)image.getWidth() / outimage.getWidth();
float yScale = (float)image.getHeight() / outimage.getHeight();
AffineTransform at = AffineTransform.getScaleInstance(xScale,yScale);
g.drawRenderedImage(im,at);
g.dispose();
Image scaledImage = outimage.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(scaledImage);
image.setIcon(icon);
image.revalidate();
basicDetails.revalidate();
}
result2.close();
con.close();
}
catch(SQLException d){
JOptionPane.showMessageDialog(null, "Error is due to " + d.getMessage(), "Error message from database", JOptionPane.ERROR_MESSAGE);
}
catch(IOException io){
JOptionPane.showMessageDialog(null, "Error is due to " + io.getMessage(), "Error message from database", JOptionPane.ERROR_MESSAGE);
}
url = "jdbc:odbc:test";
String query1 = "insert into STUDENTDBASE.STUDENT_Images (Student_Id, Image, Image_Path, Image_Size)values('" + counterValue + "', ?, '" + path + "', '" + stringLength + "' ) ";
con = DriverManager.getConnection(url, "SYSTEM", "VICTOR");
PreparedStatement ps=con.prepareStatement(query1);
FileInputStream fin=new FileInputStream(file);
ps.setString(1, file.getName());
ps.setBinaryStream(2, fin, file.length());
ps.executeUpdate();