Thomas Darimont
Erfahrenes Mitglied
Hallo,
schau mal hier:
Gruß Tom
schau mal hier:
Java:
/**
*
*/
package de.tutorials;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* @author Tom
*
*/
public class MSAccessExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager
.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:/temp/example.mdb");
insertImageIntoDatabase(con, new File("c:/temp/winter.jpg"));
BufferedImage image = readImageFromDB(1, con);
con.close();
JFrame frame = new JFrame("MSAccessExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setVisible(true);
}
private static BufferedImage readImageFromDB(int id, Connection con)
throws Exception {
PreparedStatement preparedStatement = con
.prepareStatement("SELECT data FROM image WHERE id =?");
preparedStatement.setInt(1, 1);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
InputStream data = resultSet.getBinaryStream("data");
BufferedImage image = ImageIO.read(data);
resultSet.close();
preparedStatement.execute();
preparedStatement.close();
return image;
}
private static void insertImageIntoDatabase(Connection con, File file)
throws Exception {
PreparedStatement preparedStatement = con
.prepareStatement("INSERT INTO image (data) VALUES(?)");
preparedStatement.setBinaryStream(1, new FileInputStream(file),(int)file.length());
preparedStatement.execute();
preparedStatement.close();
}
}
Gruß Tom