The execute or execteUpdate statement is not executed to insert a PDF

1

I have the following code, what I try is to add a PDF to a database. The problem is that I execute the instruction in SQL and it works, but I try to do it with Java and it does not. That is, it always returns a false.

Conexion c = new Conexion();
Connection con= c.getConexion();

if(con!=null){   
    Statement st;
    st = con.createStatement();
    String insertar = "INSERT INTO [Documentos](Nombre,Documento) VALUES('FacturaJK9389234',(SELECT * FROM OPENROWSET(BULK N'C:\Datos\"+fileName+"', SINGLE_BLOB) as Pdf))"; 

    out.println("<br />" + insertar ); 
    try{
        st.execute(insertar);
        agregado=true;
        st.close();
    }catch(SQLException e){
        agregado=false; 
        e.printStackTrace();
    }
}
out.println("<br />" + agregado);
c.cerrarConexion();
    
asked by mzarate 09.06.2017 в 23:57
source

3 answers

0

I think the error is because you are assigning an alias to the subselect when you want to insert it:

as 'PDF'

Greetings.

    
answered by 10.06.2017 в 00:05
0

Your instruction

st.execute(insertar);

will always return false; since execute returns true if and only if your query is a select, for all other cases (update, delete, insert) it will return false.

If you want to validate that your query was executed correctly and the record was added to the table you should try the following:

int filasAfectadas = st.executeUpdate(insertar);

The method executeUpdate () returns the number of rows affected, if it is greater than 0 the record was added correctly

Greetings.

    
answered by 10.06.2017 в 03:26
0

Well already solved, for those who have the same problem this page helped me:

link

If it was something of permission, for now I am testing it in a test environment, what I do not know, is, when it is already in production you can not leave it with so much insecurity, but I will publish if there is an error. Thank you all

    
answered by 12.06.2017 в 20:53