[英]Display image on a JLabel from a blob column in Oracle database
如何從Oracle數據庫中的Blob列在JLabel上顯示圖像?
您好,我已經努力工作了好幾天,試圖弄清楚如何在oracle數據庫中的blob列上顯示jLabel上的圖像,但是所有努力似乎不足以產生期望的結果。 問題是標簽顯示的是深色的東西,例如背景。 我花了很多時間在谷歌上搜索,還在堆棧溢出中進行搜索,但是,可用資源並沒有使我度過難關。請幫助我! 謝謝。 下面是我正在使用的代碼段...
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);
}
//Insert Student image into database
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();
由於您使用的PreparedStatement
錯誤,因此從未插入圖像。 你要用嗎? 所有參數,而不僅僅是其中一個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.