You are on page 1of 2

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication5;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
*
* @author owner
*/
public class FXMLDocumentController implements Initializable {

@FXML
private Label label;
@FXML
private Button button;
@FXML
private ImageView imageview;

@FXML
void handleButtonAction(ActionEvent event) throws ClassNotFoundException,
SQLException, IOException {

Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/technorev_mis", "root",
"");

String sql = "SELECT * FROM tbl_photo";


PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
InputStream is = rs.getBinaryStream("photo");
OutputStream os = new FileOutputStream(new File("photo.jpg"));
byte[]content = new byte[1024];
int size = 0;
while((size=is.read(content))!= -1)
{
os.write(content,0,size);

}
os.close();
is.close();
Image imagex = new Image("file:photo.jpg",500,500,true,true);

imageview.setImage(imagex);

}
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}

You might also like