『壹』 java中如何將大於10000的字元串,插入oracle資料庫中的blob類型欄位中
不要用blob,用clob類型。先插入一個empty_clob(),然後用Statement myStmt = conn.createStatement();
ResultSet clobResultSet=myStmt.executeQuery("SELECT "+strCLOBColName
+" FROM "+strTable
+" WHERE "+ strIdColName +" ='" + strIdValue+ "' FOR UPDATE ");
CLOB myClob = (oracle.sql.CLOB)clobResultSet.getClob(strCLOBColName);
myClob.putString(1,strBuff);
myStmt.execute("COMMIT");
clobResultSet.close();
myStmt.close();
conn.setAutoCommit(true);
『貳』 oracle 中存儲大量文字信息用什麼欄位,java後台怎麼實現,請舉例。謝謝。
在oracle中存儲大量的文字應該是用CLOB欄位來存儲。這種類型在用java將數據存進資料庫中時比較簡單,直接將String類型的文本寫入就可以了。但是在從資料庫中讀取CLOB類型的數據到java中時比較麻煩,因為你是拿不到數據的,只能拿到這個欄位的一個隱式指針,然後還要通過輸入流來拿到。
『叄』 如何用Java和oracle實現BLOB欄位的字元串讀取
Java能夠調用Oracle的存儲過程,反之Oracle也能用Java來開發外部存儲過程,這樣Java和oracle的相互界限就已經不明確了。Oracle實現BLOB欄位的字元串讀取也就非常容易了。
當然關系型資料庫最好做自己應該做的事情而不是大包大攬做所有的非資料庫應該做的事情。
--開發Java類
以上代碼均在PL/SQL developer中開發並調試通過,Java和Oracle實現BLOB欄位的字元串讀取很有意思。
『肆』 Oracle怎樣處理大文本欄位
Oracle
CHAR 定長字元串,最長2000位元組
NCHAR 定長字元串,存儲的數據為 NLS字元
VARCHAR2 變長字元串,最長4000位元組
NVARCHAR2 變長字元串,存儲的數據為 NLS字元
LONG 最大存儲2G字元數據 不推薦使用(改用CLOB)
CLOB Oracle 9i 及以前,最大4G字元數據
Oracle10g 最大4G*資料庫塊大小的字元數據
NCLOB 基本同CLOB,就是存儲的數據為NLS
『伍』 關於JAVA~~~~ 如何將圖片等大對象存入ORACLE中~~求詳解!(代碼)
插入圖片/文本(blob /clob)到oracle資料庫(引用http://www.java-asp.net/java/200512/t_48888.html)
我們在寫OA的時候經常遇到的問題就是員工圖片檔案的儲存問題,解決這個問題有兩個方法,
1.jsP/html頁面裡面讀取web伺服器上的圖片,也就是把圖片放到(上傳)到web 伺服器上,然後用html 語句讀取:
<img src=" 絕對或相對路徑 " border="0" />
2.就是上傳到資料庫裡面(oracle).關於oracle 資料庫,它支持blob, 和clob, 分別對應著圖片和文本(長字元串)操作
由於性能原因,我們還是要採用第二種方法,而且存到資料庫裡面比較容易管理,是吧?
首先,我們要解決上傳問題,這里採用普遍使用的apache commons 組件裡面的FileUpload class.
具體步驟如:
DiskFileUpload dfu=new DiskFileUpload();
dfu.setSizeMax(100000000);
dfu.setSizeThreshold(100000);
dfu.setRepositoryPath("f:\\public");
try{
List fileItems=dfu.parseRequest(request);
Iterator i=fileItems.iterator();
while(i.hasNext()){
FileItem fi=(FileItem)i.next();
if(!fi.isFormField()){
name=fi.getName();
size=fi.getSize();
if((name==null||name.equals(""))&&size==0)
continue;
}
name=fi.getName();
size=fi.getSize();
(InputStream)is=fi.getInputStream();
}
上面的代碼是web伺服器接受上傳的代碼,參考文件已經在我上篇寫的上傳文本文件里給出,今天,終於想明白了:
dfu.setRepositoryPath("f:\\public"); 的意思
原來是轉義字元也就是說\n\t等而要列印反斜杠要用\\,其實這個問題原先已經知道,可是由於經驗沒有寫過圖片上傳處理什麼的,覺得很高深,也很可怕,哈哈,心裡有點畏懼.看來基礎的東西,那怕一點點小細節也很重要,接著還有下面的java IO 問題.剛才讀core java 的時候突然發現在講io的時候特意提醒了這個問題,可是我沒有注意!
通過上面的代碼已經實現文件上傳了.然後,我們要實現JDBC數據源鏈接,目的是要把數據插入到oracle.
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");
conn=ds.getConnection();
conn.setAutoCommit(false);
關於要import java.sql.* javax.sql.* java.naming.* 不再詳細敘述了
接著根據很有用的一篇文章的提示,插入blob類型一定要先1.插入一個空的
String insert=" insert into uploadpicture "+
" values(?, empty_blob()) " ;
2.然後找到這個blob的oracle 裡面的游標:
String findCursor=" select content "+
" from uploadpicture "+
" where name=? for update ";
注意這個for update(注意!!!必須加for update,這將鎖定該行,直至該行被修改完畢,保證不產生並發沖突。這里還是難以理解,先記下來吧)
3.然後再修改
String update=" update uploadpicture "+
" set content=? "+
" where name=? ";
這里的問號是為PreparedStatement參數處理而寫的!
寫這個程序用到了oracle.sql.BLOB class ,這個類是用來操作BLOB數據類型的
當我們通過ResultSet 對象得到
blob=(BLOB)rs.getBlob(1);
的時候我不知道如何處理了,Blob 是什麼?String, int ,long? 我現在也不明白!估計CSDN上的人也不明白,否則我發個帖子半天沒有人回答,也許是很爛,也許是太簡單了,大家不屑一顧,看來我還要繼續追趕!
不發牢騷了,回到程序里(總覺得自己的發散思維很強,看來寫程序的時候不能這樣,多虧java 是純面向對象語言,如果是過程就麻煩了)
我們如何處理這個blob 呢?回答是,不管它是什麼,直接寫入 BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());
這里是建立了緩沖寫如blob 的流(注意getBinaryOutputStream()已經不被贊成使用了,一定有更優秀的方法替代!),說到流,我到現在還有點暈,類很多,不知道究竟用哪個好!
基礎的東西非常重要,這曾經是我的口頭禪,這里用到了流的讀入寫和寫入,有些流是從文件或其它位置上讀取位元組(如, FileInputStream),有寫流是把位元組組合成有用的數據(如, DataInputStream).我們讀取數字的時候,需要首先建議一個FileInpuStream, 然後, 再把該類的對象傳遞給DataInputStream
FileInputStream fin=new FileInputStream(「emp.dat」);
DataInputStream din=new DataInputStream(fin);//把fin傳遞給din
double s=din.readDouble();
默認情況下,流是沒有緩沖的, 如果使用緩沖就是
DataInputStream din=new DataInputStream(
new BufferedInputStream(new FileINputStream(「emp.dat」)));
有了這點理解也很管用,
BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());
就是建立一個緩沖寫的對象到blob.注意這里的out1 不是out,否則程序運行的時候不能列印了temp 數據了!
已經准備好如何寫了, 可是如何讀呢?
BufferedInputStream in=new BufferedInputStream(is);
在我們上傳的時候 (InputStream)is=fi.getInputStream();
讀取圖片為輸入的流.保存為is 對象,然後就用到這里了,准備好了讀和寫了,我們開始幹活:
int c;
while((c=in.read())!=-1) {out1.write(c);}
in.close();
out1.close();
通過緩沖一個個讀數據,然後一個個寫數據.-1 為文件的末尾,
最後當讀寫完成後我們要關閉讀寫對象!
程序分析就是這樣,以後還要對此問題進行研究,最後還要注意,
<%@ page contentType="image/jpeg;charset=GBK"%>
不是
<%@ page contentType="text/html;charset=GBK"%>
否則是以文字顯示圖片---亂碼.
這里研究了上傳圖片到oralce 裡面的程序,關於顯示還要麻煩一點,藉助資料我實現了,明天再研究一下.
//插入上傳圖片到資料庫
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="oracle.sql.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>getPicture.jsp</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
String name=null;
long size=0;
Connection conn=null;
String insert=" insert into uploadpicture "+
" values(?, empty_blob()) " ;
String findCursor=" select content "+
" from uploadpicture "+
" where name=? for update ";
String update=" update uploadpicture "+
" set content=? "+
" where name=? ";
BLOB blob=null;
InputStream is=null;
DiskFileUpload dfu=new DiskFileUpload();
dfu.setSizeMax(100000000);
dfu.setSizeThreshold(100000);
dfu.setRepositoryPath("f:\\public");
try{
List fileItems=dfu.parseRequest(request);
Iterator i=fileItems.iterator();
while(i.hasNext()){
FileItem fi=(FileItem)i.next();
if(!fi.isFormField()){
name=fi.getName();
size=fi.getSize();
if((name==null||name.equals(""))&&size==0)
continue;
}
name=fi.getName();
size=fi.getSize();
is=fi.getInputStream();
}
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");
conn=ds.getConnection();
conn.setAutoCommit(false);
//step 1
PreparedStatement ps=conn.prepareStatement(insert);
ps.setString(1, name);
int a=ps.executeUpdate();
if(a>0)
out.println("insert success!"+"<br>");
//step 2
ps=conn.prepareStatement(findCursor);
ps.setString(1, name);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
blob=(BLOB)rs.getBlob(1);
out.println("find cursor success!"+"<br>");
out.println("cursor :"+blob+"<br>");
//step 3
ps=conn.prepareStatement(update);
ps.setBlob(1, blob);
ps.setString(2, name);
ps.executeUpdate();
ps.close();
BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in=new BufferedInputStream(is);
int c;
while((c=in.read())!=-1) {out1.write(c);}
in.close();
out1.close();
out.println("update success!"+"<br>");}
conn.commit();
}
catch(SQLException se)
{se.printStackTrace();}
catch(FileUploadException fue)
{fue.printStackTrace();}
%>
</body>
</html>
//顯示資料庫裡面的圖片
<%@ page contentType="image/jpeg;charset=GBK"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.sun.image.codec.jpeg.*"%>
<%@ page import="javax.imageio.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.awt.image.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="image/jpeg; charset=GBK">
<title>showDBImage.jsp</title>
</head>
<body>
<%
String showImage=" select * "+
" from uploadpicture "+
" where name=´TXC with snow.JPG´ " ;
Connection conn=null;
BufferedInputStream inputImage=null;
try{
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");
conn=ds.getConnection();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(showImage);
while(rs.next())
{
oracle.sql.BLOB blob=(oracle.sql.BLOB)rs.getBlob("content");
inputImage =new BufferedInputStream(blob.getBinaryStream());
/*String name=rs.getString(1);
String content=rs.getString(2);
out.println(name+"<br>");*/}
BufferedImage image=null;
image=ImageIO.read(inputImage);
ServletOutputStream sos=response.getOutputStream();
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
inputImage.close();
conn.commit();
}
catch(SQLException se)
{se.printStackTrace();
conn.rollback(); }
catch(IOException ie)
{ie.printStackTrace();}
%>
</body>
</html>
『陸』 java程序oracle寫的sql取出是大寫列名,如何讓mysql取出也是大寫
有三種方式
1.創建表時設置:
CREATE TABLE T1(
A VARCHAR(10) BINARY
);
2、使用alter修改:
ALTER TABLE `專T1` MODIFY COLUMN `cloname` VARCHAR(45) BINARY;
3、mysql table editor中直接勾選屬BINARY項。
『柒』 java 處理 oracle clob類型 欄位
如果使用Oracle的話,直接將Clob欄位讀取為String;也就是下面這一行:
clob = (oracle.sql.CLOB) rs.getObject(1);
可以直接寫成
String str = rs.getString(1);
『捌』 java 大字元串轉為二進制流存入CLOB欄位 JDBC方法
dbField.getFieldValue().toString().getBytes();
上面這個值有嗎?取到了嗎?
下面是我以前一個項目的
InputStream pic = new FileInputStream(dto.get(i).getLibPic());
sql = "INSERT INTO piclib (name,pic,sign,remark) VALUES (?,?,?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, dto.get(i).getName());
pstmt.setBinaryStream(2,pic,(int)dto.get(i).getLibPic().length());
『玖』 oracle資料庫的大欄位怎麼在JAVA裡面獲取到
將COLOB類型,轉換成String類型
『拾』 java將string插入到類型為clob的ORACLE庫中
1:首先:寫個連接資料庫的類,裡面有返回mysq, oracle連接的方法
public Connection getConn(String flag){
Connection con=null;
try
{
if(flag.equals("1"))
{
Class.forName(「.jdbc.driver.OracleDriver」);
con = DriverManager.getConnection(「jdbc:oracle:thin:@IP:1521:資料庫名字」,"name","password");
}
if(flag.equals("2"))
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/資料庫名?user=用戶名&password=密碼&useUnicode=true&characterEncoding=GBK");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
2:執行插入操作
public void setData() {
conn = new Conn();
try {
String sqlfrom = "select p.id,p.content from table p order by p.id ";
String sqlinsert = "insert into table values(?,?)";
con = conn.getConn("2");
stmt = con.createStatement(); //從mysql取出大欄位
rs = stmt.executeQuery(sqlfrom);
con = conn.getConn("1");
PreparedStatement pstmt = con.prepareStatement(sqlinsert); //向oracle中插入大欄位
int i = 0;
while (rs.next()) {
pstmt.setInt(1, rs.getInt(1));
pstmt.setClob(2, oracle.sql.CLOB.empty_lob());
pstmt.executeUpdate(); //插入時將大欄位設為空
this.updateOne(con,rs.getInt(1),rs.getString(2)); // 這里調用然後更新這個大欄位
}
rs.close(); //關閉相關連接
pstmt.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
try
{
con.rollback();
} catch (Exception e1) {
System.out.println("回滾出現異常!");
e1.printStackTrace();
}
}
}
3:該方法實現對應大欄位記錄的更新
public void updateOne(Connection con,int id, String content) {
String str = "select t.content from table t where t.id=" + id+ " for update";
try {
// 注意:存取操作開始前,必須用setAutoCommit(false)取消自動提交,否則Oracle將拋出「讀取違反順序」的錯誤。
con.setAutoCommit(false);
stmt = con.createStatement();
ResultSet rs_clob = stmt.executeQuery(str);
while ( rs_clob .next()) {
/* 取出clob數據*/
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs_clob .getClob(1);
/* 向clob中寫入數據*/
clob.putString(1, content);
}
stmt.close();
con.commit();
con.setAutoCommit(true);
con.close();
} catch (Exception e) {
e.printStackTrace();
try
{
con.rollback();
} catch (Exception e1) {
System.out.println("回滾出現異常!");
e1.printStackTrace();
}
}
}
現在就完成了一行記錄的更新。
4:讀clob欄位以String 的形式返回(當然也可以將讀到的內容寫入文件,大家改一下就可以了)
/**
* 讀clob欄位
* @param con
* @param id
* @return
*/
public String readClob(Connection con,int id)
{
String content="";
try
{
con.setAutoCommit(false);
stmt=con.createStatement();
ResultSet rs_clob=stmt.executeQuery("select t.content from table t where t.id="+id);
oracle.sql.CLOB contents=null;
while (rs_clob.next())
{ // 取出CLOB對象
contents= (oracle.sql.CLOB) rs_clob.getClob(1);
}
BufferedReader a = new BufferedReader(contents.getCharacterStream()); //以字元流的方式讀入BufferedReader
String str = "";
while ((str = a.readLine()) != null) {
content = content.concat(str); //最後以String的形式得到
}
con.commit();
/*
BufferedWriter out = new BufferedWriter(new FileWriter("e:/test.txt"));
out.write(content); //寫入文件
out.close(); */
con.setAutoCommit(true);
con.close();
}catch(Exception e)
{
System.out.println("出現異常");
e.printStackTrace();
try
{
con.rollback();
}
catch (Exception e1)
{
System.out.println("回滾出現異常!");
e1.printStackTrace();
}
}
return content;
}