Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163

    caricare immagine in un database mysql

    Ciao!
    Ho creato una finestra dove è possibile scegliere un'immagine e visualizzarla in un pannello.
    Però vorrei che tale immagine fosse memorizzata in un database mysql per poi poterla visualizzare in un jpanel posto in un'altra finestra. Come posso fare??



    codice:
    package org.img.classi;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    
    public class img extends JFrame implements ActionListener {
      
      
      File f;
      JLabel img;
      JScrollPane sc;
      
      private ImageIcon createImageIcon(String path) {
        try {
          
           return new ImageIcon(path);
        }
        catch (Exception e) {
          System.out.println(e.toString());
          return null;
        }
      }
    
      
      public void actionPerformed (ActionEvent ae) {
        try {
          JFileChooser fc = new JFileChooser();
          
          fc.showDialog(this, "Scegli Foto");
          f = fc.getSelectedFile();
          try {
            img.setVisible(false);
            sc.setVisible(false);
          }
          catch (Exception ex) {}
          img = new JLabel(createImageIcon(f.getAbsolutePath()));
          sc = new JScrollPane(img);
          this.getContentPane().add(sc, BorderLayout.CENTER);
          this.validate();
        }
        catch (Exception e) {}
      }
      
      public img() {
        super("Image display");
        JButton open = new JButton("Scegli Foto");
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(open, BorderLayout.NORTH);
        open.addActionListener(this);
        this.setSize(300, 300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      }
      
      public static void main (String[] args) {
        img d = new img();
      }
    }

  2. #2
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328
    Una soluzione "immediata" sarebbe quella di prevedere nella tabella del DB MySQL un campo di tipo BLOB ed utilizzare, quindi, una PreparedStatement per inviare ad esso il file.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.