Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [JAVA] lettura immagini da DB

    Ciao a tutti, ho cercato sul forum e su internet, ma non ho trovato nulla, comunque come da titolo ho un problema con un programma che sto scrivendo, in un DB ho due campi (FOTO, DOCUMENTO) che vengono richiamate da Java, nel DB questi due campi sono impostati come OLE.

    Però non so perchè, quando provo a leggerli mi da errori.

    Vi allego codice di inserimento e richiamo dati dal DB e errore.

    codice:
    public void inserisci_U() throws FileNotFoundException{
    		File foto1 = this.foto;
    		File documento1 = this.documento;
    		try{
    			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    			Class.forName(driver);
    			String url = "jdbc:odbc:MY_DATA";
    		      Connection con = DriverManager.getConnection(url, "username", "password");
    		      String sql = new String ("INSERT INTO Anagrafica (nome,cognome,codiceF,causale,protocollo,archivio,dataNascita,data,gdf,cp,dogana,polmare,V01,V02,V04,V05,V06,V07,V08,V10,V11,V12,COLORE,FOTO,DOCUMENTO) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    		      PreparedStatement cmd = con.prepareStatement(sql);
    		      cmd.setString(1, this.nome);
    		      cmd.setString(2, this.cognome);
    		      cmd.setString(3, this.codiceF);
    		      cmd.setString(4, this.causale);
    		      cmd.setInt(5, this.protocollo);
    		      cmd.setString(6, this.arch);
    		      cmd.setString(7, this.dataNascita);
    		      cmd.setString(8, this.data);
    		      cmd.setString(9, this.pG);
    		      cmd.setString(10, this.pC);
    		      cmd.setString(11, this.pD);
    		      cmd.setString(12, this.pP);
    		      cmd.setString(13, this.V01);
    		      cmd.setString(14, this.V02);
    		      cmd.setString(15, this.V04);
    		      cmd.setString(16, this.V05);
    		      cmd.setString(17, this.V06);
    		      cmd.setString(18, this.V07);
    		      cmd.setString(19, this.V08);
    		      cmd.setString(20, this.V10);
    		      cmd.setString(21, this.V11);
    		      cmd.setString(22, this.V12);
    		      cmd.setString(23, this.colore);
    		      //Passo foto utente al database
    		      BufferedInputStream bis=new BufferedInputStream(new FileInputStream(foto1));
    		      int fileLength = (int)foto1.length();
    		      cmd.setBinaryStream(24, bis, fileLength);
    		      //Passo documento utente al database
    		      bis=new BufferedInputStream(new FileInputStream(documento1));
    		      fileLength = (int)documento1.length();
    		      cmd.setBinaryStream(25, bis, fileLength);
    		      //eseguo query
    		      cmd.executeUpdate();
    		}catch(SQLException | ClassNotFoundException e){
    			e.printStackTrace();
    		}
    	}
    codice:
    public void cerca_U(String codicef) throws IOException{
    		String s = codicef;
    		String query;
    		File file = null;
    		PreparedStatement ps;
    		ResultSet rs = null;
    		try{
    			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    			Class.forName(driver);
    			String url = "jdbc:odbc:MY_DATA";
    		    Connection con = DriverManager.getConnection(url, "username", "password");
    		    query="SELECT * FROM Anagrafica WHERE codiceF=?";
    		    ps = con.prepareStatement(query);
    		    ps.setString(1, s);
    		    rs = ps.executeQuery();
    		    while(rs.next()){
    		    	this.nome = rs.getString("nome");
    		    	this.cognome = rs.getString("cognome");
    		    	this.codiceF = rs.getString("codiceF");
    		    	this.causale = rs.getString("causale");
    		    	this.protocollo = rs.getInt("protocollo");
    		    	this.arch = rs.getString("archivio");
    		    	this.dataNascita = rs.getString("dataNascita");
    		    	this.data = rs.getString("data");
    		    	this.pG = rs.getString("gdf");
    		    	this.pC = rs.getString("cp");
    		    	this.pD = rs.getString("dogana");
    		    	this.pP = rs.getString("polmare");
    		    	this.V01 = rs.getString("V01");
    		    	this.V02 = rs.getString("V02");
    		    	this.V04 = rs.getString("V04");
    		    	this.V05 = rs.getString("V05");
    		    	this.V06 = rs.getString("V06");
    		    	this.V07 = rs.getString("V07");
    		    	this.V08 = rs.getString("V08");
    		    	this.V10 = rs.getString("V10");
    		    	this.V11 = rs.getString("V11");
    		    	this.V12 = rs.getString("V12");
    		    	this.colore = rs.getString("colore");
    		    	//FOTO
    		    	BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file));
    		    	InputStream is= rs.getBinaryStream("FOTO");
                    byte[] buffer = new byte[256];
                    while (is.read(buffer) > 0) {
                     bos.write(buffer);
                    }
                    this.foto = file;
                    //DOCUMENTO
                    bos=new BufferedOutputStream(new FileOutputStream(file));
    		    	is= rs.getBinaryStream("DOCUMENTO");
                    while (is.read(buffer) > 0) {
                     bos.write(buffer);
                    }
                    this.documento = file;
    		    }
    		}catch(SQLException | ClassNotFoundException e){
    			e.printStackTrace();
    		}
    	}
    Quando apro il database lo vedo che ha scritto tutti i dati, anche i due file riesce a passare ma non riesco a recuperarli.

    codice:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at badge.Gestione.cerca_U(Gestione.java:121)
    	at badge.Gui$2.actionPerformed(Gui.java:286)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)


    Vi ringrazio anticipatamente se trovate il problema o se sapete segnalarmi un link dove spiegano bene la procedura!!

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123
    Non l'ho potuto eseguire ovviamente, ma guardandolo credo di aver trovato il problema. L'oggetto file non viene inizializzato prima dell'utilizzo. Lo dichiari come "file = null" e poi sotto lo passi al costruttore di FileOutputStream. Ad occhio penso sia questo il problema.

    Ricorda comunque che devi chiudere il flusso una volta completate le operazioni.

  3. #3
    Risolto il problema vi posto la soluzione nel caso qualcuno abbia la stessa problematica.
    codice:
    public
    void cerca_U(String codicef) throws IOException{
    String s = codicef; String query; PreparedStatement ps; ResultSet rs =
    null; try{
    String driver =
    "sun.jdbc.odbc.JdbcOdbcDriver";
    Class.forName(driver); String url =
    "jdbc:odbc:MY_DATA1";
    Connection con = DriverManager.getConnection(url,
    "username", "password");
    query=
    "SELECT * FROM Anagrafica WHERE codiceF=?";
    ps = con.prepareStatement(query); ps.setString(1, s); rs = ps.executeQuery();
    while(rs.next()){ //FOTO foto_e = new File("new_" + rs.getString("nome_foto"));
    OutputStream fos =
    new java.io.FileOutputStream(foto_e);
    InputStream fin = rs.getBinaryStream(
    "FOTO"); byte[] b = newbyte[20971520];
    fin.read(b); fos.write(b);
    this.foto = foto_e;
    fos.flush(); fos.close();
    //DOCUMENTO documento_e = new File("new_" + rs.getString("nome_documento"));
    fos =
    new java.io.FileOutputStream(documento_e);
    fin = rs.getBinaryStream(
    "DOCUMENTO");
    b =
    newbyte[20971520];
    fin.read(b); fos.write(b);
    this.documento = documento_e;
    fos.flush(); fos.close(); } }
    catch(SQLException | ClassNotFoundException | IOException e){
    e.printStackTrace(); }
    }


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 © 2025 vBulletin Solutions, Inc. All rights reserved.