Database question
-
Hi folks! this is the code thats giving me problems.
try{
ResultSet rs = Main.dbase.search("select * from myTable");
//rs.next()
while(rs.next())
{
cmb.addItem(rs.getString(1).trim());
}
rs.close();
} catch (Exception ex) {System.out.println(ex);}here dbase is an object executing
Statement.executeQuery
, and cmb is JComboBox. the problem is that even though the table has 10 rows, i get only single row in cmb. if I don't comment out the firstrs.next()
, i get the 2nd row in cmb. i tried to track the value ofrs.next()
and found that afterwhile(rs.next())
it gives outfalse
, always. My target is to get all the 10 values in cmb.TheMrProgrammer http://www.icbse.com/2009/funny-exam-answers-school-students http://download.cnet.com/TheCalcMan/3000-2094\_4-10958266.html
-
Hi folks! this is the code thats giving me problems.
try{
ResultSet rs = Main.dbase.search("select * from myTable");
//rs.next()
while(rs.next())
{
cmb.addItem(rs.getString(1).trim());
}
rs.close();
} catch (Exception ex) {System.out.println(ex);}here dbase is an object executing
Statement.executeQuery
, and cmb is JComboBox. the problem is that even though the table has 10 rows, i get only single row in cmb. if I don't comment out the firstrs.next()
, i get the 2nd row in cmb. i tried to track the value ofrs.next()
and found that afterwhile(rs.next())
it gives outfalse
, always. My target is to get all the 10 values in cmb.TheMrProgrammer http://www.icbse.com/2009/funny-exam-answers-school-students http://download.cnet.com/TheCalcMan/3000-2094\_4-10958266.html
Have a go at this:
// you need to define the Statement outside of
// the try so it can be cleaned up in finally
Statement statement = null;
try {
// create and execute the query
// rs is not yet pointing to anything
statement = Main.dbase.createStatement();
ResultSet rs = statement.executeQuery("select * from myTable");
while (rs.next()) {
cmb.addItem(rs.getString(1).trim());
}
} catch (SQLException e ) {
System.out.println(ex);
} finally {
if (statement != null) {
statement.close();
}
}
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Have a go at this:
// you need to define the Statement outside of
// the try so it can be cleaned up in finally
Statement statement = null;
try {
// create and execute the query
// rs is not yet pointing to anything
statement = Main.dbase.createStatement();
ResultSet rs = statement.executeQuery("select * from myTable");
while (rs.next()) {
cmb.addItem(rs.getString(1).trim());
}
} catch (SQLException e ) {
System.out.println(ex);
} finally {
if (statement != null) {
statement.close();
}
}
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Hi folks! this is the code thats giving me problems.
try{
ResultSet rs = Main.dbase.search("select * from myTable");
//rs.next()
while(rs.next())
{
cmb.addItem(rs.getString(1).trim());
}
rs.close();
} catch (Exception ex) {System.out.println(ex);}here dbase is an object executing
Statement.executeQuery
, and cmb is JComboBox. the problem is that even though the table has 10 rows, i get only single row in cmb. if I don't comment out the firstrs.next()
, i get the 2nd row in cmb. i tried to track the value ofrs.next()
and found that afterwhile(rs.next())
it gives outfalse
, always. My target is to get all the 10 values in cmb.TheMrProgrammer http://www.icbse.com/2009/funny-exam-answers-school-students http://download.cnet.com/TheCalcMan/3000-2094\_4-10958266.html
There are two parts. 1. Database code 2. Gui code. It seems apparent that you are in fact getting 10 rows from the database. Your statements about the commented rs.next() and your implicit comments about it. If you are not sure the verify it using a debugger or System.out.println (do NOT use gui code.) Consequently that means the problem is with your GUI code and not the database code. You can replace your database code entirely with a for/next loop that adds 10 fixed values (such as "xxx") to the combo box. Until that code works it is pointless to attempt to do anything with the database code. Creating a small complete example with JUST the gui code (no database code) might help to determine what the problem is.
-
It fixes the code...
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
It fixes the code...
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett