在jsp中抛出的数据类型异常

时间:2016-01-07 09:30:50

标签: jquery json ajax jsp servlets

我正在编写一个webapp,其中有一个使用Ajax生成的表。这些数据实际上是从数据库中提取的。以下是我的代码。

的index.jsp

<html>
<head>
</head>
<body>
    <marquee>
        <h1>This is an example of ajax</h1>
    </marquee>
    <form name="vinform">
        Enter id:<input type="button" id="somebutton" value="Click Me">
    </form>

    <span id="somediv"> </span>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script type="text/javascript" src="tableGenerator.js"></script>
</body>
</html>

tableGenerator.js

$('#somebutton').click(
        function() {
            $.getJSON('Controller', function(searchList) {
                var $table = $('<table>').appendTo($('#somediv'));
                $.each(searchList, function(index, userBean) {
                    $('<tr>').appendTo($table).append(
                            $('<td>').text(userBean.caseNumber)).append(
                            $('<td>').text(userBean.caseOwner)).append(
                            $('<td>').text(userBean.status)).append(
                            $('<td>').text(userBean.issue)).append(
                            $('<td>').text(userBean.reason)).append(
                            $('<td>').text(userBean.dateOpened));
                });
            });
        });

UserBean.java

package org.bean;

public class UserBean {
    private int age;
    private String caseOwner, status, issue, reason, dateOpened, caseNumber, resolution, finalStatus, startDate,
            endDate;
    private Double totalTimeTaken;

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public String getCaseNumber() {
        return caseNumber;
    }

    public void setCaseNumber(String caseNumber) {
        this.caseNumber = caseNumber;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCaseOwner() {
        return caseOwner;
    }

    public void setCaseOwner(String caseOwner) {
        this.caseOwner = caseOwner;
    }

    public String getStatus() {
        return status;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getIssue() {
        return issue;
    }

    public void setIssue(String issue) {
        this.issue = issue;
    }

    public String getDateOpened() {
        return dateOpened;
    }

    public void setDateOpened(String dateOpened) {
        this.dateOpened = dateOpened;
    }

    public String getResolution() {
        return resolution;
    }

    public void setResolution(String resolution) {
        this.resolution = resolution;
    }

    public String getFinalStatus() {
        return finalStatus;
    }

    public void setFinalStatus(String finalStatus) {
        this.finalStatus = finalStatus;
    }

    public double getTotalTimeTaken() {
        return totalTimeTaken;
    }

    public void setTotalTimeTaken(Double totalTimeTaken) {
        this.totalTimeTaken = totalTimeTaken;
    }

    public UserBean() {

    }

    public UserBean(String caseNumber, String caseOwner, String issue, int age, String reason, String dateOpened,
            String status, String finalStatus, String resolution, String startDate, String endDate,
            Double totalTimeTaken) {
        this.caseNumber = caseNumber;
        this.caseOwner = caseOwner;
        this.issue = issue;
        this.reason = reason;
        this.age = age;
        this.dateOpened = dateOpened;
        this.status = status;
        this.resolution = resolution;
        this.finalStatus = finalStatus;
        this.startDate = startDate;
        this.endDate = endDate;
        this.totalTimeTaken = totalTimeTaken;
    }
}

Controller.java(Servlet的)

import java.io.IOException;
import java.util.ArrayList;

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 org.bean.UserBean;

import com.dao.DataDao;
import com.google.gson.Gson;

@WebServlet("/Controller")
public class Controller extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // String id = request.getParameter("val");

            DataDao dataDao = new DataDao();
            ArrayList<UserBean> list = dataDao.getFrameWork();
            String searchList = new Gson().toJson(list);
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(searchList);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

DBUtility.java

package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtility {
    private static Connection connection = null;

    public static Connection getConnection() throws Exception {
        if (connection != null)
            return connection;
        else {
            // Store the database URL in a string
            String userName = "sa";
            String password = "T!ger123";
            String url = "jdbc:sqlserver:XXXXXX;DatabaseName=TEST";

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            // set the url, username and password for the databse
            connection = DriverManager.getConnection(url, userName, password);
            return connection;
        }
    }
}

DataDao.java

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.bean.UserBean;

public class DataDao {
    private Connection connection;

    public DataDao() throws Exception {
        connection = DBUtility.getConnection();
    }

    public ArrayList<UserBean> getFrameWork() throws SQLException {
        ArrayList<UserBean> list = new ArrayList<UserBean>();
        PreparedStatement ps = null;
        try {

            ps = connection.prepareStatement("select * from statusTable");
            // ps.setString(1, frameWork);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                UserBean userBean = new UserBean();
                userBean.setCaseNumber(rs.getString(1));
                userBean.setCaseOwner(rs.getString(2));
                userBean.setStatus(rs.getString(3));
                userBean.setIssue(rs.getString(4));
                userBean.setReason(rs.getString(5));
                userBean.setDateOpened(rs.getString(6));
                userBean.setAge(rs.getInt(7));
                list.add(userBean);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;
    }
}

我是使用这种Ajax / Jquery / JSON技术的新手。我已经按照@BalusC的教程http://0.30000000000000004.com/进行了操作。

此处我有2个查询。

  1. 当我将下面的区块放在<head>时,它不起作用。

    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script type="text/javascript" src="tableGenerator.js"></script>
    
  2. 当我在<body>中放置相同的上述代码时,它会向我抛出以下异常。

    将varchar值转换为JDBC数据类型INTEGER时发生错误。

  3. 当我做了sp_help statusTable时(这是sqlserver查询相当于Oracle&#39; s desc statusTable)。我得到的结果如下面的屏幕截图所示。

    How to use Servlets and Ajax?

    无法理解数据类型不匹配的位置。

    请让我知道我哪里出错了以及如何解决。

    由于

1 个答案:

答案 0 :(得分:1)

SQL服务器列出的列顺序与JDBC结果集中的列顺序之间似乎不匹配。特别是,第7列似乎与Age列不对应。

要检查哪个列名与哪个列号对应,您可以使用结果集的元数据:

 ResultSetMetaData meta = rs.getMetaData();
 String name = meta.getColumnName(7);

它可能更明智:

  • 在查询中明确列出列名,而不是使用SELECT *;或
  • 通过指定列的名称而不是索引号来检索数据。

关于你的第一个问题,如果你把Javascript放在标题中,#somebutton clickhandler会在元素加载到DOM之前被实例化,因此它不起作用。