-1
  • コンボで製品クラスidcatクラスカテゴリを選択するためのデータを挿入しようとしましたが、アイデアはありません

  • -クラス

    パブリック クラス カテゴリ {

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

    }

    public class 製品 {

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

    }

    -DAO CLASS カテゴリーDAO

    public Collection 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 プロダクトDAO

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 カテゴリNegocio

    パブリック クラス カテゴリネゴシオ {

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

    }

  • NEGOCIO CLASS プロダクトNegocio

    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 インサート

<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>
4

1 に答える 1

0

のようなサーブレットを介してアプリケーションにアクセスする必要がありますhttp://localhost/YouWebAppName/InsertarProdServlet。これにより、GET 要求が呼び出され (doGet関数が実行され)、要求属性が設定されます。次に、insertar.jspページに転送され、送信をクリックすると、POST 要求が呼び出されます (doPost関数が実行されます)。

補足として、次回はそのコードウォールを投稿しないでください。そうしないと、誰もあなたの質問を読みません。コードのSSCCEを投稿して、問題の可能性がある場所を示してください。En mi opinión, cuando vi tu pregunta ni me dieron ganas de leerla por solo ver tanto código que no forma parte del problema concreto (IMO、あなたの質問を見たとき、そうではないすべてのコードを見るためだけにそれを読みたくありませんでした実際の問題の一部)。

于 2013-02-20T16:53:36.997 に答える