从数据库中提取信息时,Netbeans中的“executeQuery方法不能用于更新。”错误

问题描述 投票:-1回答:1

我的问题是关于一个Java Web程序,它搜索数据库并根据查询返回学生列表。代码如下: -

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

这里是包含我想要搜索学生的类别的字符串。但我收到以下错误: -

errrrr...executeQuery method can not be used for update.
java apache derby netbeans-8 javadb
1个回答
1
投票

要执行任何更新,请使用executeUpdate方法 executeQuery(String sql) 执行给定的SQL语句,该语句返回单个ResultSet对象。 execute(String sql) 执行给定的SQL语句,该语句可能返回多个结果。

有关更多信息,请查看Statement

© www.soinside.com 2019 - 2024. All rights reserved.