DBLink创建方法
DBLink创建方法
create database link dblinkName connect to touser identified by "password" using '在本地配置的链接'
使用
select * from tableName@dblinkName;
create table tableName as select * from tableName@dblinkName;可以将远程的数据导入到现有数据库。
Blob使用:
public static int insertBlob(String filepath){
int i = 0;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
//通过JDBC获得数据库连接
conn = DBConnection.getConnection();
//设置数据库为不自动提交,必须的一步
conn.setAutoCommit(false);
st = conn.createStatement();
//插入一个空对象empty_blob()
String insertSql = "insert into MOF_DOC_BLOB (ID, FILEPATH, CONTENT) values (1,'"+filepath+"', empty_blob())";
i = st.executeUpdate(insertSql);
String selectSql = "select CONTENT from MOF_DOC_BLOB where ID=1 for update";
rs = st.executeQuery(selectSql);
if (rs.next()) {
//得到流
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
//从得到的低级流构造一个高级流
PrintStream ps = new PrintStream(blob.getBinaryOutputStream());
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(filepath));
byte[] buff = new byte[1024];
int n = 0;
//从输入到输出
while ((n = bis.read(buff)) != -1) {
ps.write(buff, 0, n);
}
//清空流的缓存
ps.flush();
//关闭流,注意一定要关
ps.close();
bis.close();
} catch (FileNotFoundException e) {
System.out.println("Util.java================>insert BLOB file not fonud");
return 1;
} catch (IOException e) {
System.out.println("Util.java================>insert BLOB error");
return 1;
}
}
return i;
} catch (SQLException e) {
e.printStackTrace();
System.out.println("get conn error!");
return 1;
}finally {
try {
if(rs != null){
rs.close();
}
if(st !=null){
st.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}