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

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -