image & database

i wana know how to insert image into database.& How to view or get that image on a page or frame ?Suppose image name is pp.jpg & placed in C:\pp.jpgPl tell me or guid me.Thanks in advanceHarsh
[263 byte] By [myharshdesignera] at [2007-10-2 13:26:28]
# 1
any one know how to do that ?
myharshdesignera at 2007-7-13 11:06:53 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
which database are you using?if you search the forums, you'll find many many posts regarding the exact issue.
shahzadqua at 2007-7-13 11:06:53 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
i am using mysql.so tell me for bothMYSQL & MSSQLis there any data type in which i can show image.like intis there any IMAGE type data type in java?wana know how to store & retrive image from database
myharshdesignera at 2007-7-13 11:06:53 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
Honeybunches!It's been awhile since I've seen you. How have things been? School going well? Look into BLOBs.
myharshdesignera at 2007-7-13 11:06:53 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Hi

i am sending a sample code of inserting image data into data base

this task can be performed by two ways

A)

1.

upload image to server side and send that image file in the secify directory

2. store image path with name at the database.

B)/////////REMEMBER DATABASE COLUMN NAME IS BLOB

File file =new File(url);

FileInputStream fis=new FileInputstream(file);

.............

create connection

query="INsert into tablename values(?,?........)"

PrepareStatement pstmt=con.createPrepareStatement(query);

............

.setBinaryObject(columnindes, fix, file.length())

close connection and statement

try

it

skpmcaa at 2007-7-13 11:06:53 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Hi,

I have tryed the code write in the last post, but when i try to upload an image of 3k i have an error.

I have looking for a solution in this forum but I have always the error below.

This is the first time that i try to upload an image into MSSQL and I'm in troube.

PLEASE HELP ME!!

Attilio

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]String or binary data would be truncated.

at it.r_c_s.inrebus.db.DAOFactory.insertImage(DAOFactory.java:823)

at it.r_c_s.inrebus.Servlet.InsertAnnuncio.doGet(InsertAnnuncio.java:131)

at it.r_c_s.inrebus.Servlet.InsertAnnuncio.doPost(InsertAnnuncio.java:290)

at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))

at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))

at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java(Compiled Code))

at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java(Compiled Code))

at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java(Compiled Code))

at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java(Inlined Compiled Code))

at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java(Compiled Code))

at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java(Compiled Code))

at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java(Inlined Compiled Code))

at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java(Compiled Code))

at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java(Compiled Code))

at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java(Compiled Code))

at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java(Compiled Code))

at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java(Compiled Code))

at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java(Compiled Code))

at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java(Compiled Code))

at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java(Compiled Code))

at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java(Compiled Code))

at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java(Compiled Code))

at com.ibm.ws.http.HttpConnection.run(HttpConnection.java(Compiled Code))

at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))

******* java code ************

byte[] img = image.getB();

int imgLenght = img.length;

ps = conn.prepareStatement(query);

ps.setString(1,image.getName());

ByteArrayInputStream in = new ByteArrayInputStream(image.getB());

ps.setBinaryStream(2,in,imgLenght);

ps.executeUpdate();

********* DB Table *************

create table BACHECA_IMMAGINI(

ID int NOT NULL identity(1,1),

NOME_IMG VARCHAR(30) not null,

BLOB_IMG image not null,

PRIMARY KEY (ID)

);

4124123a at 2007-7-13 11:06:53 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
Hi,If you can insert an image of 2k then I think that the problem is on MySQL or SQLServer. Did you check your configuration file? It may have a limit and that is why you have this exception. I know that in MySQL there is a default limit of 1ko.
Marco_Poloa at 2007-7-13 11:06:53 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

There is not a problem in config file and no problem in JDBC driver.

It's a problem with SQLServer.

To resolve the problem il sufficient to make this code

boolean isInserted = false;

PreparedStatement ps = null;

String query = "";

if(conn!=null){

query = "SET ANSI_WARNINGS OFF; INSERT INTO "+

IMMAGINI+"("+

IMMAGINI_NOME+","+

IMMAGINI_BLOB+","+

IMMAGINI_SIZE+

") VALUES (?,?,?)";

if(!"".equals(query)){

try {

InputStreamReader inputStreamReader = newInputStreamReader(image.getInputStream());

ps = conn.prepareStatement(query);

ps.setString(1,image.getName());

ps.setBinaryStream(2, image.getInputStream(), image.getSize());

ps.setInt(3,image.getSize());

ps.executeUpdate();

isInserted = true;

ps.close();

try {

inputStreamReader.close();

} catch (IOException e1) {

Bacheca.logger.error("Errore ",e1.fillInStackTrace());

}

} catch (SQLException e) {

Bacheca.logger.error("Errore nell'inserimento dell'immagine ",e.fillInStackTrace());

isInserted=false;

}

}

}

Thanks for your help.

Attilio

4124123a at 2007-7-13 11:06:53 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...