Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. //Creo la conexión
  2.  
  3. static Connection connection = null;
  4. Class.forName("com.mysql.jdbc.Driver");
  5. connection =DriverManager.getConnection( "jdbc:mysql://midominio.org:3306/nombreDB", "usuario", "contraseña");
  6.  
  7. JFileChooser chooser = new JFileChooser();
  8. FileFilter imageFilter = new FileNameExtensionFilter("Imágenes", ImageIO.getReaderFileSuffixes());
  9. chooser.setFileFilter(imageFilter);
  10. chooser.showOpenDialog(null);
  11. File file = chooser.getSelectedFile();
  12. if(file != null)
  13. {
  14. BufferedImage img;
  15. FileInputStream fis = null;
  16. try {
  17. img=ImageIO.read(file);
  18.  
  19. Image img2 = img.getScaledInstance(50, 67, 50);
  20.  
  21. ImageIcon icon=new ImageIcon(img2);
  22.  
  23. fotoLb.setIcon(icon);
  24.  
  25. fotoLb.revalidate();
  26. fotoLb.repaint();
  27.  
  28. fis = new FileInputStream(file);
  29.  
  30. String sentencia = "UPDATE persona SET FOTOGRAFIA = ? WHERE id_persona = ?";
  31.  
  32. PreparedStatement pst;
  33. try {
  34. pst = connection.prepareStatement(sentencia);
  35. pst.setBinaryStream(1, fis, (int) file.length());
  36. pst.setInt(2,idPersona);
  37.  
  38. pst.executeUpdate();
  39. pst.close();
  40.  
  41. } catch (SQLException | IOException ex) {
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement