aspose file tools*
The moose likes JDBC and the fly likes Urgent.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Urgent...." Watch "Urgent...." New topic
Author

Urgent....

Jyothsna Sri Y
Ranch Hand

Joined: Sep 25, 2003
Posts: 35
Hi Everybody...
Please ignore my previous query... help me out in this...
My Code:
///////////////////////////////////////////////////
// PicLoader
//
// Read and write pictures to Oracle as BLOBs.
//
//
///////////////////////////////////////////////////

import java.io.*;
import java.util.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;

public class PicLoader {
static String sqlOut=
" begin"+
" insert into pic_store(description,picture)"+
" values(?,empty_blob())"+
" return picture into ?;"+
" end;";
static String sqlIn=
"select picture from pic_store where description=?";
static String sqlOut1=
" insert into pic_store(description,picture)"+
" values(?,empty_blob())";

String cs="jdbc dbc:aprta";
String user="rta";
String passwd="oracle";
Connection con;

void connect() {
try
{
System.out.println("Registering driver");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
System.out.println("Exception in forName try block " + e);
}
try {


System.out.println("getting connection");
con=DriverManager.getConnection(cs,user,passwd);
System.out.println("connection success");
}/* catch(Exception e) {
System.err.println("********** Could not connect to Oracle.");
System.exit(1);
} */
catch(SQLException sqle)
{System.out.println("error is "+sqle);}
}

void disconnect() {
try {
if (con != null) con.close();
}catch(Exception e){e.printStackTrace();}
}

public void write(String desc) throws Exception
{
System.out.println("going to try connecting");
if (con == null) connect();
System.out.println("going to commit false");
con.setAutoCommit(false);

System.out.println("going to prepare call"+sqlOut);
CallableStatement stmt=con.prepareCall(sqlOut);
System.out.println("going to set string "+stmt);
stmt.setString(1,desc);
System.out.println("going to register parameters");
stmt.registerOutParameter(2,java.sql.Types.BLOB);
System.out.println("going to execute "+stmt);
stmt.executeUpdate();
// get stream to Oracle
System.out.println("going to getblob");
BLOB blob=(BLOB) stmt.getBlob(2);
System.out.println("orastrm");
OutputStream orastrm=blob.getBinaryOutputStream();
// load picture from local filesystem
System.out.println("filebuffer");
BufferedInputStream filebuf=new BufferedInputStream(new FileInputStream(desc+".jpg"));
// send data to Oracle
System.out.println("writing to database");
int i;
while((i=filebuf.read())!=-1)
orastrm.write(i);
System.out.println("written");
con.commit();
// cleanup
filebuf.close();
orastrm.close();
stmt.close();
disconnect();
}
public byte[] read(String desc) throws Exception
{
if (con == null) connect();
PreparedStatement stmt=con.prepareStatement(sqlIn);
stmt.setString(1,desc);
ResultSet rs=stmt.executeQuery();
rs.next();
Blob picBlob=rs.getBlob(1);
byte[] picBuf = picBlob.getBytes(1,(int) picBlob.length());
stmt.close();
disconnect();
return picBuf;
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java PicDisp <description>");
System.exit(1);
}
System.out.println("going to try writing");
try {
(new PicLoader()).write(args[0]);
} catch(Exception e){e.printStackTrace();}
}
}
////////////////////////////////////////////////////////////////////////
at the command prompt I am running the program like this,
cmd prompt>java PicLoader sound1
where sound1 is an image file(jpg format) and I am getting the following Exception... can someone solve this for me..
regards
Jyothsna
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Why are you using the ODBC bridge when you have the Oracle thin drivers set up in your class path? I'm sure that this is or will cause errors.
Replace your Connection code with:

after you get this sorted out, you can then start to debug the errors. I have a feeling that java knows what an Oracle BLOB is, but the ODBC driver your using will probably say "what the heck is this??!".
By the way, you really should look at the Oracle9i JDBC Developer's Guide and Reference which will explain and give examples for programmers from complete newbie to expert.
Jamie
[ November 25, 2003: Message edited by: Jamie Robertson ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Jyothsna,
Please use meaningful subjects for your posts. You will get more and better responses when people looking through the forum see something that describes the nature of your question. Also, avoid using "Urgent"... it will make many people simply ignore your post which is no more and no less important than anyone else's.
thanks,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent....