2013-06-20 76 views
1

這是我的SQL連接類的代碼,我收到錯誤消息。java.sql.SQLException:參數值無效:java.io.NotSerializableException

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

* ModelDriven 是否 – Maninder

+0

我們可以有堆棧跟蹤嗎?當程序試圖序列化一個沒有實現Serializable的類的對象時拋出這個異常 –

+0

LogModel是可序列化的嗎? –

回答

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

設置模型對象準備好的語句的參數使得非感。

+0

通過使用此我不能使用此方法用於其他查詢。 – Maninder

+0

@manindersingh我不知道還有什麼其他疑問,你沒有發佈。 –

+0

我想使用getData()作爲LogAction類的通用方法。 – Maninder

0

我確定PreparedStatement不知道如何在LogModel上設置對象。我的建議是將其分解爲SQL原語,以便知道如何處理它。

您的代碼需要很多工作。您沒有正確關閉JDBC資源。你的LogAction類沒有意義。這一小部分代碼很有問題。我會擔心你的應用程序。

+0

當我使用列表而不是列表和alist.add(log.getUsername),alist.add(log.getPassword)而不是alist.add(日誌),然後一切工作正常。 – Maninder

+0

所以接受答案... – duffymo

+0

好酷感謝人 – Maninder