2013-02-20 62 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,當我看到您的問題時,我不想閱讀它只是爲了查看所有不是真正問題的一部分)。

相關問題