how to store image upload path to mssql database
hi there!!,
i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit button, the id in the database do increment by 1 however under the image column it state null.
i set my image data type as varchar
this is a sample of my code:
image.jsp
<%--
Document : image
Created on : Jul 26, 2012, 9:00:33 PM
Author : acer
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<FORM ENCTYPE="multipart/form-data" ACTION="image2" METHOD=POST>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<td colspan="2"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr><td colspan="2" > </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="FileUpload" TYPE="file" value="./books/"></td>
</tr>
<tr><td colspan="2" > </td></tr>
<tr><td colspan="2"><input type="submit" value="Send File"> </td></tr>
<table>
</FORM>
</body>
</html>
image.java(Servlet)
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String image = request.getParameter("file");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connURL = "jdbc:sqlserver://localhost:1433;"
+ "databaseName=Book Avenue Tables; user=sa; password=12345678;";
Connection conn = DriverManager.getConnection(connURL);
Statement stmt = conn.createStatement();
String sqlStr = "INSERT INTO testimage(testimage)"
+ " VALUES ('" + image + "')";
int rec = stmt.executeUpdate(sqlStr);
if (rec > 0) {
out.println("upload succesffully");
}
} catch (Exception e) {
out.println(e);
} finally {
out.close();
}
}
View Answers
July 27, 2012 at 5:19 PM
Here is a jsp code that allow the user to upload a file and save the file path to database.
1)form.jsp:
<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="uploadFile.jsp" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>
<table>
</center>
</FORM>
</HTML>
2)uploadFile.jsp:
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
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;
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;
File ff = new File("C:/UploadedFiles/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<%out.println(saveFile);%></td></tr></table>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/roseindia";
PreparedStatement psmnt = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
psmnt = connection.prepareStatement("insert into file(file_path) values(?)");
psmnt.setString(1, ff.getPath());
int s = psmnt.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");
}
else{
System.out.println("Error!");
}
<%
}
catch(Exception e){
e.printStackTrace();
}
}
%>
Related Tutorials/Questions & Answers:
how to store image upload path to mssql databasehow to
store image upload path to
mssql database hi there!!,
i need help in storing the
image upload path into the
database. basically i just use file select to
upload the
image from jsp to
database. however when i click submit
how to store image upload path to mssql databasehow to
store image upload path to
mssql database hi there!!,
i need help in storing the
image upload path into the
database. basically i just use file select to
upload the
image from jsp to
database. however when i click submit
Advertisements
how to store image upload path to mssql databasehow to
store image upload path to
mssql database hi there!!,
i need help in storing the
image upload path into the
database. basically i just use file select to
upload the
image from jsp to
database. however when i click submit
how to store image upload path to mssql databasehow to
store image upload path to
mssql database hi there!!,
i need help in storing the
image upload path into the
database. basically i just use file select to
upload the
image from jsp to
database. however when i click submit
how to upload image from jsp to mssqlhow to
upload image from jsp to mssql hi there!!,
i'm using jsp and servlet to
upload images to the
database. however i have difficulty in uploading
database. hope u can help
in my
database:
i have imagetbl that contain
image how to upload image from jsp to mssqlhow to
upload image from jsp to mssql hi there!!,
i'm using jsp and servlet to
upload images to the
database. however i have difficulty in uploading
database. hope u can help
in my
database:
i have imagetbl that contain
image How to store image into databaseHow to
store image into database Hi, all I want to
store image into
database using Java. Can anyone help me that
how can i
store image into
database... through the following link
How To
Store Image Into MySQL Using Java
How to store an image in databaseHow to
store an
image in database Hi...........
How to
store an
image in postgresql using a query. I mean tell me the way to
store an
image using datatype.
I am using the datatype bytea but tell me
how to insert the
image how to store and retrieve image from databasehow 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
Unable to store the image into database);
}
Hi,
Please check the thread
How to Insert
image into
database...Unable to
store the
image into database Hello,
I have created below... button. The problem is coming due to JSP page is not able to find complete
path upload image to databaseupload image to database i am try to
upload image to MySql
database using netbeans.
when jsp execute it return no error. but also data does inserted in
database. i am using blob datatype and preopared statement
image store in database - JDBC to
store image into
database. Check at http://www.roseindia.net/servlets/insert...
image store in database Dear Deepak Sir,
If I want to
store image...;
Inserting
Image in
Database Table
http://www.roseindia.net/jdbc/jdbc-mysql/insert
store and retrive image from the databasestore and retrive
image from the database please provide me with the code and the explanation of what each line does for the below query.
-
how to
store and retreive images from sql
database using sql commands
-
how to
store How to store url path?How to
store url
path?
Image is stored in physical directory like...() + ".PNG"; this physical directory is working fine
but I want
store in url
path like this String file = "http://www.queen.com/website/screenshots/" + username
inserting an path of an image in database - JDBC time its full
path should be inserted in the
database(MS Sql 2000)..
I m able to save it in folder..but can you plz tell me
how an the full
path of
image can...inserting an
path of an
image in database hello
kindly help related
How to store url path in file ?How to
store url
path in file ? Hi,
How to
store url
path in file ?
this my program
public class
Image implements Runnable...";
This is
store phiscal directory but i want
store url
path like
Upload Image to Database through Servlet - JSP-ServletUpload Image to
Database through Servlet Hello,
I make a application from where I
upload the
Image from local disk then
store in DB.Before storing...:101249][ServletContext(id=29091418,name=
Upload,context-
path=/
Upload)]: Servlet class
Image upload in mysql database using jsp servletImage 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 portal but it is not inserting
image into
database it save in the folder
image upload in webapp/upload folderimage upload in webapp/
upload folder sir i want to
store upload image in my project directory WebApp/
Upload_
image/
pls send the jsp servlet code
when i
upload the
image one error found
"system cannot found the specified
path How to Store Image using JSFHow to
Store Image using JSF Hi
How to
upload images in db. using jsf. For jsf photo uploading .. I used this one code for
upload image..
But this code haven't option to
upload any images . i want to
store image in db
store and retrive image from database - JDBCstore and retrive
image from database
how to
store and retrive an
image in
database using java? Hi friend,
Code for
store image...());
}
}
}
For retrieve
image from
database visit to :
http
image is display from path of mysql databaseimage is display from
path of mysql database <%@ page import...();
%>You have successfully
upload the file:<%out.println(saveFile);%>
<..._
path) values(?)");
psmnt.setString(1, ff.getPath());
int s = psmnt.executeUpdate
upload an imageupload an image Hello, i would like to
upload an
image to the
database.
how can i do it? what field type should i set in the
database? thanx
How To Store Image Into MySQL Using Java how to
store an
image into the
database
using Java and MySQL.
This example explains you about all the steps that
how to
store image into
MySQL
database... you about
how to
store image into
database using Java. We will use the MySQL
image upload in javaimage upload in java Hi, I am working with java. In my application... select any
image
i want to
store that selected
image into specified folder and that
path stored into
database, for furhter retrivation
.
Please guide me