从数据库中删除行时出现无效的SQL数据类型错误?

时间:2013-11-27 14:16:39

标签: html sql jsp jdbc

我正在尝试使用JSP文件从MS Access数据库中删除一行。但我在下面收到以下错误。谁知道怎么修它。谢谢。任何帮助将非常感激。

  

org.apache.jasper.JasperException:javax.servlet.ServletException:   javax.servlet.jsp.JspException:DELETE FROM Employees WHERE empId =   ? :[Microsoft] [ODBC Microsoft Access驱动程序]无效的SQL数据类型     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)     javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

     

根本原因

     

javax.servlet.ServletException:javax.servlet.jsp.JspException:
  从员工中删除WHERE empId =?


代码:JSP文件。

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<html>
<head>
<title>DELETE Operation</title>
</head>
<body>

<sql:setDataSource
    var = "bookdB"
    scope = "session"
    driver = "sun.jdbc.odbc.JdbcOdbcDriver"
    url = "jdbc:odbc:bookdB"
/>

<c:set var="empId" value="${param['empId']}"/>

<sql:update dataSource="${bookdB}" var="count">
  DELETE FROM Employees WHERE empId = ?
  <sql:param value="${empId}" />
</sql:update>

<sql:query dataSource="${bookdB}" var="result">
   SELECT * from Employees;
</sql:query>

<table border="1" width="100%">
<tr>
   <th></th>
   <th>Emp ID</th>
   <th>First Name</th>
   <th>Last Name</th>
   <th>Age</th>
</tr>

<c:forEach var="row" items="${result.rows}">
<tr>
   <td><a href="?empId=[empId to remove]">Delete</a></td>
   <td><c:out value="${row.empId}"/></td>
   <td><c:out value="${row.first}"/></td>
   <td><c:out value="${row.last}"/></td>
   <td><c:out value="${row.age}"/></td>
</tr>
</c:forEach>
</table>

</body>
</html>

使用以下答案更新后仍然出现错误。任何人有任何想法???,你能帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:1)

我认为锚标签有问题。您正在传递[empId to remove]而不是有效的empId

更改

<a href="?empId=[empId to remove]">Delete</a>
                    ↑  

要,

<a href="?empId=${row.empId}">Delete</a>  

来自stacktrace

[Microsoft][ODBC Microsoft Access Driver]Invalid SQL data type  

[empId to remove]是无效的SQL数据类型。

修改

要看的东西

  1. 您正在提交表单,但没有submit button
  2. 锚标记<a href="?empId=[empId to remove]">Delete</a>这是它将要调用的操作吗?
  3. 您正在循环打印表单
  4. 创建两个JSP个文件,一个用于显示所有带有删除链接view_all.jsp和第二个reserve.jsp的记录,如form标记中所述。

    view_all.jsp

    <sql:setDataSource
      var = "bookdB"
      scope = "session"
      driver = "sun.jdbc.odbc.JdbcOdbcDriver"
      url = "jdbc:odbc:bookdB"
    />
    
    <sql:query dataSource="${bookdB}" var="result">
       SELECT * from Employees;
    </sql:query>
    
    <table border="1" width="100%">
    <tr>
      <th></th>
      <th>Emp ID</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Age</th>
    </tr>
    
    <c:forEach var="row" items="${result.rows}">
      <tr>
       <td><a href="reserve.jsp?empId=${row.empId}">Delete</a></td>
       <td><c:out value="${row.empId}"/></td>
       <td><c:out value="${row.first}"/></td>
       <td><c:out value="${row.last}"/></td>
       <td><c:out value="${row.age}"/></td>
      </tr>
     </form>
    </c:forEach>
    </table>
    

    reserve.jsp

    <sql:setDataSource
      var = "bookdB"
      scope = "session"
      driver = "sun.jdbc.odbc.JdbcOdbcDriver"
      url = "jdbc:odbc:bookdB"
    />
    
    //get the empId send from view_all.jsp 
    <c:set var="empId" value="${requestScope.empId}"/> 
    
    <sql:update dataSource="${bookdB}" var="count">
      DELETE FROM Employees WHERE empId = ?
      <sql:param value="${empId}" />
    </sql:update>