Ver Mensaje Individual
  #1 (permalink)  
Antiguo 03/09/2014, 10:29
Avatar de stuart_david3
stuart_david3
 
Fecha de Ingreso: agosto-2011
Mensajes: 215
Antigüedad: 13 años, 5 meses
Puntos: 1
Sonrisa Problema JSP y MySQL

Buen día :)!

Disculpen la molestia, soy nuevo en JSP y tengo un problema al intentar dar de alta un registro. Verán, tengo un pequeño formulario con 3 cajas de texto y un botón el cual debería de dar de alta un registro en mi tabla "usuario"...

Éste es el error que me sale:

javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationExcep tion
Exception Description: Cannot acquire data source [primeraagenda].
Internal Exception: javax.naming.NamingException: Lookup failed for 'primeraagenda' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NameNotFoundException: primeraagenda not found]
Nota Los rastreos de pila completos de la excepción y sus causas raíz están disponibles en los logs GlassFish Server Open Source Edition 4.0 .


Éste es mi código HTML:

Código HTML:
Ver original
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5.         <title>Agenda</title>
  6.     </head>
  7.     <body>
  8.         <h1>Agregar un contacto</h1>
  9.         <form action="Guardar" method="Post">
  10.         Usuario: &nbsp;&nbsp;
  11.         <input type="text" name="nombre"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  12.         Telefono:&nbsp;&nbsp;&nbsp;&nbsp;
  13.         <input type="text" name="telefono">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  14.         Direccion:&nbsp;&nbsp;&nbsp;&nbsp;
  15.         <input type="text" name="direccion">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  16.         <input type="submit" value="Guardar"><br>
  17.         </form>
  18.     </body>
  19. </html>

Este es mi Servlet en un paquete llamado Controlador ---> Guardar.java

Código Java:
Ver original
  1. package controlador;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import javax.persistence.EntityManager;
  6. import javax.persistence.EntityManagerFactory;
  7. import javax.persistence.Persistence;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. /**
  15.  *
  16.  * @author Deivid
  17.  */
  18. @WebServlet(name = "Guardar", urlPatterns = {"/Guardar"})
  19. public class Guardar extends HttpServlet {
  20.  
  21.     /**
  22.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  23.      * methods.
  24.      *
  25.      * @param request servlet request
  26.      * @param response servlet response
  27.      * @throws ServletException if a servlet-specific error occurs
  28.      * @throws IOException if an I/O error occurs
  29.      */
  30.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  31.             throws ServletException, IOException {
  32.        
  33.         String nombre = request.getParameter("nombre");
  34.         String telefono = request.getParameter("telefono");
  35.         String direccion = request.getParameter("direccion");
  36.        
  37.         modelo.Usuario usuario = new modelo.Usuario();
  38.         usuario.setIdusuario(0);
  39.         usuario.setNombre(nombre);
  40.         usuario.setTelefono(telefono);
  41.         usuario.setDireccion(direccion);
  42.        
  43.         EntityManager em;
  44.         EntityManagerFactory emf;
  45.        
  46.         emf=Persistence.createEntityManagerFactory("AgendasPU");
  47.         em = emf.createEntityManager();
  48.         em.getTransaction().begin();
  49.         em.persist(usuario);
  50.         em.flush();
  51.         em.getTransaction().commit();
  52.         response.sendRedirect("correcto.jsp");
  53.        
  54.        
  55.        
  56.     }
  57.  
  58.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  59.     /**
  60.      * Handles the HTTP <code>GET</code> method.
  61.      *
  62.      * @param request servlet request
  63.      * @param response servlet response
  64.      * @throws ServletException if a servlet-specific error occurs
  65.      * @throws IOException if an I/O error occurs
  66.      */
  67.     @Override
  68.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  69.             throws ServletException, IOException {
  70.         processRequest(request, response);
  71.     }
  72.  
  73.     /**
  74.      * Handles the HTTP <code>POST</code> method.
  75.      *
  76.      * @param request servlet request
  77.      * @param response servlet response
  78.      * @throws ServletException if a servlet-specific error occurs
  79.      * @throws IOException if an I/O error occurs
  80.      */
  81.     @Override
  82.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  83.             throws ServletException, IOException {
  84.         processRequest(request, response);
  85.     }
  86.  
  87.     /**
  88.      * Returns a short description of the servlet.
  89.      *
  90.      * @return a String containing servlet description
  91.      */
  92.     @Override
  93.     public String getServletInfo() {
  94.         return "Short description";
  95.     }// </editor-fold>
  96.  
  97. }

