12
返回列表 发新帖
楼主: xiao

如何操作LONG RAW类型的字段

[复制链接]
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
11#
发表于 2002-4-26 16:34 | 只看该作者
这个过程建立在8。05下。

使用道具 举报

回复
论坛徽章:
0
12#
 楼主| 发表于 2002-4-26 17:27 | 只看该作者

8i为什么不行

不知在ORACLE8i下是否可以

使用道具 举报

回复
论坛徽章:
1
ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44
13#
发表于 2002-4-27 14:27 | 只看该作者
oracle本身带着例子
ora81\oo4o\

使用道具 举报

回复
论坛徽章:
63
版主7段
日期:2012-05-15 15:24:11itpub13周年纪念徽章
日期:2014-10-08 15:16:50itpub13周年纪念徽章
日期:2014-10-08 15:16:50itpub13周年纪念徽章
日期:2014-10-08 15:16:50itpub13周年纪念徽章
日期:2014-10-10 14:34:59马上加薪
日期:2015-01-08 15:39:192015年新春福章
日期:2015-03-04 14:19:112015年新春福章
日期:2015-03-06 11:57:31
14#
发表于 2002-4-28 14:50 | 只看该作者
希望大家讨论一下 OO40 !

使用道具 举报

回复
论坛徽章:
27
ITPUB元老
日期:2005-02-28 12:57:002012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:18
15#
发表于 2002-4-30 16:13 | 只看该作者
来自metalink上的两份文档

第一篇:使用java

Doc ID:  Note:123009.1
Subject:  Example: How to Insert/Retrieve LONG RAW Data Using JDBC (SCR 525)
Type:  SCRIPT
Status:  REVIEWED
Content Type:  TEXT/PLAIN
Creation Date:  24-OCT-2000
Last Revision Date:  23-JAN-2001


*************************************************************
This article is being delivered in Draft form and may contain
errors.  Please use the MetaLink "Feedback" button to advise
Oracle of any issues related to this article.
*************************************************************


Overview
--------

This is a sample java application which uses the Oracle JDBC Thin Driver
to insert and retrieve long raw data from Oracle.

Program Notes
-------------

  o  This should work against Oracle 7.2 or higher on any platform.
  
  o  This should be compiled as a java application.

  o  This sample uses the following table definition for it's data
      CREATE TABLE JDBCLONG( REF NUMBER(5), LONGDATA LONG RAW );


References
----------

<JDBC Developer's Guide and Reference, Chapter 3, Streaming LONG or
LONG RAW Columns, A81354-01>



Caution
-------

The sample program in this article is provided for educational purposes only
and is NOT supported by Oracle Support Services.  It has been tested
internally, however, and works as documented.  We do not guarantee that it
will work for you, so be sure to test it in your environment before relying
on it.


Program
-------
[php]
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - - - - - -

