我正在创建包裹机器程序。每个宗地都有独特的parcelID,它被导出到mysql数据库。问题是每次运行程序时,程序都从0开始计数parcelID。我正在寻找一个解决方案,它将允许我检查数据库中最后一个parcelID并在最后一个之后创建行。INSERT INTO独特的字段
现在看起来像这样:1.我正在通过java程序在db(成功)中创建一个新行。我在一段时间后关闭了程序。 3.我再次运行该程序,因为“PRIMARY”关键字出现错误“重复条目1”,所以我无法添加另一个新行。
public static void post() throws Exception{
int parcelID = Parcel.generateID();
int clientMPNumber = Parcel.typeClientNumber();
int orderPassword = Parcel.generatePass();
try{
Connection con = getConnection();
PreparedStatement posted = con.prepareStatement("INSERT INTO Parcels.Orders (parcelID, clientMPNumber, orderPassword) VALUES ('"+parcelID+"', '"+clientMPNumber+"', '"+orderPassword+"')");
posted.executeUpdate();
}
catch(Exception e){
System.out.println(e);
}
finally{
System.out.println("Insert completed");
}
}
和方法是:
public static int generateID(){
parcelID = parcelID + 1;
return parcelID;
}
btw .:不要在你的查询中使用'prepareStatement()'和字符串concat值。使用'prepareStatement()'构建“准备好的语句”(这是一个带“?”的查询作为值的占位符),并使用'setInt'和'setString'来填充这些占位符。 – Progman