将数据插入MS-Access数据库时出错 - java

时间:2018-02-24 17:03:39

标签: java ms-access netbeans ms-access-2010 netbeans-8

我通过netbeans将数据插入MS-Access数据库时出错。没有语法错误,但它显示了我自己的书面异常

  

数据库操作错误,更新失败

这是代码:

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equalsIgnoreCase("Save")) {
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            Connection con = DriverManager.getConnection("jdbc:ucanaccess://E:\\IDM Downloads\\Compressed\\student managemtnt system\\StudentManagementSystem\\student.accdb");

            try {
                String str = "insert into student(RollNo,SName,Phno,Sex,FName,Occupation,"
                        + "MName,Dob,Age,Caste,Religion,Hname,City,District,State,pin,Year,Qualification)"
                        + " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                PreparedStatement ps = con.prepareStatement(str);
                ps.setString(1, txtAdNo.getText());
                ps.setString(2, txtName.getText());
                ps.setString(3, txtPhNo.getText());
                ps.setString(4, cmbSex.getSelectedItem().toString());
                ps.setString(5, txtFatherName.getText());
                ps.setString(6, txtOccupation.getText());
                ps.setString(7, txtMotherName.getText());
                ps.setString(8, txtDOB.getText());
                ps.setString(9, txtAge.getText());
                ps.setString(10, txtCaste.getText());
                ps.setString(11, txtReligion.getText());
                ps.setString(12, txtHouseName.getText());
                ps.setString(13, txtCity.getText());
                ps.setString(14, txtDistrict.getText());
                ps.setString(15, txtState.getText());
                ps.setString(16, txtPin.getText());
                ps.setString(17, txtYear.getText());
                ps.setString(18, txtQualification.getText());
                ps.executeUpdate();
                JOptionPane.showMessageDialog(null, "Registration details successfully updated", "Success", JOptionPane.INFORMATION_MESSAGE);
                ClearForm();
            }
            catch (Exception x) {
                JOptionPane.showMessageDialog(null, "Error on database operation,Updation failure", "Error", JOptionPane.ERROR_MESSAGE);
            }//inner try catch closed
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error on connection to database, cannot continue updation process", "Error", JOptionPane.ERROR_MESSAGE);
        }//outer try catch closed
    }//if closed
    if (e.getActionCommand().equalsIgnoreCase("Clear")) {
        ClearForm();
    }//if closed
    if (e.getActionCommand().equalsIgnoreCase("Cancel")) {
        this.dispose();
    }//if closed
}

0 个答案:

没有答案