Search code examples
derby

inserting multiple data iteratively into a table in java database (derby) gives error


I've written a program that takes care of registration and each time i try to insert multiple new users at a time with different id it gives the error message: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL130217122630580' defined on 'STCEPARTICIPANTS'.

here is the action passed from a button WHICH instructs the data to be saved:

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!");
        }   
        }

the class SearchData is given below:

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;
        }

}

}

}

the class DBOptions is :

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
    }
}

}

Please, i have seen some problems like this and i have tried the different forms of solution but no head way. I need to get this ready for a mini project defense. I appreciate your response. Thank you.


Solution

  • How did you create the table? Can you paste the CREATE TABLE statement into your question?

    Each row in your table must have a unique value for each column which is defined either as PRIMARY KEY or as UNIQUE, and Derby is enforcing that constraint.

    Go back to your table definition, figure out for which columns you have specified either PRIMARY KEY or UNIQUE on the column, and then look at your program to figure out what values you are providing for those columns.

    Then modify your program so that it provides a unique value for each such column.