如何用jsp存取oracle数据库Blob字段中的word或excel(200分)

  • 主题发起人 主题发起人 wxkabc
  • 开始时间 开始时间
W

wxkabc

Unregistered / Unconfirmed
GUEST, unregistred user!
[:D]各位大侠好!请教大家一个问题:
现在我要用JSP来实现向ORACLE数据库中的BLOB字段存WORD或EXCEL,还要能用JSP对该字段的WORD或EXCEL进行修改!我不知道怎么做,多谢大家!
 
import java.io.*;
import java.util.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import java.text.*;
public class test
{
public static void main(String args[]) throws java.io.IOException,java.sql.SQLException
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: " + e.getMessage());
}
try
{
File files = new File("c://test.xls");
FileInputStream fis=new FileInputStream(files);
System.out.println(files.getName());
System.out.println((int)files.length());
byte[] fl = new byte[(int)files.length()];
System.out.println(fl.length);
fis.read(fl);
ByteArrayInputStream kk = new ByteArrayInputStream(fl) ;
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@eaiserver01:1521:orcl", "system", "admin");

PreparedStatement ps = conn.prepareStatement("INSERT INTO bin_data (name,data)" +"VALUES (?, ?)");
ps.setString(1,files.getName());
ps.setBinaryStream(2, kk,(int)files.length());
ps.executeUpdate();
fis.close();
ps.close();
int bytesRead = 0;
int byteSum = 0;
byte[] buffer = new byte[8 * 1924];
FileOutputStream fis2 = new FileOutputStream("c://hi.xls");

PreparedStatement ps2 = conn.prepareStatement( "select data from bin_data");
ResultSet rs = ps2.executeQuery();
if (rs != null)
{
while(rs.next())
{
InputStream is = rs.getBinaryStream(1);
while ((bytesRead = is.read(buffer)) != -1)
{
byteSum += bytesRead;
fis2.write(buffer, 0, bytesRead);
}
fis2.close( );
}
rs.close( );
}
ps2.close( );
}
catch(Exception e)
{
System.out.println("errror :"+e.toString() );
e.printStackTrace();

}
}
}
 
正如楼上所讲,基本要点是:
1。采用流处理
2。PreparedStatement ps = conn.prepareStatement("INSERT INTO bin_data (name,data)" +"VALUES (?, ?)");
 
后退
顶部