Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
Insert image into database using JSP
Sponsered Links
Categories
Sponsered Links

Insert image into database using JSP

This section demonstrates you how to insert an image into the database using JSP. For this purpose, you have to create the table into the database. You can create a table using the following query.

CREATE TABLE `image` (                                    
          `id` bigint(30) NOT NULL auto_increment,                      
          `image` longblob,                                      
          PRIMARY KEY  (`id`)                                    
        )

<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%
try{
        File file=new File("C:/rose.jpg");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
        PreparedStatement st = con.prepareStatement("insert into image(image) values(?)");
        FileInputStream fis= new FileInputStream(file);
        st.setBinaryStream(1, (InputStream)fis, (int)(file.length()));
        int s = st.executeUpdate();

out.println("Image is inserted successfully");
}
catch(Exception e){
System.out.println(e);
}
%>

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved