Pretty common error, and I thought it would be easy to solve, but in this case, unfortunately I couldn't figure it out.
Java project connected to MySQL, set up with Connector/J.
Error occurs when adding a property (registering an apartment):
Related files:
Database.java:
....other methods and connection stuff
/**
* Adds a property into the property table in the database
* @param address
* @param quadrant
* @param type
* @param numBedrooms
* @param numBathrooms
* @param furnished
* @param fees
* @param status
* @param landID
* @param startD
* @param endD
*/
public void addProperty(String address,String quadrant, String type, int numBedrooms, int numBathrooms, String furnished, double fees, String status, int landID, String startD, String endD){
try {
String query = "INSERT INTO property(Address, quadrant, Type, NoOfBedrooms, NoOfBathrooms, Furnished, Fees, FeesPaid, Status, Landlord_ID, StartDate, EndDate) ";
query += "VALUES ('%s', '%s','%s', %d, %d, '%s', %f, 'No', 'Active', %d, '%s', '%s')";
query = String.format(query, address, quadrant, type, numBedrooms, numBathrooms, furnished, fees, landID, startD, endD);
try (Statement stmt = dbConnect.createStatement()) {
stmt.executeUpdate(query);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
...other methods
RegisterController.java:
...other stuff
//Add property
public void add(Property p) {
db.initializeConnection();
db.addProperty(p.getAddress(), p.getQuadarnt(), p.getType(), p.getNumOfBedrooms(), p.getNumOfBathrooms(), p.getFurnished(), 0, p.getPropertyStatus(), landLordID, "null", "null");
db.close();
}
//Handle property registrations
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(createProp.getRegister()))
{
if(!createProp.getStreetNoInput().equals("") && !createProp.getStreetNameInput().equals("") && !createProp.getCityInput().equals("") && !createProp.getPostalCodeInput().equals(""))
{;
String address = createProp.getStreetNoInput() +", " + createProp.getStreetNameInput() +", " + createProp.getCityInput() + ", " + createProp.getPostalCodeInput();
Property p = new Property(landLordID, address, createProp.getQuadrantInput(), createProp.getTypeInput(), createProp.getNoOfBedInput(), createProp.getNoOfBathInput(), createProp.getFurnishedInput(), null, "Suspended");
this.add(p);
createProp.showDialog();
createProp.destroyFrame();
landView.turnOn();
}
else{
createProp.showErrorDialog();
}
}
}
}
Property.java:
public class Property {
//Member varables for the class Property
private int id; //Property ID
private int landlordID; //Landlord ID
private String address; //Property Address
private String type; //Property type [Detattached, Attached, Town House, Apartment]
private int numOfBedrooms; //Number of Bedrooms
private int numOfBathrooms; //Number of Bathrooms
private String furnished; //Furnished: Yes or No
private Fees propertyFees; //Object of Fees for Property fees
private String propertyStatus; //Property Status [Active, Rented, Cancelled, Suspeneded]
private String quadrant; //Property Quadrant [NW, NE, SW, SE]
private String rentDate; //Property Start Rent Date
private String startDate; //Property Start Date when it becomes Active
private String endDate; //Property End Date when property is removed from the application
and what's weird that it works on my friend's PC, although everything is the same on both of us.
The error is basically that when you made the String query
the number of arguments that you provided in the query did not equal the number of arguments of the table in the database, check what arguments you are missing, check if the query works in the workbench, then write it in java.
also always use PreparedStatement, it is much easier to use and more organized, for example:
PreparedStatement myStmt;
myStmt = myCon.prepareStatement(select * from students where age> ? and name = ?);
myStmt.setInt(1,10);
myStmt.setString(2,"Chhavi");