Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
- <?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>
Add Comment
Please, Sign In to add comment