java.sql.SQLException: Invalid argument value: java.io.NotSerializableException

蓝咒 提交于 2019-12-08 06:03:56

问题


This is my code of SQL Connection class where I am getting error.

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;
    }
}

Here is the LogAction class where i am calling this getdata() function.

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;
    }
}

回答1:


Modify your code to

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;
}

setting model object to the parameter of the prepared statement makes non sense.




回答2:


I'm sure that the PreparedStatement has no idea how to setObject on your LogModel. My advice would be to decompose that into SQL primitives so it'll know what to do with it.

Your code needs a lot of work. You aren't closing JDBC resources properly. Your LogAction class makes no sense. Lots wrong with this little bit of code. I'd be worried about your app.



来源:https://stackoverflow.com/questions/17212949/java-sql-sqlexception-invalid-argument-value-java-io-notserializableexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!