ORA-01002: 读取违反顺序 怎样解决?
ruixp 2003-01-07 09:14:20 我想用如下程序调用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);
}
}