Inserting Image(s)

Inserting Image(s)

Hello, I need sample code using java servlet and html form and explanation on how to insert and retrieve image from mysql.

View Answers

April 28, 2008 at 6:42 PM

Hi friend,

This is insert image code.

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InsertImageIntoDatabase extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "register";
Statement stmt = null;
ResultSet rs = null;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url+db,"root","root");
PreparedStatement pst = con.prepareStatement("insert into uploadfile values(?,?,?)");
File file = new File("C:/apache-tomcat-5.5.23/webapps/jsp/red_rose2.gif");
FileInputStream fis = new FileInputStream(file);
pst.setString(1, "amardeep");
pst.setString(2, "patel");
pst.setBinaryStream(3,fis,fis.available());
int i = pst.executeUpdate();
if(i!=0){
out.println("image has been inserted");
}
else{
out.println("image is not inserted");
}
}
catch (Exception e){
System.out.println(e);
}
}
}

--------------------------
this is servlet mapping

<servlet>
<servlet-name>Inserimage</servlet-name>
<servlet-class>InsertImageIntoDatabase</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Inserimage</servlet-name>
<url-pattern>/InsertImageIntoDatabase</url-pattern>
</servlet-mapping>


April 28, 2008 at 6:44 PM

This is retrive image code from database.

import java.sql.*;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RetriveImageExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Blob photo = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String query = "SELECT image from uploadfile where firstname = 'amardeep';";
ServletOutputStream out = response.getOutputStream();
try {
conn = getMySqlConnection();
}
catch (Exception e){
response.setContentType("text/html");
out.println("<html><head><title>Person Photo</title></head>");
out.println("<body><h1>Database Connection Problem.</h1></body></html>");
return;
}
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if (rs.next()) {
photo = rs.getBlob(1);
}
else {
response.setContentType("text/html");
out.println("<html><head><title>Person Photo</title></head>");
out.println("<body><h1>No photo found for id= 001 </h1></body></html>");
return;
}
response.setContentType("image/gif");
InputStream in = photo.getBinaryStream();
int length = (int) photo.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
System.out.println("writing " + length + " bytes");
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
catch (SQLException e) {
response.setContentType("text/html");
out.println("<html><head><title>Error: Person Photo</title></head>");
out.println("<body><h1>Error=" + e.getMessage() + "</h1></body></html>");
return;
}
finally {
try {
rs.close();
stmt.close();
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}

public static Connection getMySqlConnection() throws Exception {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";;
String db = "register";
String username = "root";
String password = "root";
Class.forName(driver);
Connection con = DriverManager.getConnection(url+db,"root","root");
return con;
}
}

----------------

<servlet>
<servlet-name>Retriveimage</servlet-name>
<servlet-class>RetriveImageExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Retriveimage</servlet-name>
<url-pattern>/RetriveImageExample</url-pattern>
</servlet-mapping>
----------------------------









Related Tutorials/Questions & Answers:
Inserting Image(s) - JSP-Servlet
Inserting Image(s)  Hello Guys, Thank you so much for your support so far. I appreciate everything you guys have done for me. Please what I wanted was java servlet or jsp codes that I can use to insert and retrieve image from MS
Inserting Image(s) - JSP-Servlet
Inserting Image(s)  Hello, I need sample code using java servlet and html form and explanation on how to insert and retrieve image from mysql.  Hi friend,This is insert image code.import java.sql.*;import java.io.
Advertisements
inserting image into database
inserting image into database  how to insert image into database using struts frame work and spring JDBC
Inserting Image into table
Inserting Image into table  In this section , we will insert a image into a table. For inserting image, table's field (in which image...)) For inserting whole image, we have to create an 'InputStream'
inserting an path of an image in database - JDBC
inserting an path of an image in database  hello kindly help related... an image using web cam.... and when the image is saved in a project at the same... to save it in folder..but can you plz tell me how an the full path of image can
Exception while inserting image in oracle using java
Exception while inserting image in oracle using java  import java.sql.*; import java.io.*; class Oracle2 { public static void main(String args...()); System.out.println("Image length: "+f.length()); System.out.println("No.of rows
how to get the image path when inserting the image into pdf file in jsp - JSP-Servlet
how to get the image path when inserting the image into pdf file in jsp  Hi Friend, my image path;C:/images/photo.jpg. i am getting the below error error: The type Image is ambiguous document.open(); Image
inserting image in mysql database using browse button in servlets
inserting image in mysql database using browse button in servlets  Hi,I want to insert a image in mysql database using browse button. and, i want to display all inserted images in a form from there i want to download the image
how to get the image path when inserting the image into pdf file in jsp - JSP-Servlet
how to get the image path when inserting the image into pdf file in jsp ...")); document.open(); Image image = Image.getInstance("C:/image2.png"); Image image1 = Image.getInstance("C:/node.jpg"); document.add(image); document.add
i am inserting an image into database but it is showing relative path not absolute path
i am inserting an image into database but it is showing relative path not absolute path   hi my first page......... Image Enter your name: Upload photo: Father name: Age: Username: Password Qualification: Gender: Phone
i am inserting an image into database but it is showing relative path not absolute path
i am inserting an image into database but it is showing relative path not absolute path   hi my first page......... Image Enter your name: Upload photo: Father name: Age: Username: Password Qualification: Gender: Phone
i am inserting an image into database but it is showing relative path not absolute path
i am inserting an image into database but it is showing relative path not absolute path   hi my first page......... <html> <head> <title>Image</title> </head> <body bgcolor="lavender">
data (image ,audio) inserting to data base (oracle 10g) and retriving the same data from jsp
data (image ,audio) inserting to data base (oracle 10g) and retriving the same data from jsp   data (image ,audio) inserting to data base (oracle 10g) and retriving the same data from jsp with script of data base plz help me
Inserting Image in Database Table
Inserting Image in Database Table   ... (Image table). For inserting an image in table, we have used the SQL statement... an image is inserted into the database, it displays ?Inserting Successfully
Inserting Image in a database Table
Inserting Image in MySql Database Table... understand the concept of inserting a image in the database table, so go through... that along with the name of the person and its information, his image should also
Inserting image in the pdf file
Inserting image in the pdf file   ... insert a image in a pdf file irrespective of the fact whether it exists...() gets the image. After that add the content to the document. Create a Paragraph
Inserting Image In Excel Sheet Using JSP
Inserting Image In Excel Sheet Using JSP... and display image on that. We can request that the browser open the results... we insert the image and the output will display in excel format with image.You
Insert Image into Database Using Servlet
Insert  Image into Database Using Servlet       This example illustrate the process of inserting image into database table using Servlet. This type of program is useful in social
Insert  Image into Database Using Servlet
of inserting image into database table using Servlet. This type of program is useful... Insert  Image into Database Using Servlet   ... photograph of the user. If the image is stored in the database you can easily
inserting picture in ms access
inserting picture in ms access  hi i am sanatan, how to insert... into user(name,address,image) values(?,?,?)"); pre.setString(1...()); pre.executeUpdate(); out.println("Inserting Successfully
image
image  how to add the image in servlet code
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
image
image   Dear every body please help me how to add and retrive image and video into oracle 11g using jsp
inserting value - JSP-Servlet
inserting value  sir i want to create a web application when we use the jsp inserting the value in the jsp after that i want these value inserted the database mySQl with the help of servlet. Inserting form data   
inserting data into datafields
inserting data into datafields  I was raised with a query.... there is a button, on click on it....... few fields regenerates and the details entered there should be inserted into Database Number of times we click that button
Inserting Data into mysql - SQL
Inserting Data into mysql  My sincere apologize for this post. I noticed there is no category for php. However am hoping my question will fit... (using php)and inserting the details in a mysql database. Lets take for instance
php inserting date into mysql
php inserting date into mysql  php inserting date into mysql   Here is a php code that inserts date into mysql database. In the given code, data is a database table. <?php table $query_manual = "INSERT INTO data
insert image - JSP-Servlet
the code for inserting image into database using JSP   Hi Friend, Try...insert image  hi friends i am mahesh i am trying to insert image...; FileInputStream fis; File image = new File("C:/image.jpg"); psmnt
duplicate records inserting
duplicate records inserting  in the application i have provided the text filed on entering and submit the record is getting inserted. my question is i want to avoid duplicate values getting inserted. ex. i am inserting name
inserting multiple file formats into database
inserting multiple file formats into database  hi i want to insert multiple file format like .pdf.doc.zip into mysql database using jsp
inserting dropdown values into database table
inserting dropdown values into database table   hi i want to insert dropdown values into a database table by using jsp
Problem in inserting clob data in jsp
Problem in inserting clob data in jsp  how to insert any rich text editor data (which have more than 32766 characters) in a clob type column of oracle database with jsp
image Processing
image Processing   BCIF Image Compresssion Algorithm s alossless image Compression algorithm ,Pleas Help Me weather it can support only 24 bit bmp images
Inserting content(DOM Insertion)
Inserting content(DOM Insertion)       Inserting content(DOM Insertion) Inserting... : .unwrap( )ADS_TO_REPLACE_2 The .unwrap() method removes the element's
Inserting id in place of name to database
Inserting id in place of name to database  Hello, I have a database department where I have "id" and "name". I am fetching this data to a jsp form in dropdown list displaying "name" there and Now I wanted to insert data from
hibernate firstExample not inserting data - Hibernate
hibernate firstExample not inserting data  hello all , i followed... problem is data is not inserting into DB even though the program is executed...("inserting records in Contact table"); Contact contactBO = new Contact
ModuleNotFoundError: No module named 's'
ModuleNotFoundError: No module named 's'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 's' How to remove the ModuleNotFoundError: No module named 's' error
ModuleNotFoundError: No module named 's'
ModuleNotFoundError: No module named 's'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 's' How to remove the ModuleNotFoundError: No module named 's' error
what?s the value of $$b?
what?s the value of $$b?  If the variable $a is equal to 5 and variable $b is equal to character a, what?s the value of $$b
2's Complement
2's Complement  hi Can anyone plsss tell me the 2's complement java program... Thanks in advance
image store in database - JDBC
; Inserting Image in Database Table http://www.roseindia.net/jdbc/jdbc-mysql/insert-image-in-mysql.shtml regards meeya  Hi, Here is another example...image store in database  Dear Deepak Sir, If I want to store image
Inserting a value to an Enum field in Table
Inserting a value to an Enum field in Table  I'm writing a code that creates a user account and sends the result to the user table in a mysql database. For example, in the user table I have: username varchar(15) PRIMARY KEY
JAVA S/W download
JAVA S/W download  Hi I'm new to JAVA, I want to download JAVA S/W from where I need to download. Please help me with the link. Thank you,   Hi ! (adsbygoogle = window.adsbygoogle || []).push({}); u can
s:textfield - Struts
s:textfield  I am using the s:textfield tag in an appication and need to display % after the field on the same line. How would I go about doing this?  Hi friend, Textfield Tag Example
s per ur answer
s per ur answer  i cannot understand please explain in detail 3)Create web.xml and classes folder inside the WEB_INF folder of web application folder.   Hello Friend, We were explaining you the directory structure
Data is not inserting correctly in mysql database.
Data is not inserting correctly in mysql database.  Hello Sir, below is my code to insert data into database by list iteration through for loop but it is not getting inserted ..it is taking only one value
inserting text into text file using java application
inserting text into text file using java application  Hi, I want to insert a text or string into a text file using java application
image scrolling
image scrolling  A brief description of scrolling image

Ads