迭代地将多个数据插入到java数据库(derby)中的表中会产生错误

时间:2013-02-19 07:31:37

标签: derby

我编写了一个负责注册的程序,每当我尝试使用不同的id一次插入多个新用户时,它会给出错误消息: java.sql.SQLIntegrityConstraintViolationException:语句已中止,因为它会在唯一或主键约束或由'STCEPARTICIPANTS'中定义的'SQL130217122630580'标识的唯一索引中导致重复键值。

这是从按钮WHICH传递的动作,指示保存数据:

private void printsavebtnActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:

    String query1= "insert into STCEPARTICIPANTS values("
                +speno1tf.getText()+",'"+sname1tf.getText()+"','"
                +fname1tf.getText()+"','"+uni1cb.getSelectedItem()
                +"')";
    String query2= "insert into STCEPARTICIPANTS values("
                +speno1tf.getText()+",'"+sname1tf.getText()+"','"
                +fname1tf.getText()+"','"+uni1cb.getSelectedItem()
                +"')";
    String query3= "insert into STCEPARTICIPANTS values("
                +speno1tf.getText()+",'"+sname1tf.getText()+"','"
                +fname1tf.getText()+"','"+uni1cb.getSelectedItem()
                +"')";
    String query4= "insert into STCEPARTICIPANTS values("
                +speno1tf.getText()+",'"+sname1tf.getText()+"','"
                +fname1tf.getText()+"','"+uni1cb.getSelectedItem()
                +"')";
    String [] queryarray= {query1,query2,query3,query4};

    int speno1,speno2,speno3,speno4;
    String task;
    if(fname1tf.getText().equals("")||sname1tf.getText().equals("")||speno1tf.getText().equals("")
            ||uni1cb.getSelectedItem().equals("-")|| fname2tf.getText().equals("")||sname2tf.getText().equals("")||speno2tf.getText().equals("")
            ||uni2cb.getSelectedItem().equals("-") || fname3tf.getText().equals("")||sname3tf.getText().equals("")||speno3tf.getText().equals("")
            ||uni3cb.getSelectedItem().equals("-") || fname4tf.getText().equals("")||sname4tf.getText().equals("")||speno4tf.getText().equals("")
            ||uni4cb.getSelectedItem().equals("-")  ){
        JOptionPane.showMessageDialog(rootPane, "Please enter the fields marked '*'");
    }
    else {
        try{
        speno1=Integer.parseInt(speno1tf.getText());
        speno2=Integer.parseInt(speno2tf.getText());
        speno3=Integer.parseInt(speno3tf.getText());
        speno4=Integer.parseInt(speno4tf.getText());

        int [] taskit = {speno1,speno2,speno3,speno4};

        for(int count2=0;count2<taskit.length;count2++){
            task=  "select * from STCEPARTICIPANTS where spe_number="+taskit[count2];
            DBOptions.executeNonQuery(queryarray[count2]);

             if(SearchData.searchSpeno(task)==true){
        JOptionPane.showMessageDialog(rootPane, "Sorry, this member is already in the database./t Please go to Profile to renew of view membership details. Thank you!");
        }   
        }

类SearchData如下:

public static boolean searchSpeno(String task){
    String query =task;
    ResultSet rs = DBOptions.executeSQLQuery(query);

    if(rs!=null)
        {
            try
            {
                java.sql.ResultSetMetaData rsmd = rs.getMetaData();
                int colCount = rsmd.getColumnCount();
                if(colCount > 0)
                {
                    try
                    {
                        if(rs.next() && ! rs.getString("spe_number").equals(""))
                        {
                                return true;
                        }
                        else
                        {
                            return false;
                        }

                    }

                    catch(SQLException e)
                    {
                        JOptionPane.showMessageDialog(null, e,"Search Error", 3);
                        return false;
                    }
                }
                else
                {
                     //JOptionPane.showMessageDialog(null, "Invalid Employee ID","Search Error", 3);
                     return false;
                }
            }
            catch(SQLException ex)
            {
                 //JOptionPane.showMessageDialog(null, ex.getMessage(),"Error Occured", 2);
                 return false;
            }

         }
        else
        {
            return false;
        }

}

}

}

类DBOptions是:

public static boolean executeNonQuery(String sqlString)
{
    try
    {
        Statement stmt = con.createStatement();
        stmt.executeUpdate(sqlString);
        JOptionPane.showMessageDialog(null,"success!");
        return true;
        //return the number of rows affected
    }
    catch(SQLException e)
    {
        //display error message
        JOptionPane.showMessageDialog(null,  e.getMessage()+"\nPlease Try Again","Non Query Execution Failure", 1);
        e.printStackTrace();
        return false;
    }
}

public static ResultSet executeSQLQuery(String sqlQuery)
{
    try
    {
        Statement stmt = con.createStatement();
        return stmt.executeQuery(sqlQuery); //query successfully executed
    }
    catch(SQLException e)
    {
        //display error message
        JOptionPane.showMessageDialog(null, e.getMessage()+"\nPlease Try Again","Query Execution Failure", 1);
        return null; //sql query execution failed
    }
}

}

拜托,我已经看到了这样的一些问题,我尝试了不同形式的解决方案,但没有头脑。我需要为小型项目防御做好准备。感谢您的回复。谢谢。

1 个答案:

答案 0 :(得分:0)

你是怎么创造这张桌子的?您可以将CREATE TABLE语句粘贴到您的问题中吗?

表中的每一行必须为每列定义一个唯一值,该值定义为PRIMARY KEY或UNIQUE,并且Derby正在强制执行该约束。

返回到您的表定义,找出您在列上指定了PRIMARY KEY或UNIQUE的列,然后查看您的程序以确定您为这些列提供的值。

然后修改您的程序,使其为每个此类列提供唯一值。