java - How to show "count" MySQL query into JTextField? -


right now, i'm stuck problem: want show total amount of songs in database inside gui (through jtextfield).

this got far:

txttotalsongs = new jtextfield(); txttotalsongs.addactionlistener(new actionlistener() {     public void actionperformed(actionevent arg0) {         try {             string q = "select count (title) songs";             preparedstatement ps=conn.preparestatement(q);              ps.setint(1, 20);              resultset rs = ps.executequery();              while(rs.next())             {                 txttotalsongs.settext(string.valueof("title"));             }          } catch (exception e) {             // todo: handle exception         }      } }); txttotalsongs.setbounds(591, 458, 86, 20); contentpane.add(txttotalsongs); 

instead of while loop, try

if(rs.first())     xttotalsongs.settext(rs.getstring("title")); 

rs.first() returns true (and moves pointer first) if there row in result set. getstring returns value of column "title" in first row.

since there should 1 row "count" (at least guess so), should work.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -