0

我以前从数据库“监狱”读取的 JSP 文件运行良好。但是我应该将值插入数据库的“add.jsp”文件显示了这个异常:

"SQLException: Number of query values and destination fields are not the same"
My MS Access2007, converted to **Access2003** Prison db with table 'Nominal' has 14 fields:
PNo(int), PName(str), Age(int), Address(str), Height(int), Weight(int), Health(str), Possessions(str), Occupation(str), Case(str), Sentence(str), From(str), To(str) and Parole(str).

到目前为止,在 db 上使用的 sql 查询都是 select 类型。但是,更改表的查询会遇到异常。引号和逗号,名称都经过彻底检查,如其他论坛所述,但它们都很好。所以我们试图捕捉异常,但情况变得更糟。它实际上显示“添加记录”,但没有。而是在tomcat7.exe cmd 提示符中出现相同的错误消息。我使用jdk1.6.0_21这是我的代码的副本。希望有朋友帮忙:

<%@ page language="java" import="java.sql.*" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    Class.forName(driver);
    Connection con=null;

    Statement stmt=null;
    try
    {
        String url="jdbc:odbc:Prison";
        con=DriverManager.getConnection(url);
        stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
    if(request.getParameter("action")!=null)
    {
        try
        {
        int PNo=Integer.parseInt(request.getParameter("PNo"));
        String PName=request.getParameter("PName");
        int Age=Integer.parseInt(request.getParameter("Age"));
        String Address=request.getParameter("Address");
        int Height=Integer.parseInt(request.getParameter("Height"));
        int Weight=Integer.parseInt(request.getParameter("Weight"));
        String Health=request.getParameter("Health");
        String Possessions=request.getParameter("Possessions");
        String Occupation=request.getParameter("Occupation");
        String Case=request.getParameter("Case");
        String Sentence=request.getParameter("Sentence");
        String From=request.getParameter("From");
        String To=request.getParameter("To");
        String Parole=request.getParameter("Parole");
        System.out.println(PNo); 
        String s="insert into Nominal values('" +PNo+ "','"+Possessions+ "','" +Occupation+ "','" +Case+ "','" +Sentence+ "','" +From+ "','" +To+ "','" +Parole+ "')";
        stmt.executeUpdate(s);
        }
        catch(SQLException e)
        {
        System.out.println(e.getMessage());
        }
%>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success!</title>
</head>
<body>
<h1>Record Added</h1>
<a href="addp.html">Back</a>
</body>
</html>
<%  

        stmt.close();
        con.close();
    }

%>

请帮忙。

4

1 回答 1

0

您缺少 INSERT INTO 语句中的字段,因此 INSERT 查询假定您正在为每个字段传递一个值,其顺序与它们在表中出现的顺序相同。

您的 INSERT INTO 语句需要如下所示:

String s="INSERT INTO Nominal (Field1, Field2, Field3) VALUES ('" + Value1 + "', '" + Value2 + "', '" + Value3 + "')"  
于 2011-06-04T11:49:51.050 回答