I have a relation like this
Create table ImageFile1(name varchar2(200), id number(30), Image BLOB);
which successfully created the table.
But when iam trying to insert data using PreparedStatement, i got the problem as null pointer exception The code i used is..
Connection con=null;
System.开发者_Python百科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();
and the output is:
Connection created0
Connection created1
Connection created2
Connection created
file ok
java.lang.NullPointerException
Please help to getrid of this...
At which point are you getting Null Pointer exception? Also, the line wherein you get the connection instance, con=(Connection)session.getAttribute("connection");
, check to make sure the connection instance is not null. Storing connection object in Session may not be a good approach. Either create connection using DriverManager class or better use a Connection Pool.
Further, use some file uploading library like Apache Commons File Upload. There is enough documentation available for the library to make the image upload quite straightforward.
You want to be using a JDBCConnectionPool
. See here for details.
For future reference, when asking about an exception it is best to tell us on what line the exception occurred. In this particular case, it is pretty clear that it's this line:
PreparedStatement pre = con.prepareStatement("insert into ImageFile1 values(?,?,?)");
and the problem is that your con
is null. So you need to look at where that con
is coming from and how that gets populated. But in other circumstances, it is hard to infer from the code what the problem might be.
精彩评论