我想用如下程序调用oracle图片数据
分别为longraw和blob
加上
conn.setAutoCommit (false);
能够正确显示,但是实际上数据里面却没有二进制数据
不加,运行时出现错误
读取违反顺序
请大侠帮忙看看怎么回事? package myprojects.imageoracle;
import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ImageOracle extends Frame {
static String url ;
static String username;
static String password;
static Connection conn;
static Statement stmt;
static byte[] imageData;
static byte[] imageData1;
public ImageOracle() {
url = "jdbc:oracle:thin:@ruixp:1521:oracledb";
username = "system";
password = "manager";
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) throws Exception{
System.out.println("Starting ImageOracle...");
ImageOracle mainFrame = new ImageOracle();
mainFrame.setSize(400, 400);
mainFrame.setTitle("ImageOracle");
// Register the Oracle JDBC driver
try {
//加载驱动程序以连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection( url, username, assword );
conn.setAutoCommit (false);
BLOB blob = null;
stmt = conn.createStatement ();
// Drop the table if it exists
try {
stmt.execute ("drop table ImageTable");
System.out.println("ImageTable已经删除 ...\n");
}
catch (SQLException e) {
System.out.println("表不存在\n");
}
// 创建表
stmt.execute ("create table ImageTable (ID varchar2(10),IMAGE lONG
RAW, THUMBNAILIMAGE BLOB)");
System.out.println("表已经创建 ...\n");
// 插入一条空记录
stmt.execute("insert into ImageTable values ('1', NULL,empty_blob())");
stmt.execute("commit");
InsertImage();//插入正常图片记录
ReadImage();//读正常图片
//选择记录
String cmd = "select * from ImageTable for update";
ResultSet rset = stmt.executeQuery(cmd);
while (rset.next())
blob = ((OracleResultSet)rset).getBLOB(3);//绑定字段变量
InsertThumbnailImage(blob);//将缩略图文件存入数据库
ReadThumbnailImage(blob); //将缩略图读出,并存为相应的图片文件
}
catch (SQLException e) {
System.out.println("SQL Exception occured: " + e.getMessage());
e.printStackTrace();
}
catch(FileNotFoundException e) {
System.out.println("File Not Found");
}
catch (IOException ioe) {
System.out.println("IO Exception" + ioe.getMessage());
}
mainFrame.setLayout(new GridLayout(1,2));
PreviewPanel previewPanel = new PreviewPanel(imageData);
mainFrame.add(previewPanel);
PreviewPanel previewPanel1 = new PreviewPanel(imageData1);
mainFrame.add(previewPanel1);
mainFrame.setVisible(true);
}
public static void InsertImage()
{
try
{
// Insert data into the img_data column
File file = new File ("p1.jpg");
InputStream istr = new FileInputStream (file);
try
{
// prepare the INSERT into webimages using parameters
PreparedStatement pstmt = conn.prepareStatement ("update
ImageTable s
et IMAGE=? where id= 1");
// use a prepared INSERT for webimages
pstmt.setBinaryStream (1, istr, (int)file.length ());
pstmt.executeUpdate();
pstmt.close();
istr.close();
System.out.println("已经成功加入图片数据 \n" );
}
catch(SQLException e)
{
System.out.println("SQL Exception occured: " +
e.getMessage());
e.printStackTrace();
}
}
catch(IOException ioe)
{
System.out.println("IO Exception" + ioe.getMessage());
}
}
public static void ReadImage()
{
try
{
stmt = conn.createStatement ();
// retrieve image from row when the ID is '1'
ResultSet rs = stmt.executeQuery ("SELECT IMAGE FROM ImageTable WHERE ID='1'");
// process the ResultSet data
if (rs.next ())
{
try
{
imageData= rs.getBytes(1);
System.out.print("成功读出正常图片!\n");
}
catch(Exception ioe)
{
System.out.println("IO Exception" +
ioe.getMessage());
}
}
}
catch(SQLException e)
{
System.out.println("SQL Exception occured: " + e.getMessage());
e.printStackTrace();
}
}
static void InsertThumbnailImage (BLOB blob) throws Exception
{
File binaryFile = new File("p2.jpg"); //需要插入到数据库中缩略图的图片文件
FileInputStream in = new FileInputStream(binaryFile);
OutputStream out = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) != -1)
out.write(buffer, 0, length);
in.close();
out.close();
System.out.print("成功插入缩略图!\n");
}
static void ReadThumbnailImage (BLOB blob) throws Exception
{
//获取oracle块的大小
imageData1 = blob.getBytes(1, (int)blob.length());
System.out.print("成功读出缩略图!\n");
}
}
class PreviewPanel extends JPanel
{
public PreviewPanel(byte[] imageData)
{
JLabel label = new JLabel("Image Previewer",SwingConstants.CENTER);
setPreferredSize(new Dimension(150,0)); setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
label.setBorder(BorderFactory.createEtchedBorder());
add(label,BorderLayout.NORTH);
ImageIcon icon = new ImageIcon(imageData) ;
JLabel previewer = new JLabel(icon) ;
add(previewer,BorderLayout.CENTER);
}
}
分别为longraw和blob
加上
conn.setAutoCommit (false);
能够正确显示,但是实际上数据里面却没有二进制数据
不加,运行时出现错误
读取违反顺序
请大侠帮忙看看怎么回事? package myprojects.imageoracle;
import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ImageOracle extends Frame {
static String url ;
static String username;
static String password;
static Connection conn;
static Statement stmt;
static byte[] imageData;
static byte[] imageData1;
public ImageOracle() {
url = "jdbc:oracle:thin:@ruixp:1521:oracledb";
username = "system";
password = "manager";
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) throws Exception{
System.out.println("Starting ImageOracle...");
ImageOracle mainFrame = new ImageOracle();
mainFrame.setSize(400, 400);
mainFrame.setTitle("ImageOracle");
// Register the Oracle JDBC driver
try {
//加载驱动程序以连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection( url, username, assword );
conn.setAutoCommit (false);
BLOB blob = null;
stmt = conn.createStatement ();
// Drop the table if it exists
try {
stmt.execute ("drop table ImageTable");
System.out.println("ImageTable已经删除 ...\n");
}
catch (SQLException e) {
System.out.println("表不存在\n");
}
// 创建表
stmt.execute ("create table ImageTable (ID varchar2(10),IMAGE lONG
RAW, THUMBNAILIMAGE BLOB)");
System.out.println("表已经创建 ...\n");
// 插入一条空记录
stmt.execute("insert into ImageTable values ('1', NULL,empty_blob())");
stmt.execute("commit");
InsertImage();//插入正常图片记录
ReadImage();//读正常图片
//选择记录
String cmd = "select * from ImageTable for update";
ResultSet rset = stmt.executeQuery(cmd);
while (rset.next())
blob = ((OracleResultSet)rset).getBLOB(3);//绑定字段变量
InsertThumbnailImage(blob);//将缩略图文件存入数据库
ReadThumbnailImage(blob); //将缩略图读出,并存为相应的图片文件
}
catch (SQLException e) {
System.out.println("SQL Exception occured: " + e.getMessage());
e.printStackTrace();
}
catch(FileNotFoundException e) {
System.out.println("File Not Found");
}
catch (IOException ioe) {
System.out.println("IO Exception" + ioe.getMessage());
}
mainFrame.setLayout(new GridLayout(1,2));
PreviewPanel previewPanel = new PreviewPanel(imageData);
mainFrame.add(previewPanel);
PreviewPanel previewPanel1 = new PreviewPanel(imageData1);
mainFrame.add(previewPanel1);
mainFrame.setVisible(true);
}
public static void InsertImage()
{
try
{
// Insert data into the img_data column
File file = new File ("p1.jpg");
InputStream istr = new FileInputStream (file);
try
{
// prepare the INSERT into webimages using parameters
PreparedStatement pstmt = conn.prepareStatement ("update
ImageTable s
et IMAGE=? where id= 1");
// use a prepared INSERT for webimages
pstmt.setBinaryStream (1, istr, (int)file.length ());
pstmt.executeUpdate();
pstmt.close();
istr.close();
System.out.println("已经成功加入图片数据 \n" );
}
catch(SQLException e)
{
System.out.println("SQL Exception occured: " +
e.getMessage());
e.printStackTrace();
}
}
catch(IOException ioe)
{
System.out.println("IO Exception" + ioe.getMessage());
}
}
public static void ReadImage()
{
try
{
stmt = conn.createStatement ();
// retrieve image from row when the ID is '1'
ResultSet rs = stmt.executeQuery ("SELECT IMAGE FROM ImageTable WHERE ID='1'");
// process the ResultSet data
if (rs.next ())
{
try
{
imageData= rs.getBytes(1);
System.out.print("成功读出正常图片!\n");
}
catch(Exception ioe)
{
System.out.println("IO Exception" +
ioe.getMessage());
}
}
}
catch(SQLException e)
{
System.out.println("SQL Exception occured: " + e.getMessage());
e.printStackTrace();
}
}
static void InsertThumbnailImage (BLOB blob) throws Exception
{
File binaryFile = new File("p2.jpg"); //需要插入到数据库中缩略图的图片文件
FileInputStream in = new FileInputStream(binaryFile);
OutputStream out = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) != -1)
out.write(buffer, 0, length);
in.close();
out.close();
System.out.print("成功插入缩略图!\n");
}
static void ReadThumbnailImage (BLOB blob) throws Exception
{
//获取oracle块的大小
imageData1 = blob.getBytes(1, (int)blob.length());
System.out.print("成功读出缩略图!\n");
}
}
class PreviewPanel extends JPanel
{
public PreviewPanel(byte[] imageData)
{
JLabel label = new JLabel("Image Previewer",SwingConstants.CENTER);
setPreferredSize(new Dimension(150,0)); setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
label.setBorder(BorderFactory.createEtchedBorder());
add(label,BorderLayout.NORTH);
ImageIcon icon = new ImageIcon(imageData) ;
JLabel previewer = new JLabel(icon) ;
add(previewer,BorderLayout.CENTER);
}
}
which is no longer valid. Note that a PL/SQL cursor loop
implicitly does fetches, and thus may also cause this error.
There are a number of possible causes for this error, including:
1) Fetching from a cursor after the last row has been retrieved
and the ORA-1403 error returned.
2) If the cursor has been opened with the FOR UPDATE clause,
fetching after a COMMIT has been issued will return the error.
3) Rebinding any placeholders in the SQL statement, then issuing
a fetch before reexecuting the statement.
*Action: 1) Do not issue a fetch statement after the last row has been
retrieved - there are no more rows to fetch.
2) Do not issue a COMMIT inside a fetch loop for a cursor
that has been opened FOR UPDATE.
3) Reexecute the statement after rebinding, then attempt to
fetch again.
which is no longer valid
(string) entries)
Cause: The amount statically allocated is not enough based on the value of the
MAX_ROLLBACK_SEGMENTS parameter.
Action: For now, take another rollback segment offline or increase the value of the parameter
MAX_ROLLBACK_SEGMENTS.
and-execute call must be issued before a fetch. This can occur if an attempt was made to FETCH
from an active set after all records have been fetched. This may be caused by fetching from a
SELECT FOR UPDATE cursor after a commit. A PL/SQL cursor loop implicitly does fetches and
may also cause this error.
Action: Parse and execute a SQL statement before attempting to fetch the data.
{
try
{
// Insert data into the img_data column
File file = new File ( "p1.jpg ");
InputStream istr = new FileInputStream (file);
try
{
// prepare the INSERT into webimages using parameters
con.setAutoCommit(false);]//这里加上这句话
[/color PreparedStatement pstmt = conn.prepareStatement ( "update
ImageTable s
et IMAGE=? where id= 1 ");
// use a prepared INSERT for webimages
pstmt.setBinaryStream (1, istr, (int)file.length ());
pstmt.executeUpdate();
pstmt.close();
istr.close();
[color=#FF0000]con.commit();//最后提交事务
System.out.println( "已经成功加入图片数据 \n " );
}
catch(SQLException e)
{
System.out.println( "SQL Exception occured: " +
e.getMessage());
e.printStackTrace();
}
}
catch(IOException ioe)
{
System.out.println( "IO Exception " + ioe.getMessage());
}
}
我之前也一样后来加了事物就好了 不知道和你的一样不 试试咯