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



Home Answers Viewqa JSP-Servlet Storing and retrieving data alongwith image in mysql db using jsp
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Jasvinder Kaur
Storing and retrieving data alongwith image in mysql db using jsp
0 Answer(s)      4 months and 11 days ago
Posted in : JSP-Servlet


I want to store and retrieve the whole data as shown below in mysql db. I've put in my all efforts for building this jsp but I'm not able to understand where's the exact flaw in my code. Can anyone guide me through all this and help me out as it is really important for my project????? THIS IS MY DB Table. mysql> use test; Database changed mysql> desc student; +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | rollno | int(11) | YES | | NULL | | | name | varchar(30) | YES | | NULL | | | age | int(11) | YES | | NULL | | | file_data | longblob | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 4 rows in set (0.08 sec)

index.jsp

Upload page

       <tr>
           <td align="right"><b>Age:</td>
               <td ><input type="text" name="age"></td>
       </tr>

              <tr>
           <td align="right"><b> Image </td>
           <td>
                   <input name="file" type="file" id="file">
               <td>
       </tr>
             <tr>
                <td align="center">
           <input type="submit" name="Submit" value="Submit"/>
                       <input type="reset" name="Reset" value="Reset"/>
                    </td>
             </tr>
</table>
    </center>

1.jsp

<%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.zip.*"%> <% String rollno1=request.getParameter("rollno"); String name1=request.getParameter("name"); String age1=request.getParameter("age"); out.println(rollno1); out.println(name1); out.println(age1); %> <% //to get the content type information from JSP Request Header String contentType = request.getContentType(); //here we are checking the content type is not equal to Null and //as well as the passed data from mulitpart/form-data is greater than or //equal to 0 if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) { DataInputStream in = new DataInputStream(request.getInputStream()); //we are taking the length of Content type data int formDataLength = request.getContentLength(); String rollno2=new String(rollno1); String name2=new String(name1); String age2=new String(age1); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //this loop converting the uploaded file into byte code while (totalBytesRead < formDataLength) { byteRead = in.read(dataBytes, totalBytesRead, formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); //for saving the file name String saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1, contentType.length()); int pos; //extracting the index of file pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)) .getBytes()).length; // creating a new file with the same name and writing the content in new file FileOutputStream fileOut = new FileOutputStream(saveFile); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %><Br>

Roll no:
Name
You have successfully upload the file by the name of: <% out.println(saveFile); %>
<%

Connection connection = null; //String connectionURL = "jdbc:mysql://localhost:8080/test"; ResultSet rs = null; PreparedStatement pst = null; FileInputStream fis;

try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); //connection = DriverManager.getConnection(connectionURL, "root", "root"); Connection con = DriverManager.getConnection("jdbc:mysql:///test","root","123"); File f = new File(saveFile);

String query="insert into student values(?,?,?,?)"; pst= con.prepareStatement(query); //psmnt = connection.prepareStatement("insert into file(file_data) values(?)");

fis = new FileInputStream(f); pst.setString(1,rollno2); pst.setString(2,name2); pst.setString(3,age2); pst.setBinaryStream(4, (InputStream)fis, (int)(f.length())); int s = pst.executeUpdate(); if(s>0){ out.println("Uploaded successfully !"); } else{ out.println("Error!"); } } catch(Exception e){e.printStackTrace();} } %>

retrive.jsp <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %>

<% // 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; Connection connection=null; // declare InputStream object to store binary stream of given image. InputStream sImage; try { HttpSession h1=request.getSession(); String rollno=(String)h1.getAttribute("rollno"); // 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("jdbc:mysql:///test","root","123"); /* prepareStatement() is used for create statement object that is used for sending sql statements to the specified database. */ psmnt = connection.prepareStatement("SELECT file_data FROM student WHERE rollno = ?"); psmnt.setString(1,rollno); // here integer number '11' is image id from the table rs = psmnt.executeQuery(); if(rs.next()) { byte[] bytearray = new byte[1048576]; int size=0; sImage = rs.getBinaryStream(1); response.reset(); response.setContentType("image/jpeg"); while((size=sImage.read(bytearray))!= -1 ){ response.getOutputStream().write(bytearray,0,size); } } } catch(Exception ex){ out.println("error :"+ex); } finally { // close all the connections. rs.close(); psmnt.close(); connection.close(); } %>


Advertisement
View Answers

