Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PhotoSaver {
- public static void main(String args[]) {
- try(
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample_db", "root", "Mywhiteflower0!");
- PreparedStatement pStmt = conn.prepareStatement("INSERT INTO photo (image) VALUES (?);");
- ) {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- File file = new File("images/Clients.png");
- System.out.println(file.exists());
- FileInputStream fis = new FileInputStream(file);
- pStmt.setBinaryStream(1, fis, (int) file.length());
- pStmt.executeUpdate();
- } catch (SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public class PhotoClient extends Application {
- private void getImage(ImageView imageView) {
- try (
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample_db", "root", "Mywhiteflower0!");
- PreparedStatement pStmt = conn.prepareStatement("SELECT image FROM photo WHERE image_id = 1;");
- ) {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- try(ResultSet rs = pStmt.executeQuery()) {
- while(rs.next()) {
- byte[] raw = rs.getBytes(1);
- ByteArrayInputStream bis = new ByteArrayInputStream(raw);
- BufferedImage read = ImageIO.read(bis);
- imageView.setImage(SwingFXUtils.toFXImage(read, null));
- }
- }
- } catch (SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement