插入数据库时​​netbeans项目中的参数类型不匹配

Posted

技术标签:

【中文标题】插入数据库时​​netbeans项目中的参数类型不匹配【英文标题】:argument type mismatch in netbeans project while inserting to database 【发布时间】:2013-04-13 20:14:28 【问题描述】:

我正在创建一个允许管理员管理菜单项的网页。对于删除部分,它是成功的..但是当我想插入一个新菜单时出现错误。

我正在使用 phpmyadmin 作为我的数据库

运行我的 netbeans 项目时出现此错误

HTTP 状态 500-

类型:异常报告

说明:服务器遇到内部错误 (),导致它无法完成此请求。

异常: org.apache.jasper.JasperException:java.lang.IllegalArgumentException:参数类型不匹配

根本原因: java.lang.IllegalArgumentException:参数类型不匹配

完整的堆栈跟踪

java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.jasper.runtime.JspRuntimeLibrary.handleSetProperty(JspRuntimeLibrary.java:672)
at org.apache.jsp.confirmadd_jsp._jspService(confirmadd_jsp.java:90)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)

ma​​nagemenu.jsp

<h3>Add new menu</h3>
<div>
<form method="post" action="confirmadd.jsp">
<table>
<tr><td>Food ID</td><td>:</td><td><input type="text" name="food_id" placeholder="food ID"/></td></tr>
<tr><td>Food Name</td><td>:</td><td><input type="text" name="food_name" placeholder="food name"/></td></tr>
<tr><td>Food Description</td><td>:</td><td><input type="text" name="food_description" placeholder="food desc"/></td></tr>
<tr><td>Food Price</td><td>: </td><td><input type="text" name="food_price" placeholder="food price"/></td></tr>
</table>
<input type="submit" value="Submit"/><input type="reset" value="Reset"/>
</form>

confirmadd.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page import="java.util.*" %>
<jsp:useBean id="newmenu" class="restaurantBean.menu" scope="session"/>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="css/jquery-ui-1.9.2.custom.css" rel="stylesheet">
    <script src="js/jquery-1.8.3.js"></script>
    <script src="js/jquery-ui-1.9.2.custom.js"></script>
    <link href="css/style.css" rel="stylesheet" type="text/css"/>
    <title>Confirm Add Food</title>
</head>
<body>
<center><br><h1>Restaurant E-menu and Reservation System</h1><br><br>

 <a href="main.jsp">Home</a>
    &nbsp;&nbsp;&nbsp;&nbsp;<a href="managemenu.jsp">Manage Menu</a>
    &nbsp;&nbsp;&nbsp;&nbsp;<a href="updatestock.jsp">Update stock</a>

<div class="content">
    <h1>Confirm Food</h1>
    <jsp:setProperty name="newmenu" property="food_id" value='<%=request.getParameter("food_id")%>'/>
    <jsp:setProperty name="newmenu" property="food_name" value="<%=request.getParameter("food_name")%>"/>
    <jsp:setProperty name="newmenu" property="food_description" value='<%=request.getParameter("food_description")%>'/>
    <jsp:setProperty name="newmenu" property="food_price" value='<%=request.getParameter("food_price")%>'/>

    <table id="form">
        <tr><th colspan="3">Food Details</th></tr>
    <tr><td>Food ID</td><td>:</td><td><jsp:getProperty name="newmenu" property="food_id"/></td></tr>
    <tr><td>Food Name</td><td>:</td><td><jsp:getProperty name="newmenu" property="food_name"/></td></tr>
    <tr><td>Food Description</td><td>:</td><td><jsp:getProperty name="newmenu" property="food_description"/></td></tr>
    <tr><td>Food Price</td><td>:</td><td>RM<jsp:getProperty name="newmenu" property="food_price"/></td></tr>
    <tr><td colspan="3"><input type="button" value="proceed" onclick="location.href='./addmenu'"/>
    <input type="button" value="Back" onClick="history.go(-1);return true;"></td></tr>
    </table>

</div>
</div>
<jsp:include page="/WEB-INF/includes/footer.jsp" />
</div>
</body>
</html>

menu.java

package restaurantBean;

public class menu 
private String food_description;
private String food_id;
private String food_name;
private double food_price;


public void setFood_description(String a)
    food_description=a;

public String getFood_description()
    return food_description;

public void setFood_id(String b)
    food_id=b;

public String getFood_id()
    return food_id;

 public void setFood_price(double c)
    food_price=c;

public double getFood_price()
    return food_price;

 public void setFood_name(String d)
    food_name=d;

public String getFood_name()
    return food_name;



addmenu.java

package restaurantPackages;

import restaurantBean.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "addmenu", urlPatterns = "/addmenu")
public class addmenu extends HttpServlet 
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException 
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try 
           Class.forName("com.mysql.jdbc.Driver");

           HttpSession session=request.getSession();
        menu m=(menu) session.getAttribute("newmenu");


        String url="jdbc:mysql://localhost:3306/restaurant";
        String username="root";
        String password="";
        Connection con=DriverManager.getConnection(url,username,password);

        Statement statement=con.createStatement();            
        //ResultSet result=statement.executeQuery("select food_id from food where food_name='"+m.getFood_name().trim()+"'");
        //int c = 0;
        //while(result.next())
        //c=result.getInt(1);
        //
        PreparedStatement stmt=con.prepareStatement("insert into food (food_id, food_name, food_description, food_price) values (?,?,?,?)");
        stmt.setString(1,m.getFood_id());
        stmt.setString(2,m.getFood_name());
        stmt.setString(3,m.getFood_description());
        stmt.setDouble(4,m.getFood_price());

        stmt.executeUpdate();


        stmt.close();
        con.close();
        menu food=new menu();
        session.setAttribute("food",food);
        RequestDispatcher dis=request.getRequestDispatcher("/WEB-INF/addsuccess.jsp");
        dis.forward(request,response);
    
    catch(java.lang.ClassNotFoundException e)
          out.println("ClassNotFoundException:"+e.getMessage());
       catch(SQLException e)
            out.println("SQLState:  " + e.getSQLState());
            out.println("Message:  " + e.getMessage());
            out.println("Vendor:  " + e.getErrorCode());
       
    finally             
        out.close();
    

addsuccess.jsp

<div class="content">

<tr><td>Successfully add food ID = $food.food_id from restaurant</td></tr>
<table id="form">
        <tr><th colspan="3">New Added Food Details</th></tr>
    <tr><td>Food ID</td><td>:</td><td>$food.food_id</td></tr>
    <tr><td>Food Name</td><td>:</td><td>$food.food_name</td></tr>
    <tr><td>Food Description</td><td>:</td><td>$food.food_description</td></tr>
    <tr><td>Food Price</td><td>:</td><td>$food.food_price</td></tr>

    </table>
<tr><td><input type="button" value="Continue Manage Menu" onclick="location.href='managemenu.jsp'"/></td></tr>
<tr><td><input type="button" value="View Menu" onclick="location.href='main.jsp'"/>    </td></tr>

【问题讨论】:

【参考方案1】:

由于没有完整的堆栈跟踪,我猜测问题出在这里:

<jsp:setProperty name="newmenu" property="food_price" value='<%=request.getParameter("food_price")%>'/>

food_price 字段是双精度的,您的 Web 表单上的价格格式可能无法转换为双精度(只是为了测试,将 food_price 类型更改为字符串,看看会发生什么)。并在此处复制更多堆栈跟踪,以便更容易识别问题。

【讨论】:

哇!这真的是问题!在我将所有“双”更改为“字符串”后,我成功添加了它。如果我想使用双。那是什么方法?感谢您的帮助 确保在设置属性之前将 String 转换为可解析的 Double 格式。例如,如果您的货币价值为 45,35,那么您应该将其转换为 45.35(或者,更好的是,将其记为美分等 - 4535.0 以尽可能避免舍入问题)

以上是关于插入数据库时​​netbeans项目中的参数类型不匹配的主要内容,如果未能解决你的问题,请参考以下文章

使用 Javafx(netbeans) 在 SQLITE 数据库中插入 PDF 或任何类型的文件

如何将文本框中的数据插入到 java-netbeans 中的 mysql 数据库中

在 netbeans 7 中,如何在构建 maven 项目时跳过测试并添加 maven 附加参数?

java netbean向db插入数据不起作用

解决 NetBeans IDE 中的项目问题(ConnectingDataBase)

安卓连接数据库