Y mi Entity en un paquete que llame Modelo ----> Usuario.java

Código Java:
Ver original
  1. package modelo;
  2.  
  3. import java.io.Serializable;
  4. import javax.persistence.Basic;
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.GeneratedValue;
  8. import javax.persistence.GenerationType;
  9. import javax.persistence.Id;
  10. import javax.persistence.NamedQueries;
  11. import javax.persistence.NamedQuery;
  12. import javax.persistence.Table;
  13. import javax.validation.constraints.Size;
  14. import javax.xml.bind.annotation.XmlRootElement;
  15.  
  16. /**
  17.  *
  18.  * @author Deivid
  19.  */
  20. @Table(name = "usuario")
  21. @XmlRootElement
  22. @NamedQueries({
  23.     @NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u"),
  24.     @NamedQuery(name = "Usuario.findByIdusuario", query = "SELECT u FROM Usuario u WHERE u.idusuario = :idusuario"),
  25.     @NamedQuery(name = "Usuario.findByNombre", query = "SELECT u FROM Usuario u WHERE u.nombre = :nombre"),
  26.     @NamedQuery(name = "Usuario.findByTelefono", query = "SELECT u FROM Usuario u WHERE u.telefono = :telefono"),
  27.     @NamedQuery(name = "Usuario.findByDireccion", query = "SELECT u FROM Usuario u WHERE u.direccion = :direccion")})
  28. public class Usuario implements Serializable {
  29.     private static final long serialVersionUID = 1L;
  30.     @Id
  31.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  32.     @Basic(optional = false)
  33.     @Column(name = "idusuario")
  34.     private Integer idusuario;
  35.     @Size(max = 45)
  36.     @Column(name = "nombre")
  37.     private String nombre;
  38.     @Size(max = 45)
  39.     @Column(name = "telefono")
  40.     private String telefono;
  41.     @Size(max = 45)
  42.     @Column(name = "direccion")
  43.     private String direccion;
  44.  
  45.     public Usuario() {
  46.     }
  47.  
  48.     public Usuario(Integer idusuario) {
  49.         this.idusuario = idusuario;
  50.     }
  51.  
  52.     public Integer getIdusuario() {
  53.         return idusuario;
  54.     }
  55.  
  56.     public void setIdusuario(Integer idusuario) {
  57.         this.idusuario = idusuario;
  58.     }
  59.  
  60.     public String getNombre() {
  61.         return nombre;
  62.     }
  63.  
  64.     public void setNombre(String nombre) {
  65.         this.nombre = nombre;
  66.     }
  67.  
  68.     public String getTelefono() {
  69.         return telefono;
  70.     }
  71.  
  72.     public void setTelefono(String telefono) {
  73.         this.telefono = telefono;
  74.     }
  75.  
  76.     public String getDireccion() {
  77.         return direccion;
  78.     }
  79.  
  80.     public void setDireccion(String direccion) {
  81.         this.direccion = direccion;
  82.     }
  83.  
  84.     @Override
  85.     public int hashCode() {
  86.         int hash = 0;
  87.         hash += (idusuario != null ? idusuario.hashCode() : 0);
  88.         return hash;
  89.     }
  90.  
  91.     @Override
  92.     public boolean equals(Object object) {
  93.         // TODO: Warning - this method won't work in the case the id fields are not set
  94.         if (!(object instanceof Usuario)) {
  95.             return false;
  96.         }
  97.         Usuario other = (Usuario) object;
  98.         if ((this.idusuario == null && other.idusuario != null) || (this.idusuario != null && !this.idusuario.equals(other.idusuario))) {
  99.             return false;
  100.         }
  101.         return true;
  102.     }
  103.  
  104.     @Override
  105.     public String toString() {
  106.         return "modelo.Usuario[ idusuario=" + idusuario + " ]";
  107.     }
  108.    
  109. }

Última edición por stuart_david3; 03/09/2014 a las 10:37 Razón: Falto información