I am trying to store a word document in Oracle database using JDBC.
I am using Oracle9i. But, I get the error as:
java.lang.ClassCastException
I tried creating a querystring, qstring like
String qString = "select resume from Resumes where emp_id = 9004 for update";
oracle.jdbc.driver.OracleResultSet ors = (OracleResultSet)stat.executeQuery(qString);
res.next();
BLOB blob = ors.getBLOB("resume");
But, it was of no help. I've included the code below. Please help. Thanks in advance.
Code:
import java.sql.*;
import java.sql.Driver;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
public class Datas1 extends JFrame implements ActionListener {
JTextField id;
JTextField name;
JButton next;
JButton addnew;
JPanel p;
static ResultSet res ;
static Connection conn;
static Statement stat;
public Datas1() {
super("Our Application");
Container c = getContentPane();
c.setLayout(new GridLayout(5,1));
id = new JTextField(20);
name = new JTextField(20);
next = new JButton("Next");
p = new JPanel();
c.add(new JLabel("Customer ID",JLabel.CENTER));
c.add(id);
c.add(new JLabel("Customer Name",JLabel.CENTER));
c.add(name);
c.add(p);
p.add(next);
next.addActionListener(this);
pack();
setVisible(true);
addWindowListener(new WIN());
}
public static void main(String args[]) {
Datas1 d = new Datas1();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:cust","SYSTEM","password"); // cust is the DSN Name
stat = conn.createStatement();
stat.execute("INSERT INTO Resumes(emp_id,name,resume)VALUES(9004,'Tom',empty_blob())");
System.out.println("LINE1");
conn.setAutoCommit(false);
res = stat.executeQuery( "select resume from Resumes where emp_id = 9004 for update");
System.out.println("LINE2");
res.next();
System.out.println("LINE3");
oracle.sql.BLOB blob = ((OracleResultSet)res).getBLOB("resume"); //ERROR is on this line
File resumeFile = new File("C:\\test.doc");
System.out.println("C:\\test.doc" + resumeFile.length());
FileInputStream fis = new FileInputStream(resumeFile);
int bufsize = blob.getChunkSize();
byte[] buffer = new byte[bufsize];
OutputStream os = blob.getBinaryOutputStream();
int nread = 0 ;
while ((nread = fis.read(buffer)) != -1){
os.write(buffer, 0, nread);
}
fis.close();
os.flush();
os.close();
conn.commit();
res = stat.executeQuery("Select emp_id, name from Resumes"); // Customers is the table name
res.next();
}
catch(SQLException ee){
ee.printStackTrace(System.out);
}
catch(Exception e) {
System.out.println("Error" +e);
e.printStackTrace();
}
d.showRecord(res);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == next) {
try {
res.next();
}
catch(Exception ee) {}
showRecord(res);
}
}
public void showRecord(ResultSet res) {
try {
id.setText(res.getString(1));
name.setText(res.getString(2));
}
catch(Exception e) {}
}//end of the main
//Inner class WIN implemented
class WIN extends WindowAdapter {
public void windowClosing(WindowEvent w) {
JOptionPane jop = new JOptionPane();
jop.showMessageDialog(null,"Database","Thanks",JOptionPane.QUESTION_MESSAGE);
}
}
} //end of the class