-1

My question is about a Java Web program which search the database and returns the list of students as per the query The code is below:-

public class StudentDAO {
        public List<Student> getStudent(String s){
             List<Student> lst=new ArrayList<>();
             try{
                Context ctx =new InitialContext();
                DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/test");
                Connection con=ds.getConnection();
                Statement st = con.createStatement(); 
                ResultSet res=st.executeQuery("select * from student where "+s);
                while(res.next())
                {
                    lst.add(new Student(res.getString(1),res.getString(2),res.getString(3),res.getString(4)));               
                }
                res.close();
                con.close();
        }
        catch(Exception e){
            System.out.println("errrrr..."+e.getMessage());
        }
        return lst;
    }
}

Here 's' is the string containing categories based on which I want to search students. But I'm getting the following error :-

errrrr...executeQuery method can not be used for update.
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    [Absolutely required reading](https://stackoverflow.com/q/1582161/335858). Forget about solving this specific problem. Your code has a bigger problem to solve. As far as learning the specifics goes, print `s` when you get the exception to see what happens. – Sergey Kalinichenko Dec 31 '17 at 13:08
  • You should have a look a sample CRUD examples. – Rajat Dec 31 '17 at 13:15

1 Answers1

1

To perform any update executeUpdate method is used
executeQuery(String sql)
Executes the given SQL statement, which returns a single ResultSet object.

execute(String sql)
Executes the given SQL statement, which may return multiple results.

for more information have a look at Statement

Rajat
  • 2,467
  • 2
  • 29
  • 38