日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql与jframe_java-如何在JFrame上显示从mysql检索到的图像

發布時間:2023/12/1 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql与jframe_java-如何在JFrame上显示从mysql检索到的图像 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我在顯示從JFrame的數據庫檢索的圖像時遇到問題.這是我將要使用的

??????……

try

{

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/studio","root","");

Statement st=con.createStatement();

ResultSet rs = st.executeQuery( "select image from photo_instn where cust_id='2'") ;

while(rs.next())

{

byte[] imagedata = rs.getBytes("image") ;

Image img = Toolkit.getDefaultToolkit().createImage(imagedata);

ImageIcon icon =new ImageIcon(img);

JLabel lPhoto = new JLabel(icon) ;

setLayout(null); // BYTES TO IMAGE

System.out.println("Inside");

System.out.println(lPhoto);

this.add(lPhoto) ;

lPhoto.setBounds(200,20,300,400);

}

}

此代碼沒有問題.但是圖像未顯示在框架上…

請幫助我解決這個問題….

最佳答案

import java.awt.*;

import java.sql.*;

import javax.swing.*;

public class DisplayImage extends JFrame {

Connection connection = null;

PreparedStatement statement = null;

ResultSet result;

public DisplayImage() {

super("Image Display");

setSize(600, 600);

connection = getConnection();

try { // table name:image and second image is field name

statement = connection

.prepareStatement("select image from image where id = 1");

result = statement.executeQuery();

byte[] image = null;

while (result.next()) {

image = result.getBytes("image");

}

Image img = Toolkit.getDefaultToolkit().createImage(image);

ImageIcon icon = new ImageIcon(img);

JLabel lPhoto = new JLabel();

lPhoto.setIcon(icon);

add(lPhoto);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

setVisible(true);

}

public Connection getConnection() {

Connection connection = null;

try {

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection(

// user name:root and password:blank

"jdbc:mysql://localhost:3306/insertRetriveImages", "root",

"");

} catch (Exception e) {

System.out.println("Error Occured While Getting the Connection: - "

+ e);

}

return connection;

}

public static void main(String[] args) {

new DisplayImage();

}

}

總結

以上是生活随笔為你收集整理的mysql与jframe_java-如何在JFrame上显示从mysql检索到的图像的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。