2017-04-01 63 views

回答

1

你應該給你設置Paramettres後執行您的Stetement。翻轉訂單,你應該沒問題:

PreparedStatement ps = cnx.getConnection().prepareStatement(req); 
ps.setInt(1,id); 
ResultSet rs = ps.executeQuery(); 
0

您正試圖執行查詢後設置綁定值,而不是之前的那樣:

public List<ProjectUser> findProjectUserId(int id){ 
    List <ProjectUser> users= new ArrayList<>(); 
    ProjectUser projectuser= null; 
    try { 
     String req="select * from projectuser where project=?"; 
     PreparedStatement ps= cnx.getConnection().prepareStatement(req); 
     ResultSet rs= ps.executeQuery(); 
     ps.setInt(1,id); 
     while (rs.next()){ 
      projectuser= new ProjectUser(rs.getInt(1), rs.getInt(2), rs.getInt(3)); 
      users.add(projectuser); 
     } 
    } catch (SQLException ex){ 
     ex.printStackTrace(); 
    } 
    return users; 
} 
+0

謝謝大家缺乏睡眠問題:) –

0

在設置PreparedStatement的參數之前,您正在執行查詢。這就是爲什麼你的代碼給予異常。聲明的順序應該是

ps.setInt(1,id); 
ResultSet rs= ps.executeQuery(); 
相關問題