JDBC returning empty result set

前端 未结 14 1810
醉话见心
醉话见心 2020-12-15 06:15

I\'m using JDBC for very simple database connectivity.

I have created my connection/statement and executed a query. I check the query object of the statement in the

相关标签:
14条回答
  • 2020-12-15 06:17

    I see a few pitfalls in your code, there are a few places where things can go wrong:

    First, use of regular statements. Use prepared statements so you won't have problems with SQL injection.

    Instead of

    statement = connection.createStatement();
    

    use

    statement = connection.prepareStatement(String sql);
    

    With this, your query becomes

    "select distinct group_name From group_members where username= ?"
    

    and you set username with

     statement.setString(1, username);
    

    Next, I don't like use of your myDB class. What if results is null? You're not doing any error checking for that in your public List<InterestGroup> getGroups() method.

    public void sendQuery(String query) seems to me like it shouldn't be void, but it should return a ResultSet. Also, search on the net for proper ways to do JDBC exception handling.

    Also, this line:

    new InterestGroup(results.getString("group_name"), myDB)
    

    Why do you have myDB as a parameter?

    I'd suggest adding more System.out.println statements in your code so you can see where things can go wrong.

    0 讨论(0)
  • 2020-12-15 06:20

    I had logged in to server using plsql client and while I tried to connect from my code.

    I was successfully connecting but there were no records in the result-set.

    Only after logging out from the server the result-set was populated.

    I agree with @dpsthree, Oracle should provide appropriate error/warning message.

    0 讨论(0)
  • 2020-12-15 06:22

    I then double checked the query (copied straight from debugger) on the database to make sure it returns data.

    I've had engineers with this problem demonstrate this verification in front of me. It turns out they were logged in with one database account in the program and with a different database account in the interactive SQL shell. [This was Oracle 8.]

    0 讨论(0)
  • 2020-12-15 06:25

    The most common is having multiple statements in the query:

    desc table;
    select * from sometable where etc.;
    

    For statements which do not return results, you have to use a different construct. Even this will cause the client to choke:

    select * from sometable where whatever;
    select * from sometable where something else;
    

    The two same-shaped result sets will biff the client.

    0 讨论(0)
  • 2020-12-15 06:27

    in my case the query which worked in sql developer didn't work in JAVA.

    *select * from table where process_date like '2014-08-06%'* (worked in sql developer)
    

    formating process_date to char helped to make it work in JAVA

    *select * from table where to_char(process_date) = '06-AUG-14'*
    
    0 讨论(0)
  • 2020-12-15 06:28

    Yeah, I had the same problem as the OP. It happens when you have two or more open connections with the database on the same user. For example one connection in SQL Developer and one connection in Java. The result is always an empty resultset.

    EDIT: Also, I noticed that it happens when you execute procedure or insert in the databse and you don't commit your transactions.

    0 讨论(0)
提交回复
热议问题