我编辑了问题。这是完整的代码。看看吧。
public class AddStaff extends Application {
private Connection connect = null;
private Statement statement = null;
private ResultSet rs = null;
private PreparedStatement preparedStatement = null;
TextField addSubName;
String msg;
private TableView<Staff> table = new TableView<Staff>();
private final ObservableList<Staff> data
= FXCollections.observableArrayList();
final HBox hb = new HBox();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Add Staff");
stage.setWidth(450);
stage.setHeight(550);
stage.setResizable(false);
final Label label = new Label("Staff Details");
label.setFont(Font.font("Calibri", FontWeight.BOLD, 22));
label.setTextFill(Color.WHITE);
TableColumn sub = new TableColumn("Staff Name");
sub.setMinWidth(400);
sub.setCellValueFactory(
new PropertyValueFactory<Staff, String>("subName"));
sub.setCellFactory(TextFieldTableCell.forTableColumn());
sub.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<Staff, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<Staff, String> t) {
((Staff) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setSubName(t.getNewValue());
}
}
);
table.setItems(data);
table.getColumns().addAll(sub);
addSubName = new TextField();
addSubName.setPromptText("Staff Name");
addSubName.setPrefSize(200, 30);
final Button b2 = new Button("Add");
b2.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
b2.setPrefSize(70, 30);
b2.setStyle(" -fx-base: #0066ff;");
b2.setTextFill(Color.BLACK);
b2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
msg = addSubName.getText();
try {
enterStaff();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(AddStaff.class.getName()).log(Level.SEVERE, null, ex);
}
data.add(new Staff(addSubName.getText()));
addSubName.clear();
}
});
hb.getChildren().addAll(addSubName, b2);
hb.setSpacing(5);
final VBox vbox = new VBox();
vbox.setSpacing(10);
vbox.setPadding(new Insets(20, 20, 20, 20));
vbox.getChildren().addAll(label, table, hb);
vbox.setStyle("-fx-background-color: #333333;");
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public void enterStaff() throws ClassNotFoundException, SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/project?"
+ "user=root&password=virus");
statement = connect.createStatement();
preparedStatement = connect
.prepareStatement("SELECT count(*)FROM information_schema.tables\n"
+ "WHERE table_schema = 'project' AND table_name = 'staff'");
rs = preparedStatement.executeQuery();
rs.next();
int chk = rs.getInt(1);
if (chk != 1) {
preparedStatement = connect
.prepareStatement("create table staff (staffname varchar(30) primary key");
preparedStatement.executeUpdate();
}
preparedStatement = connect
.prepareStatement("insert into staff values(?)");
preparedStatement.setString(1, msg);
preparedStatement.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
throw e;
} finally {
close2();
}
}
private void close2() {
try {
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (SQLException e) {
}
}
}
此代码产生异常 -
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '' for key 'PRIMARY'
不知道为什么会出现这个异常。我尝试了很长时间来纠正它。我怎样才能避免这种异常?
最佳答案
我不认为这是你的代码,但你正试图向数据库中插入一个具有相同主键的条目。
Duplicate entry '' for key 'PRIMARY'
这意味着您要插入的项目将 ''
(空)作为主键,并且您已经使用该键插入了一个项目。
问题可能是您正在创建表格,但它在上一次运行时已经存在。当您创建新表时,请始终先尝试 DROP
。
关于java - 相同类型的代码,但有一个代码显示异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22933080/