Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. class PhotoSaver {
  2. public static void main(String args[]) {
  3. try(
  4. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample_db", "root", "Mywhiteflower0!");
  5. PreparedStatement pStmt = conn.prepareStatement("INSERT INTO photo (image) VALUES (?);");
  6. ) {
  7. Class.forName("com.mysql.jdbc.Driver").newInstance();
  8.  
  9. File file = new File("images/Clients.png");
  10. System.out.println(file.exists());
  11. FileInputStream fis = new FileInputStream(file);
  12.  
  13. pStmt.setBinaryStream(1, fis, (int) file.length());
  14.  
  15. pStmt.executeUpdate();
  16. } catch (SQLException | ClassNotFoundException e) {
  17. e.printStackTrace();
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }
  23.  
  24. public class PhotoClient extends Application {
  25. private void getImage(ImageView imageView) {
  26. try (
  27. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample_db", "root", "Mywhiteflower0!");
  28. PreparedStatement pStmt = conn.prepareStatement("SELECT image FROM photo WHERE image_id = 1;");
  29. ) {
  30. Class.forName("com.mysql.jdbc.Driver").newInstance();
  31.  
  32. try(ResultSet rs = pStmt.executeQuery()) {
  33. while(rs.next()) {
  34. byte[] raw = rs.getBytes(1);
  35.  
  36. ByteArrayInputStream bis = new ByteArrayInputStream(raw);
  37. BufferedImage read = ImageIO.read(bis);
  38.  
  39. imageView.setImage(SwingFXUtils.toFXImage(read, null));
  40. }
  41. }
  42. } catch (SQLException | ClassNotFoundException e) {
  43. e.printStackTrace();
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement