无法解析方法executeQuery();

时间:2019-05-17 03:47:32

标签: java sqlite

我不知道为什么,但是当我尝试使用方法“ executeQuery()”时,inteliJ抛出了它的手,并告诉我“无法解析方法executeQuery()。

我仔细检查了我是否已导入所有与sql有关的软件包。我看过其他似乎相关的文章。当我尝试“ Shift” +“ Enter”时,它给了我一些似乎没有明显解决方案的编辑设置选项

package Student_Tech_Services_Tracking_Log;

import java.sql.*;
import java.util.ArrayList;

public class TicketDB {

    private static final String url = "jdbc:sqlite:products.db";

    protected ArrayList<Ticket> loadTickets(){

        final String srchSql = "select * from tickets";

        try(Connection conect = DriverManager.getConnection(url);
            PreparedStatement prepState = conect.prepareStatement(srchSql)){

            ResultSet tikRs = srchSql.executeQuery();
        }catch (SQLException e){
            System.out.println(e);
        }

        return new ArrayList<>();
    }

预期结果是executeQuery()不应突出显示为错误。

2 个答案:

答案 0 :(得分:1)

看到您的代码后: executeQuery()不是String的方法,而是PrepareStatement的方法,所以要这样做:

public class TicketDB {

    private static final String url = "jdbc:sqlite:products.db";

    protected ArrayList<Ticket> loadTickets(){

        final String srchSql = "select * from tickets";

        try(Connection conect = DriverManager.getConnection(url);
            PreparedStatement prepState = conect.prepareStatement(srchSql)){

            ResultSet tikRs = prepState.executeQuery();

while(tikRs.next()){  
    System.out.println(tikRs.getInt(1)+" "+tikRs.getString(2));// here put your get logic..
    }
        }catch (SQLException e){
            System.out.println(e);
        }

        return new ArrayList<>();
    }

答案 1 :(得分:0)

ResultSet tikRs = srchSql.executeQuery();

应该是:

ResultSet tikRs = prepState.executeQuery();

executeQuery()是Statements而不是String的方法。

您可以在doc

中进行了解