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(); } }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: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(); } }
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!!