JDBC returning empty result set

前端 未结 14 1870
醉话见心
醉话见心 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 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.

提交回复
热议问题