我创建了一个简单的应用程序,允许用户购买Tickets。但是,每当我尝试购买" n"时,我都会遇到此错误。门票数量。现在,我知道在使用数据库时需要crs.next()
语句,我已经多次使用它但由于某种原因它在这里不起作用。它给了我 java.sql.SQLException:无效的光标位置错误。
这是我插入故障单的代码段。
public void buyTicket(int id,int n,int price) throws SQLException{
crs.setCommand("SELECT PAYID FROM PAYMENTINFO WHERE ACCOUNT = '"+2134541+"'");
crs.execute();
crs.next();
payID = crs.getInt("PAYID");
for(int i=0;i<n;i++){
crs.setCommand("Insert into 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();
}
问题发生在for loop
内。payID
已设置并使用payID
我尝试插入Ticket表。如果我在crs.next()
语句处放置断点,则输入数据。没有它,它给了我例外。谢谢你的帮助
CRS初始化
CachedRowSet crs = RowSetProvider.newFactory()。createCachedRowSet();
crs.setUrl("jdbc:derby://localhost:1527/Events Ticket Management");
crs.setUsername("x");
crs.setPassword("x");
更新
将INSERT
语句放入try-catch
块后,将票证输入数据库,但更新现在崩溃,产生java.lang.NullPointerException
。
答案 0 :(得分:0)
该方法在捕获异常时用作临时修复。这不允许我设置“n”个票数,只有2个票!它也提供了java.lang.NullPointerException,因为我没有将ID
设置为eventID
。这有点像我这边的草率。
public void buyTicket(int id,int n,int price) throws SQLException{
crs.setCommand("SELECT PAYID FROM PAYMENTINFO WHERE ACCOUNT = '"+account+"'");
crs.execute();
crs.next();
payID = crs.getInt("PAYID");
try{
for(int i=0;i<n;i++){
crs.setCommand("Insert into 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();
}
}