Hi ,
I am facing problem in inserting images in Database through JSP.I am using Blob as the datatype in Oracle.Following
Following is the code of the JSP
<html>
<body bgcolor=fuschia>
<h1 align=center><i>EMPLOYEE RECORD</I><br>
<table bgcolor=lightyellow border=2 align=center>
<%@ page language ="java" import ="java.sql.*,java.util.*,java.io.*" %>
<%
byte buf[] = new byte[40960];
try {
FileInputStream in = new FileInputStream("/Sea1.jpg");
in.read(buf, 0, 40960);
}
catch (Exception e)
{
System.out.println("Error: " + e.toString());
}
String str = new String(buf, 0);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:hello","techno","c");
Statement st = conn.createStatement();
str = str.trim();
String qry= "insert into LOBTABLE values(200 ,EMPTY_BLOB())";
System.out.println("qry ::" +qry);
st.executeUpdate(qry);
String qry1 = "UPDATE LOBTABLE SET B_LOB = ? WHERE KVAL = 200 " ;
PreparedStatement pstmt = conn.prepareStatement(qry1);
pstmt.setBinaryStream(B_LOB,buf);
st.executeUpdate(qry1);
pstmt.close();
st.close();
conn.close();
System.out.println("Record Added");
%>
INSERTING IMAGE
</table>
</body>
</html>
Please let me know if you find the problem in code or help me by giving the source code for inserting and retreiving images from Database.
Thanks.
-
images in Database (2 messages)
- Posted by: Pravesh Bhargava
- Posted on: June 22 2001 05:04 EDT
Threaded Messages (2)
- images in Database by Raghuram Ethirajan on June 28 2001 17:40 EDT
- Anti-Pattern by Donald Diego on May 21 2004 11:56 EDT
-
images in Database[ Go to top ]
- Posted by: Raghuram Ethirajan
- Posted on: June 28 2001 17:40 EDT
- in response to Pravesh Bhargava
Hi, The following is my code for inserting a file into oracle(blob). I am using this code with weblogic!It gets the file name as input from the HTML form!Try it..and get back to me
<!--Author: Raghuram Ethirajan-->
<html>
<head>
<title>result:</title>
</head>
<body bgcolor=#FFFFFF>
<h1>
<font color=white>
result:
</font>
</h1>
<%@ page info="File uploader"%>
<%@ page import="java.io.*"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.sql.Blob"%>
<%@ page import="java.sql.PreparedStatement"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%
//initializing database connection
try
{
Class.forName("weblogic.jdbc.oci.Driver");
String url="jdbc:weblogic:oracle";
//get parameters from HTML
String des=request.getParameter("ff");
File ff=new File(request.getParameter("fn"));
String fname=ff.getName();
System.out.println("Name of the file is:" + ff.getName());
long fi=ff.length();
int fl;
fl=(int)fi;
System.out.println("size of the selected file is = " + ff.length()+"bytes");
//reading the binary file and writing into database
InputStream fin = new FileInputStream(ff);
Connection con=DriverManager.getConnection(url,"system","manager");
PreparedStatement pstmt = con.prepareStatement("insert into files (b,fname,fsize,fdes) values(?,?,?,?)");
pstmt.setBinaryStream (1,fin,fl);
pstmt.setString(2,fname);
pstmt.setInt(3,fl);
pstmt.setString(4,des);
pstmt.executeUpdate();
out.println("<center><font size=5> Data inserted into Database</font></center>");
con.close();
}
//catch Exception
catch (Exception e) {
//print error message
out.println("<center><font size=5> Error in inserting the data</font></center>");
out.println("the error is:"+e.toString());
System.out.println("Error is:"+e.toString());
}
//print the confirmation message here
%>
</body>
</html>
-
Anti-Pattern[ Go to top ]
- Posted by: Donald Diego
- Posted on: May 21 2004 11:56 EDT
- in response to Pravesh Bhargava
Good example of the antipattern of inserting data access into JSP.