开发者

JSP-Access jdbc error: insert into statement- "Number of query values and destination fields are not the same"

My previous JSP files that read from the database 'Prison' had worked well. But my 'add.jsp' file that is supposed to insert values into the database shows this exception:

"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).

The sql queries used on the db so far has been of the select type. however, queries that change the table hit exceptions. quotes and comma, names have all been checked thoroughly, as specified in other forums, but they are all fine.So we tried to catch the exception and it became worse. it actually showed 'Record added', but did not. instead the same error message appears in tomcat7.exe cmd prompt. i use jdk1.6.0_21 an here's the copy of my code. hoping some friend shall help:

<%@ 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 Occupat开发者_开发知识库ion=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();
    }

%>

please help.


You are missing the fields in your INSERT INTO statement so the INSERT Query is assuming that you are passing in a value for every single field, in the same order that they appear in the table.

Your INSERT INTO statement needs to look something like this:

String s="INSERT INTO Nominal (Field1, Field2, Field3) VALUES ('" + Value1 + "', '" + Value2 + "', '" + Value3 + "')"  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