java.sql.SQLException:参数值无效:java.io.NotSerializableException
这是我的SQL连接类代码,在这里我得到了错误java.sql.SQLException:参数值无效:java.io.NotSerializableException,java,mysql,jdbc,struts2,model-driven,Java,Mysql,Jdbc,Struts2,Model Driven,这是我的SQL连接类代码,在这里我得到了错误 public class SqlConnection { static Connection con; ....... static public ResultSet getData(String sql, List<LogModel> alist) throws ClassNotFoundException, SQLException { PreparedStatement ps
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;
}
}
我确信PreparedStatement不知道如何在LogModel上设置Object。我的建议是将其分解为SQL原语,以便它知道如何处理它 您的代码需要大量的工作。您没有正确关闭JDBC资源。你的LogAction类毫无意义。这段代码有很多错误。我会担心你的应用程序。修改你的代码以
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;
}
将model对象设置为prepared语句的参数是没有意义的。*ModelDriven是否有堆栈跟踪?当程序试图序列化未实现SerializableLogModel serializable的类的对象时,会引发此异常?@JoachimIsaksson nopeI在没有泛型的情况下做了这件事,然后工作正常……使用此方法,我无法将此方法用于其他查询。@manindersingh我不知道其他查询,您没有发布。我想使用getData作为LogAction类的通用方法。它的通用性如何?如果要将非基本对象设置为查询,则需要一个类似hibernate的持久层。由于不同的sql查询,您无法使其通用,请使用DAO模式。当我使用List而不是List,以及使用alist.addlog.getUsername、alist.addlog.getPassword而不是alist.addlog时,一切正常。
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;
}