java - Oracle 10g 上传图片时出现空指针异常

标签 java oracle file-io file-upload

我有这样的关系 Create table ImageFile1(name varchar2(200), id number(30), Image BLOB); 成功创建了表。

但是当我尝试使用PreparedStatement插入数据时,我遇到了空指针异常问题 我使用的代码是..

        Connection con=null;
        System.out.println("Connection created0");
        Statement stmt=null;
        System.out.println("Connection created1");
        ResultSet rs=null;
        System.out.println("Connection created2");

        con=(Connection)session.getAttribute("connection");
        System.out.println("Connection created");



        File imgfile = new File("C:\\Users\\HP\\Pictures\\PALLU.jpg");

        System.out.println("*******");
        FileInputStream fin = new FileInputStream(imgfile);
        System.out.println("file ok");

        PreparedStatement pre = con.prepareStatement("insert into ImageFile1 values(?,?,?)");
                System.out.println("ps ok");
        pre.setString(1,"Vijay");
        pre.setInt(2,1);
        pre.setBinaryStream(3,fin,(int)imgfile.length());
        System.out.println("image problem solved");
        pre.executeUpdate();
        System.out.println("Inserting Successfully!");
        pre.close();

输出是:

Connection created0
Connection created1
Connection created2
Connection created
<小时/>

文件正常

java.lang.NullPointerException

请帮助摆脱这个......

最佳答案

您在什么时候遇到空指针异常?另外,在获取连接实例的行 con=(Connection)session.getAttribute("connection"); 中,检查以确保连接实例不为 null。在 Session 中存储连接对象可能不是一个好方法。使用 DriverManager 创建连接类或更好地使用连接池。 此外,使用一些文件上传库,如 Apache Commons File Upload 。该库有足够的文档可供使用,使图像上传变得非常简单。

关于java - Oracle 10g 上传图片时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6247089/

相关文章:

python cx_Oracle Bind非法变量名

unix - UNIX 中文件追加是原子的吗?

C:使用 fread()/fgets() 而不是 fgetc() 逐行读取文本文件(具有可变长度行)( block I/O 与字符 I/O)

java - 读取和写入多个 HashMap

java - 参数类型后面的 `...` 有什么意义?

oracle - Entity Framework 创建疯狂的 bin 表?为什么?

sql - 当查询返回多行时,Oracle extractValue 失败

java - 在 Java 中逐行读取大型 JSON 文件的快速高效方法

java - 执行k次操作后如何最小化最终数组中元素的总和

java - 从Java代码的顶部返回,如何实现?