import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StoreAndRetrieveImage extends JApplet {
private Connection connection;
private Statement stmt;
private PreparedStatement pstmt = null;
private DescriptionPanel descriptionPanel1
= new DescriptionPanel();
private JComboBox jcboCountry = new JComboBox();
public StoreAndRetrieveImage() {
try {
connectDB();
storeDataToTable(); //Store data to the table (including image)
fillDataInComboBox();
retrieveFlagInfo((String)(jcboCountry.getSelectedItem()));
}
catch (Exception ex) {
ex.printStackTrace();
}
jcboCountry.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
retrieveFlagInfo((String)(evt.getItem()));
}
});
add(jcboCountry, BorderLayout.NORTH);
add(descriptionPanel1, BorderLayout.CENTER);
}
private void connectDB() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
connection = DriverManager.getConnection
("jdbc:mysql://localhost/javabook", "scott", "tiger");
System.out.println("Database connected");
stmt = connection.createStatement();
pstmt = connection.prepareStatement("select flag, description " +
"from Country where name = ?");
}
private void storeDataToTable() {
String[] countries = {"Canada", "UK", "USA", "Germany",
"Indian", "China"};
String[] imageFilenames = {"image/ca.gif", "image/uk.gif",
"image/us.gif", "image/germany.gif", "image/india.gif",
"image/china.gif"};
String[] descriptions = {"A text to describe Canadian " +
"flag is omitted", "British flag ...", "American flag ...",
"German flag ...", "Indian flag ...", "Chinese flag ..."};
try {
PreparedStatement pstmt = connection.prepareStatement(
"insert into Country values(?, ?, ?)");
for (int i = 0; i < countries.length; i++) {
pstmt.setString(1, countries[i]);
java.net.URL url =
this.getClass().getResource(imageFilenames[i]);
InputStream inputImage = url.openStream();
pstmt.setBinaryStream(2, inputImage,
(int)(inputImage.available()));
pstmt.setString(3, descriptions[i]);
pstmt.executeUpdate();
}
System.out.println("Table Country populated");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void fillDataInComboBox() throws Exception {
ResultSet rs = stmt.executeQuery("select name from Country");
while (rs.next()) {
jcboCountry.addItem(rs.getString(1));
}
}
private void retrieveFlagInfo(String name) {
try {
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob(1);
ImageIcon imageIcon = new ImageIcon(
blob.getBytes(1, (int)blob.length()));
descriptionPanel1.setImageIcon(imageIcon);
descriptionPanel1.setName(name);
String description = rs.getString(2);
descriptionPanel1.setDescription(description);
}
}
catch (Exception ex) {
System.err.println(ex);
}
}
public static void main(String[] args) {
StoreAndRetrieveImage applet = new StoreAndRetrieveImage();
JFrame frame = new JFrame();
frame.getContentPane().add(applet);
frame.setDefaultCloseOperation(3);
frame.setTitle("StoreAndRetrieveImage");
frame.setSize(400, 320);
frame.setVisible(true);
}
}