UCanAccess 中的“语句意外结束”错误

Posted

技术标签:

【中文标题】UCanAccess 中的“语句意外结束”错误【英文标题】:"Unexpected end of statement" error in UCanAccess 【发布时间】:2015-05-07 08:43:27 【问题描述】:

我使用下面的代码来更新或编辑我的 Access 数据库中的数据,但每次单击按钮时都会出现此错误:

net.ucanaccess.jdbc.UcanaccessSQLException: 语句意外结束

public void actionPerformed(ActionEvent e) 
                   

        String employeeName = endrollNameFields.getText();
        String employeeAddress = endrollAddressFields.getText(); 
        String employeeStatus = endrollEmploymentStatusFields.getText();
        String employeeDateOfMembership = dateOfMembershipFields.getText();
        String employeeAge = ageFields.getText();
        String employeMStatus = statusFields.getText();
        String employeeBloodType = bloodTypeFields.getText();
        String employeeDateBirth = birthDateFields.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 * employeeTable2";                     

            rs = st.executeQuery(sql);                      

             if (rs != null) 
                 while ( rs.next()) 
                 
                     rs.updateString("Employee Name", employeeName);
                     rs.updateString("Employee Address", employeeAddress);  
                     rs.updateString("Employee Status", employeeStatus);
                     rs.updateString("Employee Date of Membership", employeeDateOfMembership);
                     rs.updateString("Employee Age", employeeAge);
                     rs.updateString("Employee Marital Status", employeMStatus);
                     rs.updateString("Employee Blood Type", employeeBloodType);
                     rs.updateString("Employee Date Birth", employeeDateBirth);
                     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();     
            con.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(" ");
            endrollEmploymentStatusFields.setText(" ");
            dateOfMembershipFields.setText(" ");
            ageFields.setText(" ");
            statusFields.setText(" ");
            bloodTypeFields.setText(" ");
            birthDateFields.setText(" ");
            genderFields.setText(" ");
            beginningCapitalFields.setText(" ");;
            grossSalaryFields.setText(" ");
            salaryDeductionFields.setText(" ");
            netSalaryFields.setText(" ");   

            editFrame.dispose();



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

);

【问题讨论】:

我认为你在Select * employeeTable2 中缺少FROM 你摆脱了你面临的错误吗?如果没有,请看我的回答。 【参考方案1】:

您在代码中编写了错误的 SQL 查询

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

改成

String sql = "Select * FROM employeeTable2";

【讨论】:

以上是关于UCanAccess 中的“语句意外结束”错误的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 中的 UCanAccess 出现“给定文件不存在”错误

在 Ucanaccess 中插入错误

Java 错误 - net.ucanaccess.jdbc.UcanaccessSQLException:格式错误的字符串:'

当别名包含某些字符时,UCanAccess 出现“意外令牌”错误

UCanAccess 出现 ClassNotFoundException 和 NoClassDefFound 错误

使用 UCanAccess 连接到数据库时出现“无效页码 1”错误