Home > Enterprise >  this is my code and my problem is: java.sql.SQLException: No value specified for parameter 1
this is my code and my problem is: java.sql.SQLException: No value specified for parameter 1

Time:11-07

public boolean findCongDan(String socancuoc) throws SQLException { CongDan congdan = new CongDan();

    openDatabase();
    
    String sqlFind = "Find From CongDan Where SoCanCuoc = ?";
    PreparedStatement stmt = jdbcConnection.prepareStatement(sqlFind); 
    ResultSet rs = stmt.executeQuery();
     while(rs.next()) {
        stmt.setString(1, socancuoc);
        stmt.setString(2, congdan.getHoTen());            
        stmt.setString(3, congdan.getDiaChi());    
        stmt.setString(4, congdan.getGioiTinh());  
        stmt.setString(5, congdan.getNgaySinh());  
        stmt.setString(6, congdan.getDanToc());  
        stmt.setString(7, congdan.getTonGiao()); 
        
        stmt.close();
        }
    closeDatabase();
    int rowFind = stmt.executeUpdate();
    
    return rowFind>0;
}

CodePudding user response:

Every ? in the query string needs to be provided in the stmt.executeQuery():

ResultSet rs = stmt.executeQuery("search value");

effectively making the query like this:

Find From CongDan Where SoCanCuoc = 'search value';

CodePudding user response:

You didn't provide parameters for your query before executing it. You have to call setString on your prepared statement before calling executeQuery

  •  Tags:  
  • java
  • Related