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
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)
);
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