Anyone knows why im getting an Parameter index out of range?
Any help would be appreciated
Any help would be appreciated
Code:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3326) at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3310) at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3352) at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3296) at EquipShip.equipWeapon(EquipShip.java:949) at EquipShip.equipMenu(EquipShip.java:74) at Hangar.hangarMenu(Hangar.java:87) at Login.loginMenu(Login.java:74) at MainMenu.runMenu(MainMenu.java:32) at MainMenu.main(MainMenu.java:60)
Code:
public void equipWeapon(String selection, User user){ String userName = user.getUserName(); String choice = selection.substring(1); int intChoice = Integer.parseInt(choice); intChoice--; int itemWeight = 0; int curTotalWeight = 0; int capacity = 0; int id = 0; int slots = 0; int weaponsCount = 0; int partsCount = 0; int sum = 0; int itemCapacity = 0; int curTotalCapacity = 0; try{ PreparedStatement ps = con.prepareStatement("Select count(*) from purchase_weapons,weapons where weapons.id = purchase_weapons.weaponsId and equip = 'e' and username =?"); PreparedStatement ps2 = con.prepareStatement("Select count(*) from purchase_parts,parts where parts.id = purchase_parts.partsId and parttype = 'hulls' and equip = 'e' and username =?"); PreparedStatement ps1 = con.prepareStatement("Select slots from ships,purchase_ships where ships.id = purchase_ships.shipsId and username =?"); PreparedStatement ps3 = con.prepareStatement("update purchase_weapons set equip = 'E' where id = ?"); //Gets the weight of the parts which you want to upload PreparedStatement ps4 = con.prepareStatement("select weight from purchase_weapons,weapons where weapons.id = purchase_weapons.weaponsId and purchase_weapons.weaponsId = ?"); //Gets the total weight PreparedStatement ps5 = con.prepareStatement("select sum(weight) from purchase_parts,parts where parts.id = purchase_parts.partsId and equip = 'E'"); //Hull add capacity! //PreparedStatement ps7 = con.prepareStatement("select capacity from purchase_parts,parts where parts.id = purchase_parts.partsId and purchase_parts.partsId = ?"); PreparedStatement ps8 = con.prepareStatement("select sum(capacity) from purchase_parts,parts where parts.id = purchase_parts.partsId and equip = 'E'"); PreparedStatement ps6 = con.prepareStatement("select capacity from ships,purchase_ships where ships.id = purchase_ships.shipsId and username =?"); ps.setString(1,userName); ResultSet rs=ps.executeQuery(); ps2.setString(1,userName); ResultSet rs2=ps2.executeQuery(); ps1.setString(1,userName); ResultSet rs1=ps1.executeQuery(); ResultSet rs5=ps5.executeQuery(); ps6.setString(1,userName); ResultSet rs6=ps6.executeQuery(); id = weapon.get(intChoice).getId(); ps4.setInt(1,id); ResultSet rs4=ps4.executeQuery(); //ps7.setInt(1,id); //ResultSet rs7=ps7.executeQuery(); ps8.setInt(1,id); ResultSet rs8=ps8.executeQuery(); if(rs!=null){ while(rs.next()){ weaponsCount = rs.getInt(1); } } if(rs2!=null){ while(rs2.next()){ partsCount = rs2.getInt(1); } } if(rs1!=null){ while(rs1.next()){ slots = rs1.getInt(1); } } if(rs5!=null){ while(rs5.next()){ curTotalWeight = rs5.getInt(1); } } if(rs6!=null){ while(rs6.next()){ capacity = rs6.getInt(1); } } if(rs4!=null){ while(rs4.next()){ itemWeight = rs4.getInt(1); } } //if(rs7!=null){ // while(rs7.next()){ // itemCapacity = rs7.getInt(1); // } // } if(rs8!=null){ while(rs8.next()){ curTotalCapacity = rs8.getInt(1); } } sum = partsCount+weaponsCount; if(partsCount < 1){ if(sum < slots){ if(itemWeight + curTotalWeight + itemCapacity + curTotalCapacity < capacity){ ps3.setInt(1,id); ps3.executeUpdate(); System.out.println("You have equiped your hull of choice"); ps3.close(); }else{ System.out.println("Weight has exceeded its the capacity"); } }else{ System.out.println("It has exceeded your Ship's slots"); } }else { System.out.println("You have already equiped a hull."); } ps.close(); ps1.close(); ps2.close(); } catch (Exception e) { e.printStackTrace(); } equipMenu(user); }
Comment