//Title:      Long Raw JDBC Sample Application
//Version:    This is to be compiled as an APPLICATION
//Copyright:  Copyright (c) 1997
//Author:     Oracle Worldwide Support
//Company:    Oracle Corporation
//
// This sample uses the following table definition for it's data
//
// CREATE TABLE JDBCLONG( REF NUMBER(5), LONGDATA LONG RAW );
//
//
package untitled1;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Frame1 extends Frame {
  BorderLayout borderLayout1 = new BorderLayout();
  Panel bevelPanel1 = new Panel();
  BorderLayout borderLayout2 = new BorderLayout();
  Panel panel1 = new Panel();
  Panel panel2 = new Panel();
  TextField txtfldOutputName = new TextField();
  TextField txtfldInputName = new TextField();
  Button btnReadFile = new Button();
  Button btnWriteFile = new Button();
  BorderLayout borderLayout3 = new BorderLayout();
  BorderLayout borderLayout4 = new BorderLayout();
  Panel panel3 = new Panel();
  BorderLayout borderLayout5 = new BorderLayout();
  Button btnExit = new Button();
  Label lblMain = new Label();

  //Construct the frame
  public Frame1() {
    try {
      jbInit();
      try {
          DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ());
      } catch(Exception e) {
          System.err.println(e.getMessage());
      }

    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  public void jbInit() throws Exception{
    this.setLayout(borderLayout1);
    this.setBackground(Color.lightGray);
    this.setSize(new Dimension(446, 100));
    this.setTitle("JDBC Long Sample Application";
    panel2.setLayout(borderLayout4);
    panel1.setLayout(borderLayout3);
    btnReadFile.setLabel("File To Read";
    btnReadFile.addActionListener(new Frame1_btnReadFile_actionAdapter(this));
    btnWriteFile.setBounds(new Rectangle(356, 0, 84, 23));
    btnWriteFile.setLabel("File To Write";
    btnExit.setBounds(new Rectangle(351, 0, 84, 27));
    btnExit.setSize(new Dimension(87, 28));
    btnExit.setLabel("        Exit         ";
    btnExit.addActionListener(new Frame1_btnExit_actionAdapter(this));
    lblMain.setBackground(Color.lightGray);
    lblMain.setAlignment(1);
    lblMain.setText("Long Raw JDBC Sample";
    panel3.setLayout(borderLayout5);
    txtfldOutputName.addActionListener(new Frame1_txtfldOutputName_actionAdapter(this));
    btnWriteFile.addActionListener(new Frame1_btnWriteFile_actionAdapter(this));
    bevelPanel1.setLayout(borderLayout2);
    this.add(bevelPanel1, BorderLayout.CENTER);
    bevelPanel1.add(panel1, BorderLayout.SOUTH);
    panel1.add(txtfldOutputName, BorderLayout.CENTER);
    panel1.add(btnWriteFile, BorderLayout.EAST);
    bevelPanel1.add(panel2, BorderLayout.NORTH);
    panel2.add(txtfldInputName, BorderLayout.CENTER);
    panel2.add(btnReadFile, BorderLayout.EAST);
    bevelPanel1.add(panel3, BorderLayout.CENTER);
    panel3.add(btnExit, BorderLayout.EAST);
    panel3.add(lblMain, BorderLayout.CENTER);
  }

  void btnWriteFile_actionPerformed(ActionEvent e) {
    // btnWriteFile event Handler.
    //
    // ** This is where the code that writes a local file from
    // ** information pulled from the Database goes:
    //
    //   (This is where the important code is)
    //
    try {
        Connection conn = DriverManager.getConnection
            ("jdbcracle:thin:@msltstsv.us.oracle.com:1522:ORCL", "scott", "tiger";
        Statement stmt = conn.createStatement ();
        ResultSet rset = stmt.executeQuery
            ("SELECT LONGDATA FROM JDBCLONG WHERE REF = 1";

        if (rset.next ()) {
            try {
                java.io.FileOutputStream file = new java.io.FileOutputStream
                    (txtfldOutputName.getText());
                System.out.println("Writing File: " +txtfldOutputName.getText()+ "\n";
                java.io.InputStream fin = rset.getBinaryStream (1);
                int c;
                while ((c = fin.read ()) != -1)
                    file.write (c);
                file.close();
                lblMain.setText ("File Read";
            } catch (java.io.FileNotFoundException fe) {
                System.err.println ("thrown: java.io.FileNotFoundException\n";
                fe.printStackTrace ();
                System.err.println (fe.getMessage ());
            } catch (java.io.IOException ioe) {
                System.err.println ("thrown: java.io.IOException\n");
                ioe.printStackTrace ();
                System.err.println (ioe.getMessage ());
            }
        } // if (rset.next)
    } catch (java.sql.SQLException se) {
      System.err.println ("Caught SQL Exception\n\n");
      se.printStackTrace ();
      System.err.println (se.getMessage ());
    }

  }

  void btnReadFile_actionPerformed(ActionEvent e) {
    // btnReadFile event Handler.
    //
    // ** This is where the code that reads a local file into
    // ** the Database goes: (This is where the important code is)
    //
    try {
        Connection conn = DriverManager.getConnection
            ("jdbcracle:thin:@msltstsv.us.oracle.com:1522:ORCL", "scott", "tiger");

        java.io.File file = new java.io.File (txtfldInputName.getText());
        System.out.println ("Reading File: " +txtfldInputName.getText() + "\n");
        int fileLength = (int) file.length();
        try {
            java.io.InputStream fin = new java.io.FileInputStream(file);
            java.sql.PreparedStatement pstmt = conn.prepareStatement
               ("INSERT INTO JDBCLONG VALUES(1, ?)");
            pstmt.setBinaryStream (1, fin, fileLength);
            pstmt.executeUpdate();
            System.out.println("Update Done.\n");
            lblMain.setText ("File Written");
        } catch (java.io.FileNotFoundException fe) {
            System.err.println ("thrown: java.io.FileNotFoundException\n");
            fe.printStackTrace ();
            System.err.println (fe.getMessage ());
        }
    } catch (java.sql.SQLException se) {
      se.printStackTrace ();
      System.err.println (se.getMessage ());
    }

  }

  void txtfldOutputName_actionPerformed(ActionEvent e) {

  }

  void btnExit_actionPerformed(ActionEvent e) {
    System.exit(0);
  }
}

class Frame1_btnWriteFile_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_btnWriteFile_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.btnWriteFile_actionPerformed(e);
  }
}

class Frame1_btnReadFile_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_btnReadFile_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.btnReadFile_actionPerformed(e);
  }
}

class Frame1_txtfldOutputName_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_txtfldOutputName_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.txtfldOutputName_actionPerformed(e);
  }
}

class Frame1_btnExit_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_btnExit_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.btnExit_actionPerformed(e);
  }
}
[/php]
- - - - - - - - - - - - - - - -  Code ends here  - - - - - - - - - - - - - - - -

使用道具 举报

回复
论坛徽章:
27
ITPUB元老
日期:2005-02-28 12:57:002012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:18
16#
发表于 2002-4-30 16:14 | 只看该作者
第二篇, 使用c:



Doc ID:  Note:1007848.6
Subject:  HOW TO SELECT THE FULL 2147483647 BYTES IN A LONG OR LONG RAW COLUMN IN PLSQL?
Type:  PROBLEM
Status:  PUBLISHED
Content Type:  TEXT/PLAIN
Creation Date:  24-FEB-1995
Last Revision Date:  02-MAR-1995



Problem Description:  
====================  

How can I select and insert the full 2147483647 bytes in a long or long raw
column in PL/SQL?

Solution: PL/SQL IS LIMITED TO 32760 BYTES - USE THE ORACLE PRECOMPILERS INSTEAD


Solution Description:  
====================  

Although you can insert a long or long raw value into a long or long raw  
database column respectively, you cannot select a value longer than  
32760 bytes from a long or long raw column into a long or long raw variable.

You can manipulate the full 2147483647 bytes in a precompiler program
with regular embedded SQL by using datatype equivalencing to declare
a host variable of type long varchar, long varraw, or long raw.  

Solution Explanation:  
====================  

For users of Pro*C V2.0 or higher, see sample4.pc.  It is available in the  
manual and on-line.  For users of the Oracle Precompilers V1.x, the  
following is an example of how to do this in Pro*C.
[php]

/* manip.pc  

   This program inserts and retrieves binary executables into the database  
   using the long varraw structure.   
  
   Usage:  
     manip -c: creates the table  
     manip -i <key> <file>: inserts <file> into the table with key <key>  
     manip -r <key> <file>: writes the contents of the table at key <key> to  
                            <file>  
     manip -R <key> <file>: same as -r, but using dynamic method 4.  
     manip -I <key> <file>: same as -i, but using dynamic method 4.  
*/  
  
#define _HPUX_SOURCE  /* This is necessary for HP platforms */  
#define _POSIX_SOURCE /* This is necessary for AIX platforms */
#include <stdio.h>  
#include <sys/file.h>  
#include <string.h>  
#include <fcntl.h>
EXEC SQL include sqlca;  
EXEC SQL include sqlda;  
  
#define TOTALSIZE 5000000  
typedef struct {  
  long len;  
  char buf[TOTALSIZE];  
  } longvarraw;  
  
typedef struct {  
  long len;  
  char *buf;  
  } lvr_type;  
  
EXEC SQL begin declare section;  
  EXEC SQL type longvarraw is long varraw (5000000);  
EXEC SQL end declare section;  
  
void sqlerror();  
  
/* Reads file 'filename' into buf of size bufsize,  
   returns total length or -1 if error */  
#define LOCALSIZE 512  
int read_file(filename, buf, bufsize)  
char *filename, *buf;  
long bufsize;  {  

  char local_buffer[LOCALSIZE];  
  int n;  
  int total_size = 0;  
  int in_fd;  
  
  in_fd = open(filename, O_RDONLY, 0);  
  if (in_fd == -1)  
    return(-1);  
  
  while ((n = read(in_fd, local_buffer, LOCALSIZE)) > 0) {  
    if (total_size + n > bufsize) {  
      /* Out of space */  
      close(in_fd);  
      return(-1);  
      }  
    memcpy(buf+total_size, local_buffer, n);  
    total_size += n;  
    }  
  
  close(in_fd);  
  return(total_size);  
  }  

  
/* Writes contents of buf, size bufsize, to filename.  
   Returns -1 if error. */  
int write_file(filename, buf, bufsize)  
char *filename, *buf;  
long bufsize;  {  
  
  int out_fd;  
  int num_written;  
  printf("write_file: filename '%s'\n", filename);  
  printf("bufsize: %d\n", bufsize);  
  printf("buf: %d\n", buf);  
  
  out_fd = creat(filename, 0755);  
  if (out_fd == -1) {  
    printf("creat error.\n";  
    return(-1);  
    }  
  
  num_written = write(out_fd, buf, bufsize);  
  close(out_fd);  
  return(num_written);  
  }  

  
/* Connect to the database */  
void connect()  {  
  
EXEC SQL begin declare section;  
  char *uid = "scott/tiger";  
EXEC SQL end declare section;  
  
  EXEC SQL whenever sqlerror do sqlerror();  
  EXEC SQL connect :uid;  
  
  printf("Connected.\n";  
  }  
  
void create_table() {  
  
  connect();  
  
  /* We don't care about errors on this statement */  
  EXEC SQL whenever sqlerror continue;  
  
  EXEC SQL drop table executables;  

  /* Reset error handler */  
  EXEC SQL whenever sqlerror do sqlerror();  
  
  EXEC SQL create table executables  
    (name varchar2(20),  
     binary long raw);  
  
  printf("Table created.\n";  
  }  
  
  
void sqlerror() {  
  
  EXEC SQL whenever sqlerror continue;  
  
  printf("\nOracle Error!\n";  
  printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);  
  
  EXEC SQL rollback work release;  
  exit(1);  
  }  
  
  
/* Inserts the binary file identified by file into the executables table  
   identified by key. */  
int insert(key, file)  
EXEC SQL begin declare section;  
char *key, *file;  
EXEC SQL end declare section;  {  
  
EXEC SQL begin declare section;  
  longvarraw lvr;  
EXEC SQL end declare section;  
   
  lvr.len = read_file(file, lvr.buf, TOTALSIZE);  
  printf("length: %d\n", lvr.len);  
  if (lvr.len == -1)  
    exit(fprintf(stderr, "error while reading\n");  
  
  connect();  
  EXEC SQL whenever sqlerror do sqlerror();  
  EXEC SQL insert into executables values (:key, :lvr);  
  
  EXEC SQL commit;  
  printf("Inserted.\n";  
  }  
  
int insert4(key, file)  
EXEC SQL begin declare section;  
char *key, *file;  
EXEC SQL end declare section;  {  
  
EXEC SQL begin declare section;  
  char *sqlstatement = "insert into executables values (:key, :ll)";  
EXEC SQL end declare section;  
  
  SQLDA *bind_dp;  
  short key_ind = 0, executable_ind = 0;  
  char *ll;  
  long length;  
  char *buf_ptr;  
  
  ll = (char *)malloc(TOTALSIZE+4);  
  buf_ptr = ll + 4;  
  
  length = read_file(file, buf_ptr, TOTALSIZE);  
  printf("length: %d\n", length);  
  if (length == -1)  
    exit(fprintf(stderr, "error while reading\n");  
  
  memcpy(ll, &length, 4);  

  connect();  
  printf("Connected.\n";  
  
  bind_dp = (SQLDA *)sqlald(1, 20, 20);  
  
  bind_dp->N = 2;  
  
  EXEC SQL prepare S from :sqlstatement;  
  EXEC SQL describe bind variables for S into bind_dp;   
  
  bind_dp->L[0] = strlen(key);  
  bind_dp->T[0] = 1;  
  bind_dp->V[0] = key;  
  bind_dp->I[0] = &key_ind;  
  
  bind_dp->L[1] = TOTALSIZE + 4;  
  bind_dp->T[1] = 95;  
  bind_dp->V[1] = ll;  
  bind_dp->I[1] = &executable_ind;  
  
  printf("bound.\n";  
  
  
  EXEC SQL execute S using descriptor bind_dp;  
  printf("Executed.\n";  
   
  sqlclu(bind_dp);  
  
  EXEC SQL commit;  
  printf("Inserted.\n");  
  }  
  
  
/* Retrieves the executable identified by key into file, using  
   dynamic method 4 */  
int retrieve4(key, file)  
EXEC SQL begin declare section;  
char *key, *file;  
EXEC SQL end declare section;  {  
  
EXEC SQL begin declare section;  
  char *sqlstatement = "select binary from executables where name = :key";  
EXEC SQL end declare section;  

  SQLDA *select_dp;  
  SQLDA *bind_dp;  
  int num_written;  
  short key_ind = 0, executable_ind;  
  char *ll;  
  long length;  
  char *buf_ptr;  
  
  ll = (char *)malloc(TOTALSIZE+4);  
  
  connect();  
  
  printf("Selecting...\n");  
  
  select_dp = (SQLDA *)sqlald(1, 20, 20);  
  bind_dp = (SQLDA *)sqlald(1, 20, 20);  
  
  select_dp->N = 1;  
  bind_dp->N = 1;  
  
  EXEC SQL prepare S from :sqlstatement;  
  EXEC SQL declare C cursor for S;  
  EXEC SQL describe bind variables for S into bind_dp;   

  bind_dp->L[0] = strlen(key);  
  bind_dp->T[0] = 1;  
  bind_dp->V[0] = key;  
  bind_dp->I[0] = &key_ind;  
  
  printf("bound.\n");  
  EXEC SQL open C using descriptor bind_dp;  
  
  printf("opened\n");  
   
  EXEC SQL describe select list for S into select_dp;  
  select_dp->L[0] = TOTALSIZE + 4;  
  select_dp->V[0] = (char *)ll;  
  select_dp->T[0] = 95;  
  select_dp->I[0] = &executable_ind;  
  
  printf("described select list.\n");  
  EXEC SQL fetch C using descriptor select_dp;  
  printf("selected.\n");  
  memcpy(&length, ll, 4);  
  buf_ptr = ll + 4;  

  printf("length: %d\n", length);  
  num_written = write_file(file, buf_ptr, length);  
  printf("num_written: %d\n", num_written);  
  if (num_written != length)  
    exit(fprintf(stderr, "error while writing\n"));  
  
  EXEC SQL close C;  
  sqlclu(select_dp);  
  sqlclu(bind_dp);  
  }  
  
  
/* Retrieves the executable identified by key into file */  
int retrieve(key, file)  
EXEC SQL begin declare section;  
char *key, *file;  
EXEC SQL end declare section;  {  
  
EXEC SQL begin declare section;  
  longvarraw lvr;  
EXEC SQL end declare section;  
  
  int num_written;  
  connect();  

  printf("Selecting...\n");  
  EXEC SQL select binary  
    into :lvr  
    from executables  
    where name = :key;  
  printf("selected.\n");  
  
  printf("length: %d\n", lvr.len);  
  num_written = write_file(file, lvr.buf, lvr.len);  
  if (num_written != lvr.len)  
    exit(fprintf(stderr, "error while writing\n"));  
  
  }  
  
  
main(argc, argv)  
int argc;  
char **argv; {  
  char all[TOTALSIZE];  
  int num_written;  

  if (--argc == 1 && !strcmp(argv[1], "-c"))  
    create_table();  
  else if (argc == 3) {  
    if (!strcmp(argv[1], "-i"))   
      insert(argv[2], argv[3]);  
    else if (!strcmp(argv[1], "-I"))  
      insert4(argv[2], argv[3]);  
    else if (!strcmp(argv[1], "-r"))  
      retrieve(argv[2], argv[3]);  
    else if (!strcmp(argv[1], "-R"))  
      retrieve4(argv[2], argv[3]);  
    else  
      printf("Usage error.\n");  
    }  
  else  
    printf("Usage error.\n");  
  }
.

[/php]

--------------------------------------------------------------------------------

Copyright (c) 1995,2000 Oracle Corporation. All Rights Reserved. Legal Notices and Terms of Use.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表