Java / JDBC / SQLite - 更新BLOB的问题

时间:2016-12-21 15:49:33

标签: java sql sqlite jdbc blob

我遇到更新Blob的问题,问题是pst.executeUpdate没有执行,但是,如果我拿出与Blob / Tryinng相关的所有内容来更新Blob,其他一切都会更新即ID,名称,地址等。一切都按预期运行,问题就在于Blob。

 updateEmployee.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            Connection connection = null;
            PreparedStatement pst = null;

            try {

                Class.forName("org.sqlite.JDBC");
                connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
                connection.setAutoCommit(false);



                int idVal = Integer.parseInt(idTextField.getText());
                String nameVal= nameTextField.getText();
                String genderVal = genderTextField.getText();
                String dobVal = dobTextField.getText();
                String addressVal = addressTextField.getText();
                String postcodeVal =  postcodeTextField.getText();
                String ninVal =  ninTextField.getText();
                String jobVal =  jobtitleTextField.getText();
                String startDateVal =  startdateTextField.getText();
                String salaryVal = salaryTextField.getText();
                String emailVal =  emailTextField.getText();
                //Icon photoBlob = photoLabel.getIcon();
                InputStream img = new FileInputStream(s);
                String sql = "UPDATE employees set ID= '"+ idVal+"', Name = '"+ nameVal +"', Gender ='"+ genderVal+"', DOB='"+ dobVal+"', Address ='"+ addressVal+"', Postcode ='"+ postcodeVal+"', NIN ='"+ ninVal+"', JobTitle='"+ jobVal+"', StartDate ='"+ startDateVal+"', Salary ='"+ salaryVal+"', Email='"+ emailVal+"', Images='"+ img+" WHERE ID= '"+ idVal+"'";

                pst = connection.prepareStatement(sql);
                pst.setInt(1,Integer.parseInt(idTextField.getText()));
                pst.setString(2, nameTextField.getText());
                pst.setString(3, genderTextField.getText());
                pst.setString(4, dobTextField.getText());
                pst.setString(5, addressTextField.getText());
                pst.setString(6, postcodeTextField.getText());
                pst.setString(7, ninTextField.getText());
                pst.setString(9, startdateTextField.getText());
                pst.setString(10, salaryTextField.getText());
                pst.setString(11, emailTextField.getText());
                pst.setBytes(12, readFile(s));

                pst.executeUpdate();




                System.out.println("Employee Updated");
                JOptionPane.showMessageDialog(null, "Employee has successfully been updated");              


                 connection.commit();
                pst.close();
                connection.close();
            }
            catch ( Exception e1 ) {

                if(idTextField.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "Please Ensure An Employee Has Been Selected");
                }
            }
        }});

编辑 -

但是,我可以插入和删除blob文件以及检索。只是这个更新给了我一个问题。

1 个答案:

答案 0 :(得分:1)

您的SQL命令文本对参数化查询无效。而不是使用嵌入值创建动态SQL命令字符串...

String sql = "UPDATE employees set ID = ?, Name = ?, Gender = ?, DOB = ?, Address = ?, Postcode = ?, NIN = ?, JobTitle = ?, StartDate = ?, Salary = ?, Email = ?, Images = ? WHERE ID = ?;

...你应该使用带有问号的命令字符串作为参数占位符......

.setInt

...然后使用.setString.setBytesSET 来设置参数值。

(另请注意,当您使用... WHERE ID = ?时,Sub DeleteTheDoops() Dim RowNdx As Long Dim RowNdx2 As Long Dim FR as Long FR = Range("A1:G1").End(xlDown).Row 'Freeze this row For RowNdx = FR To 2 Step -1 For RowNdx2 = FR to 2 Step -1 'From what I can tell, you are interested when A, E and F are 'equal and when C is smallest, so ... If RowNdx <> RowNdx2 and _ Cells(RowNdx, "A").Value = Cells(RowNdx2, "A").Value and _ Cells(RowNdx, "F").Value = Cells(RowNdx2, "F").Value and _ Cells(RowNdx, "E").Value = Cells(RowNdx2, "E").Value and _ Cells(RowNdx, "C").Value >= Cells(RowNdx2, "C").Value Then Rows(RowNdx2).Delete End If Next RowNdx2 Next RowNdx End Sub &#34; ID&#34;值实际上是多余的。)