我有这样的关系
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/