非常常见的错误,我认为这很容易解决,但在这种情况下,不幸的是我无法弄清楚。
Java项目连接到MySQL,使用Connector/J设置。
添加房产(注册公寓)时出现错误:
相关文件:
数据库.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();
}
}
}
}
属性.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
奇怪的是,它可以在我朋友的电脑上运行,尽管我们俩的一切都是一样的。
错误基本上是,当您创建
String query
时,您在查询中提供的参数数量不等于数据库中表的参数数量,请检查您缺少哪些参数,检查查询是否适用工作台,然后用java编写。