import java.sql.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
/**
* This class is used to retreive images from
* an MS Access database.
*/
public class DataObject {
private Connection con;
public DataObject(String databaseName) {
// Connect to the database
connect(databaseName);
}
/**
* This method attempts to load the default driver
* and connect to the specified database.
*
* @param dbName The database name
*/
private void connect(String dbName) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(
"jdbc:odbc:DRIVER=Microsoft Access
Driver*.mdb);DBQ="+dbName+".mdb");
} catch (ClassNotFoundException ce) {
System.out.println(ce.getMessage());
} catch (SQLException se) {
System.out.println(se.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* This method attempt to disconnect from the currently
* connected database.
*/
public void disconnect() {
try {
con.close();
} catch (SQLException se) {
System.out.println(se.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* This method attempt to retreive a image stored
* in the database.
*
* @param pos The image column position.
* @param query The sql query.
* @return The collection with images retreived from the database
*/
public Image getImage(int pos, String query) {
return getImageIcon(pos, query).getImage();
}
/**
* This method attempt to retreive a image icon stored
* in the database.
*
* @param pos The image column position.
* @param query The sql query.
* @return The collection with image icons retreived from the database
*/
public ImageIcon getImageIcon(int pos, String query) {
InputStream is = null;
try {
Statement stmt = con.createStatement();
// Execute the specified query string
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
// Get the BLOB inputstream
is = rs.getBinaryStream(pos);
OutputStream fos = new FileOutputStream("tmpImage.jpg");
byte[] buffer = new byte[1024];
int read;
while ((read = is.read(buffer,0,buffer.length)) != -1) {
fos.write(buffer,0,read);
}
fos.close();
is.close();
}
rs.close();
stmt.close();
} catch (SQLException se) {
System.out.println(se.getMessage());
} catch (FileNotFoundException fe) {
System.out.println(fe.getMessage());
} catch (IOException ie) {
System.out.println(ie.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
return new ImageIcon("tmpImage.jpg");
}
/**
* This method inserts a specified file (image) into the
* currently connected databse.
*
* @param file The file we want to store
* @param pos The column position
* @param query The insert query
*/
public void insertImage(File file, int pos, String query) {
try {
// Example: INSERT INTO tblImage VALUES( ? )
PreparedStatement stmt = con.prepareStatement(query);
InputStream is = new FileInputStream(file);
stmt.setBinaryStream(pos, is, (int)(file.length()));
stmt.executeUpdate();
} catch (SQLException se) {
System.out.println(se.getMessage());
} catch (FileNotFoundException fe) {
System.out.println(fe.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
}
}