奇奇的空间

奇奇的空间
posts - 5, comments - 13, trackbacks - 0, articles - 23

JDBC如何操作long/long raw/blob/clob数据类型

Posted on 2006-05-08 16:54 奇奇 阅读(915) 评论(0)  编辑 收藏 引用 所属分类: JAVA

  如何写long和long raw类型

  代码:
     case Types.LONGVARCHAR:
                java.io.StringReader long_var =
                    new java.io.StringReader(oldval.toString());
                stmt.setCharacterStream(pos,long_var,32768);
                return;
            case Types.LONGVARBINARY:
                java.io.File os_file = new java.io.File(oldval.toString());
                if (os_file.exists() && os_file.isFile() && os_file.canRead())
                {
                    try {
                        java.io.FileInputStream long_bin =
                            new java.io.FileInputStream(os_file);
                        stmt.setBinaryStream(pos,long_bin,(int)(os_file.length()));
                    } catch (java.io.IOException ioe) {}
                }
                else
                    stmt.setNull(pos,1);
                return;
                  __________________
  

                  如何操作BLOB/CLOB类型
                  ============================
                  操作blob/clob时,先用select lobfld from tab where ... for
                  update,取得java.sql.Blob或javq.sql.Clob类型


  代码:
  stmt = prepareStatement(Database,p_query,variable_table);
            stmt.bind(variable_table);
            rset = stmt.stmt.executeQuery();
            java.sql.ResultSetMetaData rsetmeta = rset.getMetaData();
            if (rset.next())
            {
                if (rsetmeta.getColumnType(1) == java.sql.Types.CLOB)
                {
                    java.sql.Clob clob = rset.getClob(1);
                    long offs = 0;
                    if (clob != null)
                    {
                        char[] long_buf=new char[8192];
                        try {
                            int len=0;
                            clob.truncate(offs);   
                            java.io.Writer long_out = clob.setCharacterStream(0);
                            java.io.FileReader long_file = new java.io.FileReader(os_file);
                            while((len = long_file.read(long_buf))>0)
                            {
                                long_out.write(long_buf,0,len);
                                offs = offs + len;
                            }
                            long_file.close();
                            long_out.close();
                        }
                         catch ( java.io.IOException ioe)
                        {
                            log.println(ioe.getMessage());
                        }
                    }
                }
                else if (rsetmeta.getColumnType(1) == java.sql.Types.BLOB)
                {
                    java.sql.Blob blob = rset.getBlob(1);
                    if (blob != null)
                    {
                        byte[] long_buf=new byte[8192];
                        long offs = 0;
                        try {
                            int len=0;
                            blob.truncate(offs);
                            java.io.OutputStream long_out = blob.setBinaryStream(0);
                            java.io.FileInputStream long_file = new java.io.FileInputStream(os_file);
                            while((len = long_file.read(long_buf))>0)
                            {
                                long_out.write(long_buf,0,len);
                                offs = offs + len;
                            }
                            long_file.close();
                            long_out.close();
                        }
                         catch ( java.io.IOException ioe)
                        {
                            log.println(ioe.getMessage());
                        }
                    }
                }
                data = getMessage(jdbc_cfg,"JDBC.COMMAND.COMPLETED");
                log.println(data.getColumn(1));
            }
            else
            {
                log.println("0 "+jdbc_cfg.getNLSString("JDBC.COMMAND.ROWSSELECTED"));
            }
        }
                  __________________
      
                  如何读取long/longraw/blob/clob中的数据?

  代码:
     stmt = prepareStatement(Database,p_query,variable_table);
            stmt.bind(variable_table);
            rset = stmt.stmt.executeQuery();
            java.sql.ResultSetMetaData rsetmeta = rset.getMetaData();
            if (rset.next())
            {
                if (rsetmeta.getColumnType(1) == java.sql.Types.LONGVARCHAR)
                {
                    java.io.Reader long_out = rset.getCharacterStream(1);
                    if (long_out != null)
                    {
                        char[] long_buf=new char[8192];
                        try {
                            int len=0;
                            java.io.FileWriter long_file = new java.io.FileWriter(os_file);
                            while((len = long_out.read(long_buf))>0)
                                long_file.write(long_buf,0,len);
                            long_file.close();
                            long_out.close();
                        }
                         catch ( java.io.IOException ioe)
                        {
                            log.println(ioe.getMessage());
                        }
                    }
                }
                else if (rsetmeta.getColumnType(1) == java.sql.Types.LONGVARBINARY)
                {
                    java.io.InputStream long_out = rset.getBinaryStream(1);
                    if (long_out != null)
                    {
                        byte[] long_buf=new byte[8192];
                        try {
                            int len=0;
                            java.io.FileOutputStream long_file = new java.io.FileOutputStream(os_file);
                            while((len = long_out.read(long_buf))>0)
                                long_file.write(long_buf,0,len);
                            long_file.close();
                            long_out.close();
                        }
                         catch ( java.io.IOException ioe)
                        {
                            log.println(ioe.getMessage());
                        }
                    }
                }
                else if (rsetmeta.getColumnType(1) == java.sql.Types.CLOB)
                {
                    java.sql.Clob clob = rset.getClob(1);
                    if (clob != null)
                    {
                        java.io.Reader long_out = clob.getCharacterStream();
                        if (long_out != null)
                        {
                            char[] long_buf=new char[8192];
                            try {
                                int len=0;
                                java.io.FileWriter long_file = new java.io.FileWriter(os_file);
                                while((len = long_out.read(long_buf))>0)
                                    long_file.write(long_buf,0,len);
                                long_file.close();
                                long_out.close();
                            }
                             catch ( java.io.IOException ioe)
                            {
                                log.println(ioe.getMessage());
                            }
                        }
                    }
                }
                else if (rsetmeta.getColumnType(1) == java.sql.Types.BLOB)
                {
                    java.sql.Blob blob = rset.getBlob(1);
                    if (blob != null)
                    {
                        java.io.InputStream long_out = blob.getBinaryStream();
                        if (long_out != null)
                        {
                            byte[] long_buf=new byte[8192];
                            try {
                                int len=0;
                                java.io.FileOutputStream long_file = new java.io.FileOutputStream(os_file);
                                while((len = long_out.read(long_buf))>0)
                                    long_file.write(long_buf,0,len);
                                long_file.close();
                                long_out.close();
                            }
                             catch ( java.io.IOException ioe)
                            {
                                log.println(ioe.getMessage());
                            }
                        }
                    }
                }
                data = getMessage(jdbc_cfg,"JDBC.COMMAND.COMPLETED");
                log.println(data.getColumn(1));
            }
            else
            {
                log.println("0 "+jdbc_cfg.getNLSString("JDBC.COMMAND.ROWSSELECTED"));
            }
                  __________________
 


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理