Related Tutorials/Questions & Answers:
Storing and retrieving data alongwith image in mysql db using jsp
Storing and retrieving data alongwith image in mysql db using jsp  I want to store and retrieve the whole data as shown below in mysql db. I've put in my all efforts for building this jsp but I'm not able to understand where's
retrieving image from mysql db with standard height and width
retrieving image from mysql db with standard height and width  Hi . Here is my code to retrieve an image from mysql db. Its working properly. But i... = "jdbc:mysql://localhost:3306/"; String dbName = "db"; String userName
Advertisements
retrieving image from mysql db with standard height and width
retrieving image from mysql db with standard height and width  Hi . Here is my code to retrieve an image from mysql db. Its working properly. But i... = "jdbc:mysql://localhost:3306/"; String dbName = "db"; String userName
Saving image and text in mysql db using spring?
Saving image and text in mysql db using spring?  Hii, My requirement is to save the employee details(including image) into mysql db.i want to save the image location in db .And actual image(binary formate) should save
retrieving image files - JSP-Servlet
retrieving image files  How to retrieve multiple images at a time from mysql database using JSP
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
upload csv to mysql db using jsp upload
upload csv to mysql db using jsp upload  Hello all, Please give me the code to uplad .csv from jsp page and insert values into MySQl db. I have a table with 8 cloumns.(MDN--varchar(30),Otafdate date,crt varchar(30),dmdn
retrieving info from DB using struts?
retrieving info from DB using struts?  Hi. I was looking info about retrieving info from a database using struts. I need a .java that I suppose connects to a database and show the info on a jsp. Somebody could help me
retrieving from db - JSP-Servlet
retrieving from db  hello' I am trying to write my first application...: Exception in JSP: /jsp1/mydb.jsp:25 22: if(request.getParameter("action...; Hi Retrive value from database Retrive data from
Retrieving data from data base using jsp combo box
Retrieving data from data base using jsp combo box  Hi guys please help me , i have on GUI page int that Server type(like apache,jboss,weblogic) one combo box is there and another filed is version of the server(like 1.0,2.0) like
How to insert data from a combobox and textbox values into DB using JSP?
How to insert data from a combobox and textbox values into DB using JSP?  hi, How to insert a comb-box and a text box values in to DB using JSP? @DB... and textbox2? into MYSQL DB
Produces XML file but format not correct for storing data using JSP and XML
Produces XML file but format not correct for storing data using JSP and XML  hii I have created a project using JSP and XML as database to store data entered by user in XML file ,It stores data entered in XML file
Image upload in mysql database using jsp servlet
Image upload in mysql database using jsp servlet  Hello, I need code to insert image in mysql database, I have seen the code which is already in your... different format of file to upload into mysql db like pdf and doc file
how to fetch image from mysql using jsp
how to fetch image from mysql using jsp  how to fetch image from mysql using jsp
Connecting to MySQL database and retrieving and displaying data in JSP page
Connecting to MySQL database and retrieving and displaying data in JSP page...; This tutorial shows you how to connect to MySQL database and retrieve the data... web application. Creating Table in the database. Using a JDBC driver
Storing Multiple image in sql using java
Storing Multiple image in sql using java  Hi, How to store and retrieve a multiple image in sql using java but already i have created sql table if i want to insert a image while runtime execution. Can anyone tell me solution
How to get data from DB in to Text box by using Jsp & Ajax
How to get data from DB in to Text box by using Jsp & Ajax   I want to get the data from database in to text box in a jsp page by using Ajax. If I enter the letter A in the textbox it should show only those values which starts
Retrieving images from the oracle database using jsp and create that rretrieved image as hyperlink
Retrieving images from the oracle database using jsp and create that rretrieved image as hyperlink  I want to insert images into oracle database.I want to retrieve images using jsp and I want to display on the browser.The
JSP:select image for db and display in image tag
JSP:select image for db and display in image tag  Hi, i am new to this forum. My query is that, i am trying to display image in image tag but i am not getting, how to do this by using this code. plz help me. if(rs1.next
JSP:select image for db and display in image tag
JSP:select image for db and display in image tag  Hi, i am new to this forum. My query is that, i am trying to display image in tag but i am not getting, how to do this by using this code. plz help me. if(rs1.next
JSP:select image for db and display in image tag
JSP:select image for db and display in image tag  Hi, i am new to this forum. My query is that, i am trying to display image in tag but i am not getting, how to do this by using this code. plz help me. if(rs1.next
Insert Blob(Image) in Mysql table using JSP
Insert Blob(Image) in Mysql table using JSP In this Section, we will insert blob data(image) in Mysql database table using JSP code. A Blob stores a binary... Blob(Image) in Mysql table using JSP Download Source Code
Retrieving XML Data Using GWT
Retrieving XML Data Using GWT       This Example Describes the way of retrieving XML file Data from the server using GWT. The basic building block for running this example which we
How to "Get data froom MySQL DB on giving a value in a tex-box in a JSP file."
How to "Get data froom MySQL DB on giving a value in a tex-box in a JSP file."  Hi, How to get data from MYSQL Database tables on giving a "text" in a text-box in a JSP file. Ex:dept table; if we give dept_no in a text-box in 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 name city and upload image in database using mysql and jsp
access image from ajax store in mysql using jsp
access image from ajax store in mysql using jsp  access image from ajax store in mysql using jsp (code to access image captured by camera and store in mysql
retrieve related data from database using jsp and mysql
retrieve related data from database using jsp and mysql  Hi sir, please give some example of jsp code for retrieving mysql database values in multiple dropdown list. if we change a value in a dropdown its related value must
How to use next and previous button(or href) for database table that is retrieved from MySQL DB using jsp,jstl,javascript
that is retrieved from MySQL DB using jsp,jstl,javascript  when click on the next..._name where id=..; database:mysql technology:jsp,servlet,javascript,jstl  .../paging.shtml http://www.roseindia.net/jsp/data-grid.shtml Pagination in JSP http
Image retrieval from mysql - JSP-Servlet
Image retrieval from mysql  Hai friends, I used the following code to insert image into mysql db. Db data type is blob.... Inserted file image does not exist database limit. But, while retrieving image
Exporting data from mysql into csv using jsp
Exporting data from mysql into csv using jsp  Hi friends.... I want to export the data from mysql to csv file using... i am having 30 columns in my database.. Eg- text1,text2,text3,....,upto text30... i want to export this data
Display Blob(Image) from Mysql table using JSP
Display Blob(Image) from Mysql table using JSP In this section, we will display blob data(image) from Mysql database table using JSP code. A Blob stores... : Insert Blob(Image) in Mysql table using JSP Download Source Code
 

 

 

DMCA.com