Error java.lang.ArrayIndexOutOfBoundsException: 0

0

I have this program

Class UploadImage

package Pruebas;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@ManagedBean
public class SubirImagen {

    private UploadedFile file;

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public void upload() {
        try{
        if (file != null) {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/pruebajpa", "root", "admin");

            PreparedStatement ps = con.prepareStatement("INSERT INTO pruebajpa.img (img) VALUES ('?');");
            ps.setBinaryStream(1, file.getInputstream());
            ps.executeUpdate();
            con.close();
            FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
        }catch(Exception e){
            System.out.println("Error     " + e); 
        }
    }
}

upload.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">


    <h:head>
        <title>TODO supply a title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </h:head>


    <h:body>
        <h:form>

            <p:galleria value="#{galeria.images}" var="image" panelWidth="500" panelHeight="313" showCaption="true">
                <p:graphicImage name="imagenes/#{image}" alt="Image Description for #{image}" title="#{image}"/>

            </p:galleria>


        </h:form>

        <h:form enctype="multipart/form-data">
            <p:growl id="messages" showDetail="true" />

            <p:fileUpload value="#{subirImagen.file}" mode="simple" skinSimple="true"/>

            <p:commandButton value="Submit2" ajax="false" actionListener="#{subirImagen.upload()}" disabled="false" />
        </h:form>

        <h:form enctype="multipart/form-data">

            <p:growl showDetail="true" />
            <p:fileUpload value="#{uploadBean.file}" mode="simple" />
            <p:commandButton ajax="false" value="Subir" actionListener="#{uploadBean.upload()}" />

        </h:form>


    </h:body> 
</html>

I try to upload the image but on the line ps.setBinaryStream (1, file.getInputstream ()); stop running and send the exception can you help me?

    
asked by DanniMunar 21.02.2018 в 21:25
source

1 answer

0

Your query is misspelled:

INSERT INTO pruebajpa.img (img) VALUES ('?');

When you use prepared queries, you should never enclose the placeholder ? in quotation marks.

Try to write the query like this:

INSERT INTO pruebajpa.img (img) VALUES (?);

Also, make sure that your table is called pruebajpa.img ... gives the impression that you are placing the name of the column at the end of the table.

If the table is named pruebajpa , then the query should be written like this:

INSERT INTO pruebajpa (img) VALUES (?);

    
answered by 21.02.2018 в 22:13