SQLIntegrityConstraintViolationException,

时间:2016-07-25 17:23:54

标签: java duplicates key add derby

我试图创建一个基本程序,我可以在Java Derby数据库中添加和删除数据。但是问题是它引发了一个异常'语句被中止,因为它会导致在'CONTACTS_LIST'中定义的'SQL160724181654400'标识的唯一或主键约束或唯一索引中出现重复键值。 '主要目标是在2个JTextFields中添加文本字符串,并将其导入到只有2个列的数据库中。我不明白问题出在哪里。

                try {
                    String writeString = "INSERT INTO Contacts_List(NAME, NUMBER) VALUES(?,?)";
                    PreparedStatement pst = connection.prepareStatement(writeString);

                    pst.setString(1, nameF.getText());
                    pst.setString(2, numberL.getText());

                    pst.execute();

                    showMessageDialog(this, " Contact's added! ");
                    nameF.setText(" ");
                    numberF.setText(" ");

                    pst.close();
                } catch (Exception ex) {

                    ex.printStackTrace();
                }

我的完整代码如下;

package address_book;

  import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
  import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
  import java.io.File;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
import java.sql.SQLException;
 import java.sql.Statement;
import javax.swing.BorderFactory;
 import javax.swing.JButton;
  import javax.swing.JFrame;
    import javax.swing.JLabel;
   import javax.swing.JOptionPane;
  import static javax.swing.JOptionPane.showMessageDialog;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import org.apache.derby.drda.NetworkServerControl;

 public class Add_Data extends JFrame implements ActionListener {

JButton add = new JButton("Add To database");

JTextField nameF = new JTextField(12);
JTextField numberF = new JTextField(12);

JLabel nameL = new JLabel("Enter Name : ");
JLabel numberL = new JLabel("Enter Number : ");

private static Connection connection;
private static Statement stmt;

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            new Add_Data().setVisible(true);

        }
    });
}

public Add_Data() {
    super("Add Contact");
    setSize(300, 250);
    setVisible(true);
    setLocationRelativeTo(null);
    setResizable(false);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    // add jpanel and the layout
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = new Insets(10, 10, 10, 10);

    // add componenets 
    constraints.gridx = 0;
    constraints.gridy = 0;
    panel.add(nameL, constraints);

    constraints.gridx = 1;
    panel.add(nameF, constraints);

    constraints.gridx = 0;
    constraints.gridy = 1;
    panel.add(numberL, constraints);

    constraints.gridx = 1;
    panel.add(numberF, constraints);

    constraints.gridx = 0;
    constraints.gridy = 2;
    constraints.gridwidth = 2;
    constraints.anchor = GridBagConstraints.CENTER;
    panel.add(add, constraints);
    add.addActionListener(this);

    //create border 
    panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Enter Contact's Details"));

    add(panel);

    try {
        NetworkServerControl server = new NetworkServerControl();
        server.start(null);
        // Load JDBC driver
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        //Establish a connection
        String sourceURL = "jdbc:derby://localhost:1527/"
                + new File("Contacts_List").getAbsolutePath() + ";";
        connection = DriverManager.getConnection(sourceURL, "use", "use");
        stmt = connection.createStatement();
    } // The following exceptions must be caught
    catch (ClassNotFoundException cnfe) {
        System.out.println(cnfe);
    } catch (SQLException sqle) {
        System.out.println(sqle);
    } catch (Exception e) {
        System.out.println(e);
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    String key = nameF.getText();
    String name = Database.getName(key);

    String nameAdd = nameF.getText();
    if (nameF.getText().isEmpty() || numberF.getText().isEmpty() && e.getSource() == add) {

        JOptionPane.showMessageDialog(this, "Fill the empty fields");
    } else if (e.getSource() == add) {
        if (name == null) {
            int confirm = JOptionPane.showConfirmDialog(null, "You are about to add " + nameAdd + ", Do you want to Continue?");

            if (confirm == JOptionPane.YES_OPTION) {

                try {
                    String writeString = "INSERT INTO Contacts_List(NAME, NUMBER) VALUES(?,?)";
                    PreparedStatement pst = connection.prepareStatement(writeString);

                    pst.setString(1, nameF.getText());
                    pst.setString(2, numberL.getText());

                    pst.execute();

                    showMessageDialog(this, " Contact's added! ");
                    nameF.setText(" ");
                    numberF.setText(" ");

                    pst.close();
                } catch (Exception ex) {

                    ex.printStackTrace();
                }
                if (confirm == JOptionPane.NO_OPTION) {
                    showMessageDialog(this, "Please Try Again !");
                }
            }
        }
    }

}

}

0 个答案:

没有答案