• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL Statement Not Working Properly

 
Peter Harvey
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the exact code for a function, where sections[1] is a JComboBox, and levels is a String which equals "1". The Statement and the ResultSet are global variables which work fine.
private void fillSectionCombo(String levels) throws SQLException
{
String query="Select Section from Section where Section like '" + levels + "*'";

statement=conn.createStatement();
results=statement.executeQuery(query);
boolean more=results.next();
while(more==true)
{
sections[1].addItem(results.getString("Section"));
more=results.next();
}
}

The query is actually ends up looking like this:
Select Section from Section where Section like '1*'
The table Section exists, and the field Section exists within it. There are also plenty of entries which begin with '1' (101, 102, etc).
What am I doing wrong? results.next() returns false. I just don't understand why.
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try 1% instead of 1*
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree.
I tend to cut the query out and run it in another BD client if it isn't working.
Heaps of times my problem has been in the sql and not the Java (but then my strength is Java not SQL )
Dave.
 
Peter Harvey
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys!
Yet another reason why JavaRanch rocks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic