java - variable in web service operation not being updated -
i creating simple web service example java. have variable num initialized 0, not able retain value @ end. value should incremented 1 per db have.
here web service operation:
@webmethod(operationname = "getnewid") public string getnewid(@webparam(name = "newid") string txt2) { connection connection = null; resultset resultset = null; statement statement = null; int num=0; try { class.forname("org.sqlite.jdbc"); connection = drivermanager.getconnection("jdbc:sqlite:src/java/db/test.sqlite"); statement = connection.createstatement(); resultset = statement.executequery("select id logindetails id = '"+txt2+"'"); while (resultset.next()) { //system.out.println("your id "+ resultset.getstring("id")); num++; } } catch (exception e) { system.out.println("you had exception"); e.printstacktrace(); } { try { resultset.close(); statement.close(); connection.close(); } catch (exception e) { e.printstacktrace(); } } if(num==0) { return "your id not match"; } else { return "welcome "+txt2; } }
in output getting
method returned java.lang.string : "your id not match"
for cases if part working , not else. when tried same method inside class working fine, not in case of web service operation
new code
@webmethod(operationname = "getnewid") public string getnewid(@webparam(name = "newid") string txt2) { string str = null; connection connection = null; resultset resultset = null; statement statement = null; int num=0; try { class.forname("org.sqlite.jdbc"); connection = drivermanager.getconnection("jdbc:sqlite:src/java/db/test.sqlite"); statement = connection.createstatement(); resultset = statement.executequery("select id logindetails id = '"+txt2+"'"); while (resultset.next()) { //system.out.println("your id "+ resultset.getstring("id")); num++; } } catch (exception e) { system.out.println("you had exception"); e.printstacktrace(); } { try { resultset.close(); statement.close(); connection.close(); } catch (exception e) { e.printstacktrace(); } } assert num == 0 || txt2 == null : str = "num "+num+"txt2 "+txt2; return str; /*if(num==0) { return "your id not match please try again or if new user please register"; } else { return "welcome "+txt2; }*/ }
in result getting value of str null.
ok tried ..
@webmethod(operationname = "getnewid") public string getnewid(@webparam(name = "newid") string txt2) { connection connection = null; resultset resultset = null; statement statement = null; int num=0; try { class.forname("org.sqlite.jdbc"); connection = drivermanager.getconnection("jdbc:sqlite:src/java/db/test.sqlite"); statement = connection.createstatement(); resultset = statement.executequery("select id logindetails id = '"+txt2+"'"); while (resultset.next()) { num++; } return resultset.tostring(); } catch (exception e) { system.out.println("you had exception"); e.printstacktrace(); return "you had exception"; } }
i getting output
java.lang.string : "you had exception"
when working fine outside web service operation why showing exception here... , how can correct ?
- check exceptions
- debug code via debugger, assert statements or if need
system.out.println
statements. - if able, run query against db first , see if expected result.
- make sure class driver being loaded loads properly. make sure lib in class path.
regards rentius
Comments
Post a Comment