java.sql.SQLException: Invalid cursor position -
i created simple application lets users purchase tickets. however, have run error whenever try purchase "n" number of tickets. now, know need crs.next()
statement while operating databases, have used number of times reason doesn't work here. gives me java.sql.sqlexception: invalid cursor position error.
this code snippet inserting ticket.
public void buyticket(int id,int n,int price) throws sqlexception{ crs.setcommand("select payid paymentinfo account = '"+2134541+"'"); crs.execute(); crs.next(); payid = crs.getint("payid"); for(int i=0;i<n;i++){ crs.setcommand("insert tickets (tickettype,eventid,userid,payid,price) values ("+false+","+id+ ","+ loginbean.getuid() + ","+payid+","+price+")"); crs.execute(); crs.next(); } crs.setcommand("update events set maxtickets = maxtickets - "+n+"where eventid = "+eb.geteventid()); crs.execute(); crs.next(); }
the problem occurs inside for loop
.the payid
set , using payid
try insert ticket table. if place breakpoint @ crs.next()
statement, data entered. without it, gives me exception. thank help
crs initialization
cachedrowset crs = rowsetprovider.newfactory().createcachedrowset();
crs.seturl("jdbc:derby://localhost:1527/events ticket management"); crs.setusername("x"); crs.setpassword("x");
update
after placing insert
statement in try-catch
block, tickets entered database, update crashes now, giving java.lang.nullpointerexception
.
the method works temporary fix exception caught. doesn't let me set "n" number of tickets , 2 tickets! giving java.lang.nullpointerexception because wasn't setting id
eventid
. kind of sloppy side.
public void buyticket(int id,int n,int price) throws sqlexception{ crs.setcommand("select payid paymentinfo account = '"+account+"'"); crs.execute(); crs.next(); payid = crs.getint("payid"); try{ for(int i=0;i<n;i++){ crs.setcommand("insert tickets (tickettype,eventid,userid,payid,price) values ("+false+","+id+ ","+ loginbean.getuid() + ","+payid+","+price+")"); crs.execute(); crs.next(); } } catch(sqlexception e){ system.out.println(" message: " + e.getmessage()); e = e.getnextexception();} try{ crs.setcommand("update events set maxtickets = maxtickets - "+n+"where eventid = "+id); crs.execute(); crs.next(); } catch(sqlexception e ){ system.out.println(" message: " + e.getmessage()); e = e.getnextexception(); } }
Comments
Post a Comment