0

我必须读取一个 xml 文件,以表的形式显示其内容,并且需要将数据存储在数据库表中。首先,我尝试在我的 jsp 中仅使用 jstl1.2 标签显示数据。它工作正常。但是当我尝试实现一些代码来插入记录时,我收到了这个错误..

 Can't infer the SQL type to use for an instance of org.apache.taglibs.standard.tag.common.xml.JSTLNodeList. Use setObject() with an explicit Types value to specify the type to use.

我的 Jsp 代码如下:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<sql:setDataSource var="dataSource" driver="org.postgresql.Driver"
     url="jdbc:postgresql://localhost:5432/postgres" user="postgres" password="shail"
     scope="session" />

<html>
  <head>
    <title>cust_xml</title>
  </head>
  <body>

    <h1>Customer Information</h1>

    <c:import var="xmlFile" url="cust.xml"  charEncoding="UTF-8"/>
    <x:parse var="myDoc" xml="${xmlFile}" />

    <table border="1">
      <tr>
        <th>Customer Number</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Street</th>
        <th>City</th>
        <th>State</th>
        <th>Zipcode</th>
        <th>Balance</th>
      </tr>
      <x:forEach select="$myDoc/CUSTOMERS/Customer" var="cust">
        <tr>
             <td>
              <x:out select="$cust/CustNo" />
              <x:set var="custno" select="$cust/CustNo" scope="session"/>
             </td>
             <td>
              <x:out select="$cust/CustFirstName" />
              <x:set var="custfn" select="$cust/CustFirstName" scope="session"/>
             </td>
             <td>
              <x:out select="$cust/CustLastName" />
              <x:set var="custln" select="$cust/CustLastName" scope="session"/>
             </td>
             <td>
              <x:out select="$cust/CustStreet" />
              <x:set var="custst" select="$cust/CustStreet" scope="session"/>
             </td>
             <td>
              <x:out select="$cust/CustCity" />
              <x:set var="custcity" select="$cust/CustCity" scope="session"/>
             </td>
             <td>
              <x:out select="$cust/CustState" />
              <x:set var="custstate" select="$cust/CustState" scope="session"/>
             </td>
             <td>
              <x:out select="$cust/CustZip" />
              <x:set var="custz" select="$cust/CustZip" scope="session"/>
             </td>
             <td>
              <x:out select="$cust/CustBal" />
              <x:set var="custbal" select="$cust/CustBal" scope="session"/>
             </td>
        </tr>
      </x:forEach>
    </table>

    <sql:update dataSource="${dataSource}" var="updatedTable">
        INSERT INTO data  VALUES (?,?,?,?,?,?,?,?);
        <sql:param value="${custno}" />
        <sql:param value="${custfn}" />
        <sql:param value="${custln}" />
        <sql:param value="${custst}" />
        <sql:param value="${custcity}" />
        <sql:param value="${custstate}" />
        <sql:param value="${custz}" />
        <sql:param value="${custbal}" />
    </sql:update>
    <c:if test="${updatedTable>=1}">
     <font size="5" color='green'> Congratulations ! Data inserted successfully.</font>
    </c:if>

  </body>
</html>

请提供您的建议,我应该使用哪个标签来设置插入查询参数的值?如何获取要在查询参数中设置的 xml 元素数据?感谢和问候

4

2 回答 2

0

我努力解决我的问题,但不幸的是无法获得任何帮助。在谷歌搜索时,我得到了一些基本的想法来解决这个问题。我试过这个

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<sql:setDataSource var="dataSource" driver="org.postgresql.Driver"
     url="jdbc:postgresql://localhost:5432/postgres" user="postgres" password="shail"
     scope="session" />

