By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,425 Members | 2,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,425 IT Pros & Developers. It's quick & easy.

problem regarding retrieving image from database

P: 40
i have inserted the image into database using the following code


Expand|Select|Wrap|Line Numbers
  1.  
  2. String driverName = "com.mysql.jdbc.Driver";
  3. String url = "jdbc:mysql://localhost:3306/";
  4. String dbName = "hibernatetutorial";
  5. String userName = "root";
  6. String password = "root";
  7. Connection con = null;
  8. try{
  9. Class.forName(driverName);
  10. con = DriverManager.getConnection(url+dbName,userName,password);
  11. Statement st = con.createStatement();
  12. File imgfile = new File("images.jpg");
  13. FileInputStream fin = new FileInputStream(imgfile);
  14. PreparedStatement pre = con.prepareStatement("insert into Image values (?,?,?)");
  15. pre.setInt(1,5);
  16. pre.setString(2,"firstimg");
  17. pre.setBinaryStream(3,fin,(int)imgfile.length());
  18. pre.executeUpdate();
  19. System.out.println("Inserting Successfully!");
  20. pre.close();
  21. con.close(); 
  22. }
  23. catch (Exception e){
  24. System.out.println(e.getMessage());
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. and wrote code for retrieving which is as follows
  33.  
  34.  
  35.  
  36. <%@ page import="java.sql.*" %> 
  37. <%@ page import="java.io.*" %> 
  38. <% 
  39.  
  40. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  41. Connection con = DriverManager.getConnection("jdbc:odbc:mybean", "scott", "tiger"); 
  42.  
  43. String strSQL = "SELECT Image " 
  44. + "FROM Images " 
  45. + "WHERE (ImageID=5)"; 
  46. Statement stmt = con.createStatement(); 
  47. ResultSet rs = stmt.executeQuery(strSQL); 
  48. rs.next(); 
  49.  
  50.  
  51. OutputStream os = response.getOutputStream(); 
  52. os.write(rs.getBytes("Image")); 
  53.  
  54.  
  55. %> 
  56.  
when i execute(retrieving code) the above code ,iam getting javax.servlet.excption:general error

what is the error in the above code ,if anyone knows please clear my doubt.
Mar 18 '08 #1
Share this Question
Share on Google+
2 Replies


P: 40
i have inserted the image into database using the following code


Expand|Select|Wrap|Line Numbers
  1.  
  2. Connection con = null;
  3. try{
  4. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");;
  5. con = DriverManager.getConnection("jdbc:odbc:mybean", "scott", "tiger");
  6. Statement st = con.createStatement();
  7. File imgfile = new File("images.jpg");
  8. FileInputStream fin = new FileInputStream(imgfile);
  9. PreparedStatement pre = con.prepareStatement("insert into Image values (?,?,?)");
  10. pre.setInt(1,5);
  11. pre.setString(2,"firstimg");
  12. pre.setBinaryStream(3,fin,(int)imgfile.length());
  13. pre.executeUpdate();
  14. System.out.println("Inserting Successfully!");
  15. pre.close();
  16. con.close(); 
  17. }
  18. catch (Exception e){
  19. System.out.println(e.getMessage());
  20. }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. and wrote code for retrieving which is as follows
  28.  
  29.  
  30.  
  31. <%@ page import="java.sql.*" %> 
  32. <%@ page import="java.io.*" %> 
  33. <% 
  34.  
  35. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  36. Connection con = DriverManager.getConnection("jdbc:odbc:mybean", "scott", "tiger"); 
  37.  
  38. String strSQL = "SELECT Image " 
  39. + "FROM Images " 
  40. + "WHERE (ImageID=5)"; 
  41. Statement stmt = con.createStatement(); 
  42. ResultSet rs = stmt.executeQuery(strSQL); 
  43. rs.next(); 
  44.  
  45.  
  46. OutputStream os = response.getOutputStream(); 
  47. os.write(rs.getBytes("Image")); 
  48.  
  49.  
  50. %> 
  51.  
when i execute(retrieving code) the above code ,iam getting javax.servlet.excption:general error

what is the error in the above code ,if anyone knows please clear my doubt.
Mar 18 '08 #2

amitpatel66
Expert 100+
P: 2,367
Duplicate threads merged for better management of forum

MODERATOR
Mar 18 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.