使用ucanaccess连接更新我的ms访问数据库

时间:2015-05-08 07:11:39

标签: java

我有这些代码使用ucanaccess更新我的ms访问数据库。我的代码工作如果我更新我的数据库的第一行,但问题是如果我更新第二行直到最后一行出现此错误。

“net.ucanaccess.jdbc.UcanaccessSQLException:完整性约束违规:唯一约束或索引违规; SYS_PK_10339表:EMPLOYEETABLE2”

我怀疑我在ms访问设置中犯了一个错误,我的主键是Employee Name,其数据类型是Text。

   public void actionPerformed(ActionEvent e) 
   {                    

                        String employeeName = endrollNameFields.getText();
                        String employeeAddress = endrollAddressFields.getText(); 
                        String employeeStatus = endrollEmployeestatusFields.getText();
                        String employeeDateOfMembership = dateOfMembershipFields.getText();
                        String employeeAge = ageFields.getText();
                        String employeMStatus = maritalstatusFields.getText();
                        String employeeBloodType = bloodTypeFields.getText();                               
                        String employeeGender = genderFields.getText();
                        String beginningCapital = beginningCapitalFields.getText();
                        String grossSalary = grossSalaryFields.getText();
                        String salaryDeductions = salaryDeductionFields.getText(); 
                        String netSalary = netSalaryFields.getText();

                        try
                        {
                            //------------CREATE CONNECTION TO DATA BASE--------------/

                            String DBPAD = "sourceFolder/employeeTable2.mdb";
                            String DB = "jdbc:ucanaccess://" + DBPAD;                           

                            con = DriverManager.getConnection(DB);                  
                            st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
                            String sql = "Select * FROM employeeTable2";

                            rs = st.executeQuery(sql);  

                            if (rs != null) 
                                while (rs.next()) 
                                {
                                    rs.updateString("Employee Name", employeeName);
                                    rs.updateString("Employee Age", employeeAge);   
                                    rs.updateString("Employee Address", employeeAddress);
                                    rs.updateString("Employee Marital Status", employeMStatus);
                                    rs.updateString("Employee Date of Membership", employeeDateOfMembership);
                                    rs.updateString("Employee Blood Type", employeeBloodType);
                                    rs.updateString("Employee Status", employeeStatus);                                 
                                    rs.updateString("Employee Gender", employeeGender);
                                    rs.updateString("Beginning Capital", beginningCapital);
                                    rs.updateString("Gross Salary", grossSalary);
                                    rs.updateString("Salary Deductions", salaryDeductions);
                                    rs.updateString("Net Salary", netSalary);                           

                                     rs.updateRow();
                                 }                                  

                            st.close();     


                            st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
                            String sql1 = "select * from employeeTable2";
                            rs = st.executeQuery(sql1);

                            JOptionPane.showMessageDialog(null, "<html>" + "<font color=\"#008000\">" + "<html><span style='font-size:1.5em'>Update to Data Base is Successful");

                            endrollNameFields.setText(" "); 
                            endrollAddressFields.setText(" ");
                            endrollEmployeestatusFields.setText(" ");
                            dateOfMembershipFields.setText(" "); 
                            ageFields.setText(" "); 
                            maritalstatusFields.setText(" ");
                            genderFields.setText(" ");                            
                            bloodTypeFields.setText(" ");
                            beginningCapitalFields.setText(" ");
                            grossSalaryFields.setText(" ");
                            salaryDeductionFields.setText(" ");
                            netSalaryFields.setText(" ");                           

                        }
                        catch(Exception e1)
                        {
                            JOptionPane.showMessageDialog(null, e1);
                        }               
                    }       

                });

1 个答案:

答案 0 :(得分:0)

您正在尝试修改主键列Employee Name,这会导致重复。这是因为您从employeeTable2中选择了所有行并尝试将它们全部更新为相同的值

要插入新值,请使用INSERT语句,例如......

String employeeName = endrollNameFields.getText();
String employeeAddress = endrollAddressFields.getText();
String employeeStatus = endrollEmployeestatusFields.getText();
String employeeDateOfMembership = dateOfMembershipFields.getText();
String employeeAge = ageFields.getText();
String employeMStatus = maritalstatusFields.getText();
String employeeBloodType = bloodTypeFields.getText();
String employeeGender = genderFields.getText();
String beginningCapital = beginningCapitalFields.getText();
String grossSalary = grossSalaryFields.getText();
String salaryDeductions = salaryDeductionFields.getText();
String netSalary = netSalaryFields.getText();

String DBPAD = "sourceFolder/employeeTable2.mdb";
String DB = "jdbc:ucanaccess://" + DBPAD;