<html>
  <head>
    <title>cust_xml</title>
  </head>
  <body bgcolor="skyblue">

    <h1 align="center">Customer Information Stored In XMl File</h1>

    <c:import var="xmlFile" url="cust.xml"  charEncoding="UTF-8"/>
    <x:parse var="myDoc" xml="${xmlFile}" />

    <table border="1" cellspacing="1" cellpadding="1" bgcolor="pink" align="center">
      <tr>
        <th>Customer Number</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Street</th>
        <th>City</th>
        <th>State</th>
        <th>Zipcode</th>
        <th>Balance</th>
      </tr>
      <x:forEach select="$myDoc/CUSTOMERS/Customer" var="cust">
        <tr>
             <td>
              <c:set var="custno" >
              <x:out select="$cust/CustNo" />
              </c:set>
              <x:out select="$cust/CustNo" />
             </td>
             <td>
              <c:set var="custfn" >
              <x:out select="$cust/CustFirstName" />
              </c:set>
              <x:out select="$cust/CustFirstName" />
             </td>
             <td>
              <c:set var="custln" >
              <x:out select="$cust/CustLastName" />
              </c:set>
              <x:out select="$cust/CustLastName" />
             </td>
             <td>
              <c:set var="custst" >
              <x:out select="$cust/CustStreet" />
              </c:set>
              <x:out select="$cust/CustStreet" />
             </td>
             <td>
              <c:set var="custcity" >
              <x:out select="$cust/CustCity" />
              </c:set>
              <x:out select="$cust/CustCity" />
             </td>
             <td>
              <c:set var="custstate" >
              <x:out select="$cust/CustState" />
              </c:set>
              <x:out select="$cust/CustState" />
             </td>
             <td>
              <c:set var="custz" >
              <x:out select="$cust/CustZip" />
              </c:set>
              <x:out select="$cust/CustZip" />
             </td>
             <td>
              <c:set var="custbal" >
              <x:out select="$cust/CustBal" />
              </c:set>
              <x:out select="$cust/CustBal" />
             </td>
        </tr>
        <sql:update dataSource="${dataSource}" var="updatedTable">
        INSERT INTO data  VALUES (?,?,?,?,?,?,?,?);
        <sql:param value="${custno}" />
        <sql:param value="${custfn}" />
        <sql:param value="${custln}" />
        <sql:param value="${custst}" />
        <sql:param value="${custcity}" />
        <sql:param value="${custstate}" />
        <sql:param value="${custz}" />
        <sql:param value="${custbal}" />

    </sql:update>
    <c:set var="rowcount" value="${updatedTable+rowcount}"/>
      </x:forEach>

    <c:if test="${rowcount>=1}">
      <tr>
       <td colspan="8" align="center">
     <font size="5" color='red' align="center">
     Congratulations ! <c:out value="${rowcount}" /> Rows inserted successfully  in DB Table.</font>
       </td>
      </tr>
    </c:if>
    </table>
  </body>
</html>

但我得到了这个错误:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: 
        INSERT INTO data  VALUES (?,?,?,?,?,?,?,?);
ERROR: column "balance" is of type integer but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.

正如我的表余额(最后一列)是整数类型。当我用谷歌搜索时,我发现 jstl 标签会自动处理投射。但它显示错误为by default <c:set > tag places value for variable as string. 任何想法?有什么帮助吗?有什么建议吗?请告诉我。

于 2013-02-27T09:18:41.317 回答
0

您可以附上?withCAST函数并仍然使用setObject()方法。
让我举个例子在MySQL中存储 IP 地址时,我的查询是这样的

INSERT INTO log (ip,logged_on) VALUES(inet6_aton(?),no());

PS:inet6_aton()对于 MySQL 5.6+ 否则使用inet4_aton()或只是inet_aton()

现在我转向Postgres,它没有inet_aton功能,而是inet作为一种数据类型。不幸的是,没有setInet()方法,所以我会这样做

INSERT INTO log (ip,looged_on) VALUES(CAST(? AS INET),now());

有了这个,我可以使用setString()or同时setObject()传递参数JDBCJSTL

插入数据值 (?,?,?,?,?,?,?,?);

由于您没有像我在示例中那样提及列名,因此我不知道需要对哪一列进行类型转换,但您可以尝试这样编写

 INSERT INTO data VALUES (CAST(? AS INTEGER),?,?,?,?,?,?,?);

我只是假设您表格的第一列,balance请根据表格中列的位置为适当的参数执行此操作balance

于 2018-01-16T11:12:47.763 回答