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
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
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.