分享
 
 
 

JDBC存取Oralce数据库的注意几点事项

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

Be careful with date type column.

Just like Y2K, when you try to give a value to a column which type is date, you should determine the year area with YYYY not YY.

Suppose there is a user, Named Bob, Male, borned on 1977,Jan, 1st.

Insert into test (user_name, user_birthday, sex) values(‘Bob’, to_date(‘770109’,’YYMMDD’,’M’);

Ok, now let’s check Bob’s birthday:

Select to_char(birthday,’YYYYMMDD’) as birthday from test where user_name=’Bob’;

Birthday

20770109

Ok, its 21st century now, don’t forget it.

How to operate on Clob:

Clob type is to save single-byte character data up to 4 gigabytes.

If you are a newer to use clob, you must find it’s not so easy to operate on it! Ok, take it easy, from now on I’ll give an example to insert, update, retrieve clob type values from test.

→ Insert new value for clob type column:

Because it is now allowed to insert string directly into Clob type column, we must insert an Empty_clob into the table.

Suppose we have established a connection to database, and the reference of the Connection object it conn.

String sql = “insert into test (user_name, user_birthday, sex, biography) values(‘Bob’, to_date(‘19770109’,’yyyymmdd’), ‘M’,EMPTY_CLOB())”;

PreparedStatement stmt = conn.prepareStatement(sql);

Stmt.execute();

Now an Empty_Clob object has be signed to biography.

To give real value of biography column, use the following code fragment:

sql = “select biography from test where user_name=’Bob’ for update”;

stmt = conn.prepareStatement(sql);

ResultSet rs = stmt.executeQuery();

Please remembered the “for update” clause, it’s very important. This particular clause means you told the database to prepare the biography of Bob for update.

Oracle.sql.CLOB clob = (oracle.sql.CLOB)((oracle.jdbc.OracleResultSet)rs).getCLOB("biography");

Java.io.OutputStream clobWriter = clob.getAsciiOutputStream();

byte[] temp = currentBiography.getBytes();

(supposed currentBiography is the value to be update to database)

clobWriter.write(temp);

clobWriter.flush();

clobWriter.close();

rs.close();

stmt.close();

conn.commit();

Ok, the biography has been updated.

→ When you retrieve clob from database, remember to use InputStream.

oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("biography");

java.io.InputStream asciiStream = clob.getAsciiStream();

byte[] tempBody = new byte[1024]; //set to 1K;

int readLen = asciiStream.read(tempBody,0,1024);

String tempString = (new String(tempBody)).substring(0,readLen);

Be careful with PreparedStatement

When you use PreparedStatement to pre-compile and execute SQL String, you must close it after transaction ending. Otherwise you’ll got an exception named ORA 0001: maximum cursors exceeded!

I’m not sure about cursor, there is a parameter in the init.ora file which will used by oracle database. In this file, max cursors is defined.

Actually when try to prepare statement and execute in a loop block. You may also cause ORA 0001 exception.

So only prepare statement once for particular sql strings set. Use set XXX(int index, XXX value) method to set column values.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有