1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| public class Image {
public static void main(String[] args) throws ClassNotFoundException{
JFrame frame = new JFrame();
frame.setTitle("Image from DataBase");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Connection conn;
Statement instruction;
ResultSet rs;
String url = "jdbc:mysql://localhost:3306/assvisual";
String userName = "root";
String password = "admin";
String sql="SELECT nom, images FROM image";
JLabel photo, description;
String s;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, userName, password);
instruction = conn.createStatement();
Blob blob;
rs = instruction.executeQuery(sql);
while(rs.next()){
s = rs.getString("nom");
blob = rs.getBlob("images");
ImageIcon icon = new ImageIcon(blob.getBytes(1, (int)blob.length()));
photo = new JLabel(icon);
description = new JLabel();
frame.add(photo, BorderLayout.NORTH);
description.setText(s);
frame.add(description, BorderLayout.SOUTH);
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null,
"erreur JDBC : " + ex.getMessage());
}
frame.setVisible(true);
}
} |
Partager