【问题标题】:got ORA-01465: invalid hex number while tried to insert image into oracle database得到 ORA-01465: 尝试将图像插入 Oracle 数据库时,十六进制数无效
【发布时间】:2018-03-29 11:56:33
【问题描述】:

我所做的是,我在 oracle 数据库中创建了一个带有数字数据类型的 image_id 和带有 blob 的 image_scr 的新表。 所以,我想在连接了 jdbc 驱动程序的情况下从 java.jframe 将图像插入到表中。

在下面的代码中,我将照片定义为: 字节[] 照片=空; 和文件名为:String filename= null;

这是从我的计算机获取图像的代码

JButton btnNewButton = new JButton("Change picture");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            File f = chooser.getSelectedFile();
            lblNewLabel_2.setIcon(new ImageIcon(f.toString()));
            filename = f.getAbsolutePath();
            textField_5.setText(filename);
            try {
                File image = new File(filename);
                FileInputStream fis = new FileInputStream(image);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for(int readNum; (readNum =fis.read(buf))!=-1;)
                {
                bos.write(buf,0,readNum);   
                }
                photo= bos.toByteArray();
                fis.close();
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }



        }
    });

这是数据库查询代码

JButton btnNewButton_2 = new JButton("insert");
    btnNewButton_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try{
                String query ="insert into stu_images(student_id,stu_image)values(01,'"+photo+"')";
                PreparedStatement pstmt = connection.prepareStatement(query);
                pstmt.executeQuery();
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });

【问题讨论】:

    标签: java oracle


    【解决方案1】:

    这是你应该做的。

    第 1 步。要了解您的代码 id 失败的原因,请打印出 query 字符串的值...并查看它。这不会是你所期望的。它肯定不是有效的 SQL。 (这就是 ORA 消息告诉您的内容。)

    第 2 步。要了解执行此操作的正确方法,请阅读有关如何使用 PreparedStatement、其中包含占位符的查询以及 PreparedStatement::setBytes 和 PreparedStatement::setBlob 方法的信息。

    简短的版本是......不要使用字符串抨击将参数值添加到 SQL 查询字符串。

    参考资料:

    【讨论】:

    • 谢谢。我查看了教程,发现到处都是字符串攻击。主要是印度教程..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多