java.sql.SQLException:无效的参数值:java.io.NotSerializableException

时间:2013-06-20 11:53:16

标签: java mysql jdbc struts2 model-driven

这是我的SQL Connection类代码,我收到错误。

public class SqlConnection {
    static Connection con;
.......

    static public ResultSet getData(String sql, List<LogModel> alist)
            throws ClassNotFoundException, SQLException {
        PreparedStatement pst = con.prepareStatement(sql);
        if (alist != null) {
            for (int i = 1; i <= alist.size(); i++) {
                pst.setObject(i, alist.get(i-1)); //Exception at this Line
            }
        }
        ResultSet rs = pst.executeQuery();
        return rs;
    }
}

以下是LogAction类,我称之为getdata()函数。

public class LogAction extends ActionSupport implements ModelDriven<LogModel>, Preparable {

    LogModel log = null;
    List<LogModel> alist = null;
    public static final String FAILURE = "failure";

    @Override
    public void prepare() throws Exception {
        log = new LogModel();
        alist = new ArrayList<LogModel>();
    }

    @Override
    public LogModel getModel() {
        return log;
    }

    public String login() throws ClassNotFoundException, SQLException {
        String sql = "Select username,password from registration where username=? and password=?";
        alist.add(log);
        System.out.println(alist);
        ResultSet rs = SqlConnection.getData(sql, alist);
        if (rs.next()) {
            return SUCCESS;
        } else
            return FAILURE;
    }
}

2 个答案:

答案 0 :(得分:0)

我确信PreparedStatement不知道如何在LogModel上设置setObject。我的建议是将其分解为SQL原语,以便它知道如何处理它。

您的代码需要大量工作。您没有正确关闭JDBC资源。你的LogAction类毫无意义。这一小段代码有点不对劲。我会担心你的应用程序。

答案 1 :(得分:0)

将代码修改为

static public ResultSet getData(String sql, String username, String password)
        throws ClassNotFoundException, SQLException {
    PreparedStatement pst = con.prepareStatement(sql);
    pst.setString(1, username);
    pst.setString(2, password);
    ResultSet rs = pst.executeQuery();
    return rs;
}

将模型对象设置为预准备语句的参数是没有意义的。