关闭

ORACLE中表,视图名变大写和BLOB操作的问题

2480人阅读 评论(0) 收藏 举报
1. 表,视图创建后名字全部变成大写
     通过JDBC中的DatabaseMetaData.getTables函数可以来查询当前数据库的MetaData,既可以查询一些数据表和视图等很多信息。但是,ORACLE在当你创建完成,名字全部变成了大写,如果在getTables中,参数名字输入的是小写,那么将查询不到该表或者该视图。
   其实,getTables函数也是通过SQL语句来查询一个叫做all_objects的表。
 
SELECT NULL AS table_cat,
       o.owner AS table_schem,
       o.object_name AS table_name,
       o.object_type AS table_type,
       NULL AS remarks
  FROM all_objects o
  WHERE o.owner LIKE ? ESCAPE '/'
    AND o.object_name LIKE ? ESCAPE '/'
    AND o.object_type IN ('xxx', 'VIEW')
  ORDER BY table_type, table_schem, table_name
 
2。关于BLOB数据类型的存取:
  ORACLE的新版本,其中BLOB的存取一直是个比较麻烦的事情,似乎它不兼容JDBC的标准。我在MYSQL下创建,查询的BLOB字段的SQL,在ORACLE上要出现一些问题:比如getBytes(),setBytes()要出现问题等等。
  网上讨论这个的还是比较多的,最简单的办法就是把BLOB改成LONG RAW,虽然LONG RAW最大存储容量是2GB,而BLOB是4GB,但是其实大多数情况都够了。
  如果非要使用BLOB,我这里从网上找到了一些贴子和代码:
package demo;
import java.sql.*;
import java.io.*;
import java.sql.PreparedStatement;
import java.util.*;

import oracle.jdbc.driver.*;
import oracle.sql.BLOB;


/**
* Insert record in the MEDIA table
* MEDIA (file_name varchar2(256), file_content BLOB);
*/

public class BlobOracle
{
private final static String hostname =
"localhost";
private final static String port =
"1521";
private final static String sid =
"ORCL";
private final static String username =
"scott";
private final static String password =
"tiger";
private static String fileLocation;
private static Connection connection;

public BlobOracle()
{
}

/**
*
* @param args
*/

public static void main(String[] args)
{
try
{
if (args.length == 0)
{
System.out.println(
"/n/n Usage demo.BlobOracle ");
System.exit(0);
}

fileLocation = args[0];

setConnection();
insertBLOB();

} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
}
}


private static void setConnection() throws SQLException
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@"+hostname+ ":"+ port +":"+ sid , username , password);
connection.setAutoCommit(false);
// we must control the commit
}

private static void insertBLOB() throws SQLException, Exception
{
BLOB blob;
File file ;
FileInputStream is;
OutputStream os;

long ts1 = System.currentTimeMillis();


//Create a statement.
PreparedStatement pstmt = connection.prepareStatement(
"insert into media (file_name, file_content) values (?,empty_blob())");
file = new File(fileLocation);
String fileName = file.getName();
//Set the file name and execute the query
pstmt.setString(1, fileName);
pstmt.execute();

//Take back the record for update (we will insert the blob)
//supposely the file name is the PK
pstmt = connection.prepareStatement(
"select file_content from media where file_name = ? for update");
pstmt.setString(1, fileName);

//Execute the query, and we must have one record so take it
ResultSet rset = pstmt.executeQuery();
rset.next();

//Use the OracleDriver resultset, we take the blob locator
blob = ((OracleResultSet)rset).getBLOB(
"file_content");

is = new FileInputStream(file);
//Create a stream from the file
// JDBC 2.0
//os = blob.getBinaryOutputStream(); //get the output stream from the Blob to insert it
// JDBC 3.0
os = blob.setBinaryStream(0);
//get the output stream from the Blob to insert it

//Read the file by chuncks and insert them in the Blob. The chunk size come from the blob
byte[] chunk = new byte[blob.getChunkSize()];
int i=-1;
System.out.println(
"Inserting the Blob");
while((i = is.read(chunk))!=-1)
{
os.write(chunk,0,i);
//Write the chunk
System.out.print('.');
// print progression
}

// When done close the streams
is.close();
os.close();

//Close the statement and commit
pstmt.close();
long ts2 = System.currentTimeMillis();

connection.commit();
connection.close();

System.out.println(
"/n"+ (ts2 - ts1) +" ms" );


}


}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:925766次
    • 积分:12200
    • 等级:
    • 排名:第1222名
    • 原创:204篇
    • 转载:37篇
    • 译文:0篇
    • 评论:838条
    最新评论