java - Counting rows sql table with jdbc -
i have trouble counting rows of sqltable. using query in ms studio works fine when try in java sql exception:
connection con = null; statement stat = null; int result = 0; int i; try { con = drivermanager.getconnection("jdbc:sqlserver://localhost:1433;user=sa;password=123456; databasename=rpa;"); stat = con.createstatement(); result = stat.executeupdate("select count(*) rpa_users"); }catch (sqlexception e) { system.out.println("cannot connect!"); e.printstacktrace(); } { if (con != null) { try { con.close(); } catch (sqlexception e) { e.printstacktrace(); } } } return result;
could explain me i'm doing wrong? in advance
edit: nevermind found guys, usual help! solution if interested:
result = stat.executequery("select count(*) /tablename/"); result.next(); rowcount = result.getint(1);
use executequery
instead of executeupdate
issuing database read queries
result = stat.executequery("select count(*) rpa_users");
aside: consider using prepared statements
Comments
Post a Comment