2013-02-20 61 views
-1
  • 我试图选择产品类别idcat类 类别与组合,但没有插入数据的任何想法,请如何将数据从一张表插入到另一张表中选择它作为组合框?

  • - 班

    公共类Categoria {

    private int idcat; 
    private String name; 
    private String descrip; 
    

    }

    public class Producto {

    private int producto; 
    private int idcategoria; 
    private String nombre; 
    private String descrip; 
    

    }

    -dao CLASS CategoriaDAO

    公文集listarIdCat()抛出DAOExcepcion {

    Collection<Categoria> c=new ArrayList<Categoria>(); 
        String query="SELECT id_categoria from categoria "; 
        Connection con=null; 
        PreparedStatement stmt=null; 
        ResultSet rs=null; 
    
        try { 
         con=ConexionBD.obtenerConexionDirecta(); 
         stmt=con.prepareStatement(query); 
         rs=stmt.executeQuery(); 
         while(rs.next()) { 
          Categoria vo=new Categoria(); 
          vo.setIdcat(rs.getInt("id_categoria")); 
          c.add(vo); 
    
         } 
    
        } catch (SQLException e) { 
         System.err.println(e.getMessage()); 
         throw new DAOExcepcion(e.getMessage()); 
        }finally{ 
    
         this.cerrarStatement(stmt); 
    
         this.cerrarResultSet(rs); 
         this.cerrarConexion(con); 
        } 
        return c; 
    } 
    
  • DAO CLASS ProductoDAO

public void insertar(Producto vo) throws DAOExcepcion { 

>  
>    String query = "INSERT INTO producto(id_categoria,nombre,descripcion) VALUES ((SELECT id_categoria 
> FROM categoria WHERE id_categoria=?),?,?,?)"; 
>    Connection con = null; 
>    PreparedStatement stmt = null; 
>  
>    try { 
>     con = ConexionBD.obtenerConexionDirecta(); 
>     stmt = con.prepareStatement(query); 
>     stmt.setInt(1, vo.getIdcat()); 
>     stmt.setString(2, vo.getNombre()); 
>     stmt.setString(3, vo.getDescrip()); 
>     int i = stmt.executeUpdate(); 
>     if (i != 1) { 
>      throw new SQLException("No se pudo insertar"); 
>     } 
>    } catch (SQLException e) { 
>     System.err.println(e.getMessage()); 
>     throw new DAOExcepcion(e.getMessage()); 
>    } finally { 
>     this.cerrarStatement(stmt); 
>     this.cerrarConexion(con); 
>    } 
>  
>   } 
  • NEGOCIO CLASS CategoriaNegocio

    公共类CategoriaNegocio {

    public Collection<Categoria> listarIdCat()throws DAOExcepcion{ 
    
        CategoriaDAO dao=new CategoriaDAO(); 
    
        Collection<Categoria> lista=dao.listarIdCat(); 
    
        return lista; 
    
    } 
    

    }

  • NEGOCIO CLASS ProductoNegocio

    public void insertarProducto(int idCat, String nom, String descrip, 
          double prec, int stock, String image) throws DAOExcepcion { 
    
         Producto p = new Producto(); 
         p.setIdcat(idCat); 
         p.setNombre(nom); 
         p.setDescrip(descrip); 
         ProductoDAO dao = new ProductoDAO(); 
         try { 
          dao.insertar(p); 
         } catch (DAOExcepcion e) { 
          throw e; 
         } 
    
        } 
    
  • SERVLET InsertarProdServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     CategoriaNegocio negocio = new CategoriaNegocio(); 

     try { 

      Collection<Categoria> lista = negocio.listarIdCat(); 

      request.setAttribute("IDCATEGORIA", lista); 

     } catch (DAOExcepcion e) { 

      System.out.println(e.getMessage()); 
     } 
     RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/insertar.jsp"); 
     rd.forward(request, response); 

    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     String id=request.getParameter("tid_cat"); 
     int idcat=Integer.parseInt(id.trim()); 

     String nom=request.getParameter("tnomprod"); 
     String des=request.getParameter("tdesprod"); 



     ProductoNegocio negocio=new ProductoNegocio(); 

     try { 
      negocio.insertarProducto(idcat, nom, des); 
      request.setAttribute("MENSAJE", "Se inserto correctamente"); 
      RequestDispatcher rd=request.getRequestDispatcher("/insertar.jsp"); 
      rd.forward(request,response); 


     } catch (DAOExcepcion e) { 

      request.setAttribute("MENSAJE_ERROR", "Hubo problemas"); 
      RequestDispatcher rd=request.getRequestDispatcher("/error.jsp"); 
      rd.forward(request,response); 
     } 


    } 

JSP insertar

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<form action="InsertarProdServlet" method="post"> 

<table><caption>Inserte Producto</caption> 
<tr> 
<td align="center"><select name="tid_cat" size="1" > 
<c:forEach items="${IDCATEGORIA}" var="c" > 
<option value="${c.idcategoria}" >${c.idcategoria}</option> 
</c:forEach> 
</select></td><td> 
</td> 
</tr> 
<tr> 
<td>NOMBRE</td><td><input type="text" name="tnomprod"> </td></tr><tr> 
<td>DESCRIPCION</td><td><input type="text" name="tdesprod" > </td></tr><tr> 
<td><input type="submit" value="INSERTAR"> </td></tr><tr> 
</tr> 
</table> 
</form> 
${MENSAJE} 
</body> 
</html> 
+0

你好BalusC这是我的问题 – JuanHeart 2013-02-20 16:06:46

回答

0

您必须通过Servlet来访问你的应用程序一样http://localhost/YouWebAppName/InsertarProdServlet,THI s将调用GET请求(从而执行doGet函数)并设置请求属性。然后它将转到您的insertar.jsp页面,点击提交时,它将调用POST请求(从而执行doPost函数)。

作为便笺,下次不要发布该代码墙,否则没有人会阅读您的问题,只需发布​​代码的SSCCE即可显示问题出在哪里。 En miopinión,cuando vi tu pregunta ni me dieron ganas de leerla por solo ver tantocódigoque no forma parte del problema concreto(IMO,当我看到您的问题时,我不想阅读它只是为了查看所有不是真正问题的一部分)。

相关问题