Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners how to insert and retrieve an image from mysql using java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


siva
how to insert and retrieve an image from mysql using java
1 Answer(s)      6 years ago
Posted in : Java Beginners



Advertisement
View Answers

January 19, 2009 at 12:25 AM


Hi friend,

Code to insert image using 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 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();
}
}
}

Thanks


Related Tutorials/Questions & Answers:
how to insert and retrieve an image from mysql using java - Java Beginners
how to insert and retrieve an image from mysql using java  how to insert and retrieve an image from mysql using java?  Hi friend, Code to insert image using java : import java.sql.*; import java.io.*; class
how to retrieve image from mysql using java - Java Beginners
how to retrieve image from mysql using java  hi i am using jsf and java IDE netbean,database mysql.how to retrieve image from mysql database. please help me   Hi friend, I am sending you a link. I hope
Advertisements
How To Retrieve Image From From MySQL Using Java
How To Retrieve Image From From MySQL Using Java In this section we will discuss about how to retrieve image from the MySQL using Java. This example explains you about all the steps that how to retrieve image from MySQL database
how to retrieve image from mysql database using java and show it in HTML img tag ?
how to retrieve image from mysql database using java and show it in HTML img tag ?  how to retrieve image from mysql database using java and show it in HTML img tag
Insert or retrieve image into oracle 10g by using java
Insert or retrieve image into oracle 10g by using java   How can i insert or retrieve image into oracle10g using java plz i need it urgently,need guidance to do this plz
How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java
How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java  How To Store... and then retrieve them in image gallery using java
how to fetch image from mysql using jsp
how to fetch image from mysql using jsp  how to fetch image from mysql using jsp
HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVA
HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVA  H ello, i have an xml code, i need to save it into mysql 5.5 server database using java. i also want to be able to retrieve it as well. please note that i want it saved
How to insert image in sql database from user using servlet
How to insert image in sql database from user using servlet  pls tell me accept image from user and insert image in sql server 2005 database using servlet and jsp
How to retrieve image from database
How to retrieve image from database  hi........ How to retrieve image from database when it is stored. I have stored image in postgresql and want to retrieve in java form. Can u tel me hw to do? I am trying n able to do also
Insert and Retrieve Image - JSP-Servlet
Insert and Retrieve Image  Hello, I need source code using java servlet or jsp and html form and brief explanations on how to insert and retrieve image from Microsoft sql database 2000 not Mysql please as sent to me earlier
How to insert and retreive an image in mysql database using spring mvc framework???
How to insert and retreive an image in mysql database using spring mvc framework???  I ve solved this problem based on the Product Search Application. Those who need this concept and code segment kindly copy it. Thanks and All
Retrieve image from mysql database through jsp
Retrieve image from mysql database through jsp... to retrieve image from mysql database through jsp code. First create a database... will retrieve image of specified 'id' value. In this example we will retrieve
how to store and retrieve image from database
how to store and retrieve image from database  how to store and retrieve images into database(oracle) and how to retrive images from database using jsp   Here is a jsp code that insert and retrieve image from mysql
image retrieve - Java Beginners
Image Retrieve  How to retrieve the image from mysql field using java
Store image from html img tag into mysql db using java
Store image from html img tag into mysql db using java  Hi. How to get the image displayed in the < img > tag of HTML and store it in the mysql database using java? Thanks in advance
how to insert, retrieve data from,to db(code)....
how to insert, retrieve data from,to db(code)....  Hi..... i ve... that data will b stored in db(m using sybase). 2.also hw to retrieve the data from... validations for username, password using javascript,hw to write that,n in wch pgm
how to insert, retrieve data from,to db(code)....
how to insert, retrieve data from,to db(code)....  Hi, i have created... will b stored in db(m using sybase). 2.also hw to retrieve the data from db. cn u... for username, password using javascript,hw to write that,n in wch pgm i ve to write(i
<img src=""> using retrieve image from database using jsp
using retrieve image from database using jsp  how to <img src="" > tag using retrieve image from database and display in jsp
how to insert image into server
how to insert image into server  how to insert an image into sql server and retrieve it using jSP
how we retrieve image from database
how we retrieve image from database  my code is:- $search=$_POST...;lt;/table&amp;amp;gt;"; mysql_close($con);   PHP retrieve image from mysql database: <?php mysql_connect("localhost","username","password
insert name city and upload image in database using mysql and jsp
insert name city and upload image in database using mysql and jsp   insert name city and upload image in database using mysql and jsp
Insert image from user using and save in database
Insert image from user using and save in database  when i am trying to upload a image from user and trying to save into oracle9i database....   dear sir or madam, i wont to How to Store Image Into The Database
How to Retrieve Data from the database and write into excel file using Java
How to Retrieve Data from the database and write into excel file using Java  Hi, I am trying to develop an small application where i trying to retrieve Data from the database and store the details in excel file. Please can
How To Store Image Into MySQL Using Java
How To Store Image Into MySQL Using Java In this section we will discuss about how to store an image into the database using Java and MySQL. This example... using Java. We will use the MySQL database system to store the image
insert name city image in database using mysql and jsp
insert name city image in database using mysql and jsp  how to insert name ,city and image in database in mysql and jsp   Here is an example in jsp that insert name, city and image to database. <%@ page import
how to upload an image from a jsp page to a mysql database table using jsp
how to upload an image from a jsp page to a mysql database table using jsp  how to upload an image from a jsp page to a mysql database table using jspstrong text
How to Retrieve Data from Facebook Using PHP
How to Retrieve Data from Facebook Using PHP  is this link has right content regards that?? http://techpdf.co.cc/blog/how-to-retrieve-data-from-facebook-using-php
How to Retrieve Data from Facebook Using PHP
How to Retrieve Data from Facebook Using PHP  is this link has right content regards that?? http://techpdf.co.cc/blog/how-to-retrieve-data-from-facebook-using-php
how to send and retrieve image along with text to and from database
how to send and retrieve image along with text to and from database  im using app engine and i have two logics from different sources and i want... image generator. im so stumped right now and i dont know how to combine the two
how to store and retrieve image using struts2 and hibernate integration
how to store and retrieve image using struts2 and hibernate integration  how to store and retrieve image using struts2 and hibernate integration? plz help !! thanks in advance
Advertisements
 

 

 

DMCA.com