看clob的实现类SerialClob得构造你就明白了。
CLOB是存放在一个大数组里面的
public SerialClob(char ch[]) throws SerialException, SQLException {
// %%% JMB Agreed Add code here to throw a SQLException if no
// support is available for locatorsUpdateCopy=false
// Serializing locators is not supported
len = chlength;
buf = new char[(int)len];
for (int i = 0; i < len ; i++){
buf[i] = ch[i];
}
origLen = len;
}
你试试我这段代码,缩放的
/
缩小或者放大
@param data 的byte数据
@param nWidth 要放大缩小的宽度
@param nHeight 要放大或缩小的高度
@return 返回放大或缩小后的的byte数据
/
public static byte[] ChangeImgSize(byte[] data,int nWidth,int nHeight) {
byte[] newdata=null;
try {
BufferedImage bImage=ImageIOread(new ByteArrayInputStream(data));
int w=bImagegetWidth();
int h=bImagegetHeight();
double sx=(double)nWidth/w;
double sy=(double)nHeight/h;
AffineTransform transform=new AffineTransform();
transformsetToScale(sx, sy);
AffineTransformOp ato=new AffineTransformOp(transform, null);
//原始颜色
BufferedImage bImage2=new BufferedImage(nWidth, nHeight, BufferedImageTYPE_3BYTE_BGR);
atofilter(bImage, bImage2);
//转换成byte
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIOwrite(bImage2, "jpeg", baos);
newdata=baostoByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
Systemoutprintln("ChangeImgSize失败:"+etoString());
}
return newdata;
}
解决方案:
Map集合中提供了两种取出方式:
<1> 返回值类型Set<k> 方法是: keySet() :返回此映射中包含的键的 Set 视图
将map中所有的键存入到Set集合,因为set具备迭代器,所有迭代方式取出所有的键
再根据get()方法 ,获取每一个键对应的值
<2> 返回值类型:Set<MapEntry<K,V>>方法是:entrySet()
取出的是关系,关系中包含key和value,其中MapEntry<k,V>来表示这种数据类型
即:将map集合中的映射关系存入到set集合中,这个关系的数据类型为:MapEntry
MapEntry接口
此接口在javautil包中,其实Entry也是一个接口,它是Map接口中的一个内部接口 ,getKey()和getValue是接口MapEntry<K,V>中的方法,返回对应的键和对应的值
Map集合中是没有迭代器 的 ,Map集合取出键值的原理:将map集合转成set集合,再通过迭代器取出 。
怎么读取orcl数据库,表中字段是clob类型的一条数据
写入clob数据
import javaioWriter;
import javasqlConnection;
import javasqlDriverManager;
import javasqlResultSet;
import javasqlStatement;
public class TestClobIn {
public static void main(String args[]){
String data="this is a long passage!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
Writer outStream = null;
//通过JDBC获得数据库连接
try {
ClassforName("OraclejdbcdriverOracleDriver");
Connection con = DriverManagergetConnection("jdbc:oracle:thin:@localhost:1521:ewins", "scott", "tiger");
consetAutoCommit(false);
Statement st = concreateStatement();
//插入一个空对象empty_clob(),这个是必须的
stexecuteUpdate("insert into TESTCLOB(ID, NAME, CLOBATTR)values(2,'thename', empty_clob())");
//锁定数据行进行更新,注意“for update”语句,这里不用for update锁定不可以插入clob
ResultSet rs = stexecuteQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
if (rsnext())
{
//得到javasqlClob对象后强制转换为oraclesqlCLOB
oraclesqlCLOB clob = (oraclesqlCLOB) rsgetClob("CLOBATTR");
outStream = clobgetCharacterOutputStream();
//data是传入的字符串,定义:String data
char[] c = datatoCharArray();
outStreamwrite(c, 0, clength);
}
outStreamflush();
outStreamclose();
concommit();
conclose();
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
读取clob数据
import javaioInputStream;
import javaioReader;
import javasqlConnection;
import javasqlResultSet;
import javasqlStatement;
public class TestClobOut {
public static void main(String args[]){
String data;
Reader inStream=null;
//获得数据库连接
Connection con = ConnectionFactorygetConnection();//ConnectionFactory类是另外定义的,不必纠结
consetAutoCommit(false);
Statement st = concreateStatement();
//不需要“for update”
ResultSet rs = stexecuteQuery("select CLOBATTR from TESTCLOB where ID=1");
if (rsnext())
{
javasqlClob clob = rsgetClob("CLOBATTR");
inStream = clobgetCharacterStream();
char[] c = new char[(int) cloblength()];
inStreamread(c);
//data是读出并需要返回的数据,类型是String
data = new String(c);
inStreamclose();
}
inStreamclose();
concommit();
conclose();
}
}
我以前写程序发现:
oracle的数据库BLOB不能用来存储字符,改用NCLOB就OK了。
BLOB:用来存储无结构的二进制数据
CLOB:存储单字节字符数据。(别用来存中文喔。。。)
NCLOB:用来存储定宽多字节字符数据。
/
Created by IntelliJ IDEA
User: ljt
Date: 2003-3-31
Time: 18:51:38
To change this template use Options | File Templates
/
import oraclejdbcdriverOraclePreparedStatement;
import oraclejdbcdriverOracleResultSet;
import javasqlConnection;
import javasqlDriverManager;
import javasqlStatement;
import javasqlClob;
public class TestOpenDoc {
public OracleResultSet ors = null; //这里rs一定要用Oracle提供的
public OraclePreparedStatement opst = null; //PreparedStatement用
public Connection conn = null;
public Statement stmt = null;
public TestOpenDoc() {
}
public boolean getConnect() {
//这是我的数据库所在
String serverName = "prosrv";
try {
ClassforName("oraclejdbcdriverOracleDriver");
String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA";
conn = DriverManagergetConnection(url, "appuser", "appuser");
}
catch (Exception e) {
Systemoutprintln(e);
return false;
}
return true;
}
public static void main(String[] args) {
TestOpenDoc test = new TestOpenDoc();
if (!testgetConnect()) {
Systemoutprintln("数据库连结错误");
return ;
}
try{
testconnsetAutoCommit(false);
byte a[] = null; //将测试文件testdoc读入此字节数组
javaioFileInputStream fin = null;
javaioFileOutputStream fout = null;
//Oracle提供的
try {
javaioFile f1 = new javaioFile("c:/testdoc");
javaioFile f2 = new javaioFile("d:/testoutdoc"); //从BLOB读出的信息写
//入该文 件,和源文件对比测试用
fin = new javaioFileInputStream(f1);
fout = new javaioFileOutputStream(f2);
int flength = (int) f1length(); //读入文件的字节长度
Systemoutprintln("file length::" + flength);
a = new byte[flength];
int i = 0;
int itotal = 0;
// 将文件读入字节数组
for (; itotal < flength; itotal = i + itotal) {
i = finread(a, itotal, flength - itotal);
}
finclose();
Systemoutprintln("read itotal::" + itotal);
//注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化
String mysql =
"insert into filelist (FileName,FileSize,FileBody) values (,,EMPTY_BLOB())";
OraclePreparedStatement opst = (OraclePreparedStatement) testconn
prepareStatement(mysql);
opstsetString(1, "wordtemplate2");
opstsetInt(2, flength);
opstexecuteUpdate();
opstclearParameters();
// /插入其它数据后,定位BLOB字段
mysql = "select filebody from filelist where filename=";
opst = (OraclePreparedStatement) testconnprepareStatement(mysql);
opstsetString(1, "wordtemplate2");
OracleResultSet ors = (OracleResultSet) opstexecuteQuery();
if (orsnext()) {
oraclesqlBLOB blob = orsgetBLOB(1); //得到BLOB字段
int j = blobputBytes(1, a); //将字节数组写入BLOB字段
Systemoutprintln("j:" + j);
testconncommit();
orsclose();
Clob clob;
clob = orsgetClob("");
String str;
str = clobtoString();
str = clobgetSubString(0L,(int)cloblength());
Systemoutprintln(str);
}
Systemoutprintln("insert into ok");
byte b[] = null; //保存从BLOB读出的字节
opstclearParameters();
mysql = "select filebody from filelist where filename=";
opst = (OraclePreparedStatement) testconn
prepareStatement(mysql);
opstsetString(1, "wordtemplate2");
ors = (OracleResultSet) opstexecuteQuery();
if (orsnext()) {
oraclesqlBLOB blob2 = orsgetBLOB(1);
Systemoutprintln("blob2 length:" + blob2length());
b = blob2getBytes(1, flength); //从BLOB取出字节流数据
Systemoutprintln("b length::" + blength);
testconncommit();
}
orsclose();
// 将从BLOB读出的字节写入文件
foutwrite(b, 0, blength);
foutclose();
Systemoutprintln("write itotal::" + blength);
}
catch (Exception e) {
Systemoutprintln("errror :" + etoString());
eprintStackTrace();
}
finally { //关闭所有数据联接
testconncommit();
}
}
catch(Exception e){
Systemoutprintln(e);
}
}
}
以上就是关于JAVA Clob问题全部的内容,包括:JAVA Clob问题、clob字段中含有图片,如何单独拿出来,再控制大小,JAVA中、如何在map类型的list里面取出CLOB数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)