try (Connection con = DriverManager.getConnection(DB)) {
    try (PreparedStatement stmt = con.prepareStatement("insert into employeeTable2 ("
                    + "'Employee Name', "
                    + "'Employee Age', "
                    + "'Employee Address', "
                    + "'Employee Marital Status', "
                    + "'Employee Date of Membership', "
                    + "'Employee Blood Type'"
                    + "'Employee Status',"
                    + "'Employee Gender',"
                    + "'Beginning Capital',"
                    + "'Gross Salary',"
                    + "'Salary Deductions',"
                    + "'Net Salary') values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {

        stmt.setString(1, employeeName);
        stmt.setString(2, employeeAge);
        stmt.setString(3, employeeAddress);
        stmt.setString(4, employeMStatus);
        stmt.setString(5, employeeDateOfMembership);
        stmt.setString(6, employeeBloodType);
        stmt.setString(7, employeeStatus);
        stmt.setString(8, employeeGender);
        stmt.setString(9, beginningCapital);
        stmt.setString(10, grossSalary);
        stmt.setString(11, salaryDeductions);
        stmt.setString(12, netSalary);

        JOptionPane.showMessageDialog(null, "<html>" + "<font color=\"#008000\">" + "<html><span style='font-size:1.5em'>Update to Data Base is Successful");

        endrollNameFields.setText(" ");
        endrollAddressFields.setText(" ");
        endrollEmployeestatusFields.setText(" ");
        dateOfMembershipFields.setText(" ");
        ageFields.setText(" ");
        maritalstatusFields.setText(" ");
        genderFields.setText(" ");
        bloodTypeFields.setText(" ");
        beginningCapitalFields.setText(" ");
        grossSalaryFields.setText(" ");
        salaryDeductionFields.setText(" ");
        netSalaryFields.setText(" ");

    }

} catch (Exception e1) {
    JOptionPane.showMessageDialog(null, e1);
}

现在,如果要更新现有行,只需使用UPDATE语句,或使用当前的编码样式,选择与员工姓名相匹配的行并更新该行,但是,这假设名称没有改变......

String employeeName = endrollNameFields.getText();
String employeeAddress = endrollAddressFields.getText();
String employeeStatus = endrollEmployeestatusFields.getText();
String employeeDateOfMembership = dateOfMembershipFields.getText();
String employeeAge = ageFields.getText();
String employeMStatus = maritalstatusFields.getText();
String employeeBloodType = bloodTypeFields.getText();
String employeeGender = genderFields.getText();
String beginningCapital = beginningCapitalFields.getText();
String grossSalary = grossSalaryFields.getText();
String salaryDeductions = salaryDeductionFields.getText();
String netSalary = netSalaryFields.getText();

String DBPAD = "sourceFolder/employeeTable2.mdb";
String DB = "jdbc:ucanaccess://" + DBPAD;

try (Connection con = DriverManager.getConnection(DB)) {
    try (PreparedStatement stmt = con.prepareStatement("Select * FROM employeeTable2 where 'Employee Nam' == ?",
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE)) {

        stmt.setString(1, employeeName);

        try (ResultSet rs = stmt.executeQuery()) {

            if (rs.next()) {

                rs.updateString("Employee Name", employeeName);
                rs.updateString("Employee Age", employeeAge);
                rs.updateString("Employee Address", employeeAddress);
                rs.updateString("Employee Marital Status", employeMStatus);
                rs.updateString("Employee Date of Membership", employeeDateOfMembership);
                rs.updateString("Employee Blood Type", employeeBloodType);
                rs.updateString("Employee Status", employeeStatus);
                rs.updateString("Employee Gender", employeeGender);
                rs.updateString("Beginning Capital", beginningCapital);
                rs.updateString("Gross Salary", grossSalary);
                rs.updateString("Salary Deductions", salaryDeductions);
                rs.updateString("Net Salary", netSalary);
                rs.updateRow();

                JOptionPane.showMessageDialog(null, "<html>" + "<font color=\"#008000\">" + "<html><span style='font-size:1.5em'>Update to Data Base is Successful");

                endrollNameFields.setText(" ");
                endrollAddressFields.setText(" ");
                endrollEmployeestatusFields.setText(" ");
                dateOfMembershipFields.setText(" ");
                ageFields.setText(" ");
                maritalstatusFields.setText(" ");
                genderFields.setText(" ");
                bloodTypeFields.setText(" ");
                beginningCapitalFields.setText(" ");
                grossSalaryFields.setText(" ");
                salaryDeductionFields.setText(" ");
                netSalaryFields.setText(" ");

            } else {
                // No matching row, maybe do a insert insetad
            }

        }

    }

} catch (SQLException e1) {
    JOptionPane.showMessageDialog(null, e1);
}

另一种方法可能是维护对原始ResultSet的引用,该引用用于加载数据并简单地用新数据更新“当前”行,但您需要小心确保光标位置未更改为新行。否则,您应该保留该行的原始密钥(Employee Name)的副本,并在您要执行更新时使用该副本从数据库中选择数据...

就个人而言,我认为UPDATE声明会更简单......

您还应该更加谨慎地使用您的资源,如果代码中发生异常,则不太可能关闭未完成的开放资源。

请查看The try-with-resources Statement了解更多详情

(ps自从我对MS Access数据库做了任何工作以来,已经很长时间了,所以我的列引用可能错了)