Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Insert Image into Mysql Database through Simple Java Code 
 

This is detailed simple java code that how save image into mysql database. Before running this java code you need to create data base and table to save image in same database.

 

Insert Image into Mysql Database through Simple Java Code

                         

This is detailed simple java code that how save image into mysql database. Before running this java code you need to create data base and table to save image in same database. In the example given below we have used database 'mahendra' and table 'save_image'.

Structure of table 'save_image'

First create database named 'mahendra' by query given below:

mysql> create database mahendra;

and create table by query given below:

CREATE TABLE save_image (             
              id int(5) NOT NULL auto_increment,  
              name varchar(25) default NULL,      
              city varchar(20) default NULL,      
              image blob,                         
              Phone varchar(15) default NULL,     
              PRIMARY KEY  (`id`)                   
 );

Save table 'save_image' in database 'mahendra'. Before running this java code you need mysql connector jar in the jdk1.6.0_01./lib.

SaveImageToDatabase java file provides full code for uploading image in database.

SaveImageToDatabase.java

import java.sql.*;
import java.io.*;
class SaveImageToDatabase {
   public static void main(String[] args) throws SQLException {
   // declare a connection by using Connection interface 
   Connection connection = null;
   /* Create string of connection url within specified format with machine 
   name, port number and database name. Here machine name id localhost 
   and database name is mahendra. */
   String connectionURL = "jdbc:mysql://localhost:3306/mahendra";
  /*declare a resultSet that works as a table resulted by execute a specified 
    sql query. */
  ResultSet rs = null;
  // Declare prepare statement.
  PreparedStatement psmnt = null;
  // declare FileInputStream object to store binary stream of given image.
  FileInputStream fis;
  try {
 // Load JDBC driver "com.mysql.jdbc.Driver"
 Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes 
parameters of string type connection url, user name and password to 
connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "root");
// create a file object for image by specifying full path of image as parameter.
File image = new File("C:/image.jpg");
/* prepareStatement() is used for create statement object that is 
used for sending sql statements to the specified database. */
psmnt = connection.prepareStatement
("insert into save_image(name, city, image, Phone) "+ "values(?,?,?,?)");
psmnt.setString(1,"mahendra");
psmnt.setString(2,"Delhi");
psmnt.setString(4,"123456");
fis = new FileInputStream(image);
psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query 
 insert data and image from specified address. */ 
int s = psmnt.executeUpdate();
if(s>0) {
  System.out.println("Uploaded successfully !");
 }
else {
System.out.println("unsucessfull to upload image.");
  }
}
// catch if found any exception during rum time.
     catch (Exception ex) {
System.out.println("Found some error : "+ex);
}
finally {
// close all the connections.
connection.close();
psmnt.close();
  }
 }
}

Output of the program:

Download Source Code

                         

» View all related tutorials
Related Tags: java sql mysql c database api ide jdbc data batch application io get vi exec state int bat this id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

it is good approach to meet
the point of uploading , but
why do u use so many fields,
the image and id fields were
enough, b/c other wise it leads to cumbersome code..
any way thaks mahadra

Posted by targeta on Wednesday, 12.10.08 @ 03:00am | #82591

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.