ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÈçºÎͨ¹ýJDBCÁ¬½Óµ½OracleÊý¾Ý¿â

·¢²¼Ê±¼ä:2006-09-26 00:16:46À´Ô´:ºìÁª×÷Õß:ĪÞÈ
µ¼ ¶Á:JavaÊý¾Ý¿âÁ¬½Ó£¨JDBC£©APIÊÇһϵÁÐÄܹ»ÈÃJava±à³ÌÈËÔ±·ÃÎÊÊý¾Ý¿âµÄ½Ó¿Ú£¬¸÷¸ö¿ª·¢É̵Ľӿڲ¢²»ÍêÈ«Ïàͬ¡£ÔÚʹÓöàÄêµÄOracle¹«Ë¾µÄJDBCºó£¬ÎÒ»ýÀÛÁËÐí¶à¼¼ÇÉ£¬ÕâЩ¼¼ÇÉÄܹ»Ê¹ÎÒÃǸüºÃµØ·¢»ÓϵͳµÄÐÔÄܺÍʵÏÖ¸ü¶àµÄ¹¦ÄÜ¡£

JavaÊý¾Ý¿âÁ¬½Ó£¨JDBC£©APIÊÇһϵÁÐÄܹ»ÈÃJava±à³ÌÈËÔ±·ÃÎÊÊý¾Ý¿âµÄ½Ó¿Ú£¬¸÷¸ö¿ª·¢É̵Ľӿڲ¢²»ÍêÈ«Ïàͬ¡£ÔÚʹÓöàÄêµÄOracle¹«Ë¾µÄJDBCºó£¬ÎÒ»ýÀÛÁËÐí¶à¼¼ÇÉ£¬ÕâЩ¼¼ÇÉÄܹ»Ê¹ÎÒÃǸüºÃµØ·¢»ÓϵͳµÄÐÔÄܺÍʵÏÖ¸ü¶àµÄ¹¦ÄÜ¡£

1¡¢ÔÚ¿Í»§¶ËÈí¼þ¿ª·¢ÖÐʹÓÃThinÇý¶¯³ÌÐò

ÔÚ¿ª·¢JavaÈí¼þ·½Ã棬OracleµÄÊý¾Ý¿âÌṩÁËËÄÖÖÀàÐ͵ÄÇý¶¯³ÌÐò£¬¶þÖÖÓÃÓÚÓ¦ÓÃÈí¼þ¡¢applets¡¢servletsµÈ¿Í»§¶ËÈí¼þ£¬ÁíÍâ¶þÖÖÓÃÓÚÊý¾Ý¿âÖеÄJava´æ´¢¹ý³ÌµÈ·þÎñÆ÷¶ËÈí¼þ¡£ÔÚ¿Í»§»ú¶ËÈí¼þµÄ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔÑ¡ÔñOCIÇý¶¯³ÌÐò»òThinÇý¶¯³ÌÐò¡£OCIÇý¶¯³ÌÐòÀûÓÃJava±¾µØ»¯½Ó¿Ú£¨JNI£©£¬Í¨¹ýOracle¿Í»§¶ËÈí¼þÓëÊý¾Ý¿â½øÐÐͨѶ¡£ThinÇý¶¯³ÌÐòÊÇ´¿JavaÇý¶¯³ÌÐò£¬ËüÖ±½ÓÓëÊý¾Ý¿â½øÐÐͨѶ¡£ÎªÁË»ñµÃ×î¸ßµÄÐÔÄÜ£¬Oracle½¨ÒéÔÚ¿Í»§¶ËÈí¼þµÄ¿ª·¢ÖÐʹÓÃOCIÇý¶¯³ÌÐò£¬ÕâËƺõÊÇÕýÈ·µÄ¡£µ«ÎÒ½¨ÒéʹÓÃThinÇý¶¯³ÌÐò£¬ÒòΪͨ¹ý¶à´Î²âÊÔ·¢ÏÖ£¬ÔÚͨ³£Çé¿öÏ£¬ThinÇý¶¯³ÌÐòµÄÐÔÄܶ¼³¬¹ýÁËOCIÇý¶¯³ÌÐò¡£

2¡¢¹Ø±Õ×Ô¶¯Ìá½»¹¦ÄÜ£¬Ìá¸ßϵͳÐÔÄÜ

ÔÚµÚÒ»´Î½¨Á¢ÓëÊý¾Ý¿âµÄÁ¬½Óʱ£¬ÔÚȱʡÇé¿öÏ£¬Á¬½ÓÊÇÔÚ×Ô¶¯Ìύģʽϵġ£ÎªÁË»ñµÃ¸üºÃµÄÐÔÄÜ£¬¿ÉÒÔͨ¹ýµ÷Óôø²¼¶ûÖµfalse²ÎÊýµÄConnectionÀàµÄsetAutoCommit()·½·¨¹Ø±Õ×Ô¶¯Ìá½»¹¦ÄÜ£¬ÈçÏÂËùʾ£º


conn.setAutoCommit(false);


ÖµµÃ×¢ÒâµÄÊÇ£¬Ò»µ©¹Ø±ÕÁË×Ô¶¯Ìá½»¹¦ÄÜ£¬ÎÒÃǾÍÐèҪͨ¹ýµ÷ÓÃConnectionÀàµÄcommit()ºÍrollback()·½·¨À´È˹¤µÄ·½Ê½¶ÔÊÂÎñ½øÐйÜÀí¡£

3¡¢ÔÚ¶¯Ì¬SQL»òÓÐʱ¼äÏÞÖƵÄÃüÁîÖÐʹÓÃStatement¶ÔÏó

ÔÚÖ´ÐÐSQLÃüÁîʱ£¬ÎÒÃÇÓжþÖÖÑ¡Ôñ£º¿ÉÒÔʹÓÃPreparedStatement¶ÔÏó£¬Ò²¿ÉÒÔʹÓÃStatement¶ÔÏó¡£ÎÞÂÛ¶àÉٴεØʹÓÃͬһ¸öSQLÃüÁPreparedStatement¶¼Ö»¶ÔËü½âÎöºÍ±àÒëÒ»´Î¡£µ±Ê¹ÓÃStatement¶ÔÏóʱ£¬Ã¿´ÎÖ´ÐÐÒ»¸öSQLÃüÁîʱ£¬¶¼»á¶ÔËü½øÐнâÎöºÍ±àÒë¡£Õâ¿ÉÄÜ»áʹÄãÈÏΪ£¬Ê¹ÓÃPreparedStatement¶ÔÏó±ÈʹÓÃStatement¶ÔÏóµÄËٶȸü¿ì¡£È»¶ø£¬ÎÒ½øÐеIJâÊÔ±íÃ÷£¬ÔÚ¿Í»§¶ËÈí¼þÖУ¬Çé¿ö²¢·ÇÈç´Ë¡£Òò´Ë£¬ÔÚÓÐʱ¼äÏÞÖƵÄSQL²Ù×÷ÖУ¬³ý·Ç³ÉÅúµØ´¦ÀíSQLÃüÁÎÒÃÇÓ¦µ±¿¼ÂÇʹÓÃStatement¶ÔÏó¡£

´ËÍ⣬ʹÓÃStatement¶ÔÏóҲʹµÃ±àд¶¯Ì¬SQLÃüÁî¸ü¼Ó¼òµ¥£¬ÒòΪÎÒÃÇ¿ÉÒÔ½«×Ö·û´®Á¬½ÓÔÚÒ»Æ𣬽¨Á¢Ò»¸öÓÐЧµÄSQLÃüÁî¡£Òò´Ë£¬ÎÒÈÏΪ£¬Statement¶ÔÏó¿ÉÒÔʹ¶¯Ì¬SQLÃüÁîµÄ´´½¨ºÍÖ´ÐбäµÃ¸ü¼Ó¼òµ¥¡£

4¡¢ÀûÓÃhelperº¯Êý¶Ô¶¯Ì¬SQLÃüÁî½øÐиñʽ»¯

ÔÚ´´½¨Ê¹ÓÃStatement¶ÔÏóÖ´ÐеĶ¯Ì¬SQLÃüÁîʱ£¬ÎÒÃÇÐèÒª´¦ÀíһЩ¸ñʽ»¯·½ÃæµÄÎÊÌâ¡£ÀýÈ磬Èç¹ûÎÒÃÇÏë´´½¨Ò»¸ö½«Ãû×ÖO'Reilly²åÈë±íÖеÄSQLÃüÁÔò±ØÐëʹÓöþ¸öÏàÁ¬µÄ¡°''¡±ºÅÌæ»»O'ReillyÖеġ°'¡±ºÅ¡£Íê³ÉÕâЩ¹¤×÷µÄ×îºÃµÄ·½·¨ÊÇ´´½¨Ò»¸öÍê³ÉÌæ»»²Ù×÷µÄhelper·½·¨£¬È»ºóÔÚÁ¬½Ó×Ö·û´®ÐÄ·þÓù«Ê½±í´ïÒ»¸öSQLÃüÁîʱ£¬Ê¹Óô´½¨µÄhelper·½·¨¡£Óë´ËÀàËƵÄÊÇ£¬ÎÒÃÇ¿ÉÒÔÈÃhelper·½·¨½ÓÊÜÒ»¸öDateÐ͵ÄÖµ£¬È»ºóÈÃËüÊä³ö»ùÓÚOracleµÄto_date()º¯ÊýµÄ×Ö·û´®±í´ïʽ¡£

5¡¢ÀûÓÃPreparedStatement¶ÔÏóÌá¸ßÊý¾Ý¿âµÄ×ÜÌåЧÂÊ

ÔÚʹÓÃPreparedStatement¶ÔÏóÖ´ÐÐSQLÃüÁîʱ£¬ÃüÁî±»Êý¾Ý¿â½øÐнâÎöºÍ±àÒ룬Ȼºó±»·Åµ½ÃüÁ³åÇø¡£È»ºó£¬Ã¿µ±Ö´ÐÐͬһ¸öPreparedStatement¶ÔÏóʱ£¬Ëü¾Í»á±»ÔÙ½âÎöÒ»´Î£¬µ«²»»á±»ÔٴαàÒë¡£ÔÚ»º³åÇøÖпÉÒÔ·¢ÏÖÔ¤±àÒëµÄÃüÁ²¢ÇÒ¿ÉÒÔÖØÐÂʹÓá£ÔÚÓдóÁ¿Óû§µÄÆóÒµ¼¶Ó¦ÓÃÈí¼þÖУ¬¾­³£»áÖظ´Ö´ÐÐÏàͬµÄSQLÃüÁʹÓÃPreparedStatement¶ÔÏó´øÀ´µÄ±àÒë´ÎÊýµÄ¼õÉÙÄܹ»Ìá¸ßÊý¾Ý¿âµÄ×ÜÌåÐÔÄÜ¡£Èç¹û²»ÊÇÔÚ¿Í»§¶Ë´´½¨¡¢Ô¤±¸¡¢Ö´ÐÐPreparedStatementÈÎÎñÐèÒªµÄʱ¼ä³¤ÓÚStatementÈÎÎñ£¬ÎһὨÒéÔÚ³ý¶¯Ì¬SQLÃüÁîÖ®ÍâµÄËùÓÐÇé¿öÏÂʹÓÃPreparedStatement¶ÔÏó¡£

6¡¢ÔÚ³ÉÅú´¦ÀíÖظ´µÄ²åÈë»ò¸üвÙ×÷ÖÐʹÓÃPreparedStatement¶ÔÏó

Èç¹û³ÉÅúµØ´¦Àí²åÈëºÍ¸üвÙ×÷£¬¾ÍÄܹ»ÏÔÖøµØ¼õÉÙËüÃÇËùÐèÒªµÄʱ¼ä¡£OracleÌṩµÄStatementºÍ CallableStatement²¢²»ÕæÕýµØÖ§³ÖÅú´¦Àí£¬Ö»ÓÐPreparedStatement¶ÔÏó²ÅÕæÕýµØÖ§³ÖÅú´¦Àí¡£ÎÒÃÇ¿ÉÒÔʹÓÃaddBatch()ºÍexecuteBatch()·½·¨Ñ¡Ôñ±ê×¼µÄJDBCÅú´¦Àí£¬»òÕßͨ¹ýÀûÓÃPreparedStatement¶ÔÏóµÄsetExecuteBatch()·½·¨ºÍ±ê×¼µÄexecuteUpdate()·½·¨Ñ¡ÔñËٶȸü¿ìµÄOracleרÓеķ½·¨¡£ÒªÊ¹ÓÃOracleרÓеÄÅú´¦Àí»úÖÆ£¬¿ÉÒÔÒÔÈçÏÂËùʾµÄ·½Ê½µ÷ÓÃsetExecuteBatch()£º


PreparedStatement pstmt3D null;
try {
((OraclePreparedStatement)
pstmt).setExecuteBatch(30);
...
pstmt.executeUpdate();
}


µ÷ÓÃsetExecuteBatch()ʱָ¶¨µÄÖµÊÇÒ»¸öÉÏÏÞ£¬µ±´ïµ½¸Ãֵʱ£¬¾Í»á×Ô¶¯µØÒý·¢SQLÃüÁîÖ´ÐУ¬±ê×¼µÄexecuteUpdate()·½·¨¾Í»á±»×÷ΪÅú´¦ÀíË͵½Êý¾Ý¿âÖС£ÎÒÃÇ¿ÉÒÔͨ¹ýµ÷ÓÃPreparedStatementÀàµÄsendBatch()·½·¨Ëæʱ´«ÊäÅú´¦ÀíÈÎÎñ¡£

7¡¢Ê¹ÓÃOracle locator·½·¨²åÈë¡¢¸üдó¶ÔÏó£¨LOB£©

OracleµÄPreparedStatementÀ಻ÍêÈ«Ö§³ÖBLOBºÍCLOBµÈ´ó¶ÔÏóµÄ´¦Àí£¬ÓÈÆäÊÇThinÇý¶¯³ÌÐò²»Ö§³ÖÀûÓÃPreparedStatement¶ÔÏóµÄsetObject()ºÍsetBinaryStream()·½·¨ÉèÖÃBLOBµÄÖµ£¬Ò²²»Ö§³ÖÀûÓÃsetCharacterStream()·½·¨ÉèÖÃCLOBµÄÖµ¡£Ö»ÓÐlocator±¾ÉíÖеķ½·¨²ÅÄܹ»´ÓÊý¾Ý¿âÖлñÈ¡LOBÀàÐ͵ÄÖµ¡£¿ÉÒÔʹÓÃPreparedStatement¶ÔÏó²åÈë»ò¸üÐÂLOB£¬µ«ÐèҪʹÓÃlocator²ÅÄÜ»ñÈ¡LOBµÄÖµ¡£ÓÉÓÚ´æÔÚÕâ¶þ¸öÎÊÌ⣬Òò´Ë£¬ÎÒ½¨ÒéʹÓÃlocatorµÄ·½·¨À´²åÈë¡¢¸üлò»ñÈ¡LOBµÄÖµ¡£

8¡¢Ê¹ÓÃSQL92Óï·¨µ÷Óô洢¹ý³Ì

ÔÚµ÷Óô洢¹ý³Ìʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃSQL92»òOracle PL/SQL£¬ÓÉÓÚʹÓÃOracle PL/SQL²¢Ã»ÓÐʲôʵ¼ÊµÄºÃ´¦£¬¶øÇÒ»á¸øÒÔºóά»¤ÄãµÄÓ¦ÓóÌÐòµÄ¿ª·¢ÈËÔ±´øÀ´Âé·³£¬Òò´Ë£¬ÎÒ½¨ÒéÔÚµ÷Óô洢¹ý³ÌʱʹÓÃSQL92¡£

9¡¢Ê¹ÓÃObject SQL½«¶ÔÏóģʽתÒƵ½Êý¾Ý¿âÖÐ

¼ÈÈ»¿ÉÒÔ½«OracleµÄÊý¾Ý¿â×÷ΪһÖÖÃæÏò¶ÔÏóµÄÊý¾Ý¿âÀ´Ê¹Ó㬾ͿÉÒÔ¿¼Âǽ«Ó¦ÓóÌÐòÖеÄÃæÏò¶ÔÏóģʽתµ½Êý¾Ý¿âÖС£Ä¿Ç°µÄ·½·¨ÊÇ´´½¨Java bean×÷ΪαװµÄÊý¾Ý¿â¶ÔÏ󣬽«ËüÃǵÄÊôÐÔÓ³Éäµ½¹Øϵ±íÖУ¬È»ºóÔÚÕâЩbeanÖÐÌí¼Ó·½·¨¡£¾¡¹ÜÕâÑù×÷ÔÚJavaÖÐûÓÐʲôÎÊÌ⣬µ«ÓÉÓÚ²Ù×÷¶¼ÊÇÔÚÊý¾Ý¿âÖ®Íâ½øÐеģ¬Òò´ËÆäËû·ÃÎÊÊý¾Ý¿âµÄÓ¦ÓÃÈí¼þÎÞ·¨ÀûÓöÔÏóģʽ¡£Èç¹ûÀûÓÃOracleµÄÃæÏò¶ÔÏóµÄ¼¼Êõ£¬¿ÉÒÔͨ¹ý´´½¨Ò»¸öеÄÊý¾Ý¿â¶ÔÏóÀàÐÍÔÚÊý¾Ý¿âÖÐÄ£·ÂÆäÊý¾ÝºÍ²Ù×÷£¬È»ºóʹÓÃJPublisherµÈ¹¤¾ßÉú³É×Ô¼ºµÄJava beanÀà¡£Èç¹ûʹÓÃÕâÖÖ·½Ê½£¬²»µ«JavaÓ¦ÓóÌÐò¿ÉÒÔʹÓÃÓ¦ÓÃÈí¼þµÄ¶ÔÏóģʽ£¬ÆäËûÐèÒª¹²ÏíÄãµÄÓ¦ÓÃÖеÄÊý¾ÝºÍ²Ù×÷µÄÓ¦ÓÃÈí¼þÒ²¿ÉÒÔʹÓÃÓ¦ÓÃÈí¼þÖеĶÔÏóģʽ¡£

10¡¢ÀûÓÃSQLÍê³ÉÊý¾Ý¿âÄڵIJÙ×÷

ÎÒÒªÏò´ó¼Ò½éÉܵÄ×îÖØÒªµÄ¾­ÑéÊdzä·ÖÀûÓÃSQLµÄÃæÏò¼¯ºÏµÄ·½·¨À´½â¾öÊý¾Ý¿â´¦ÀíÐèÇ󣬶ø²»ÊÇʹÓÃJavaµÈ¹ý³Ì»¯µÄ±à³ÌÓïÑÔ¡£

Èç¹û±à³ÌÈËÔ±ÒªÔÚÒ»¸ö±íÖвéÕÒÐí¶àÐУ¬½á¹ûÖеÄÿ¸öÐж¼»á²éÕÒÆäËû±íÖеÄÊý¾Ý£¬×îºó£¬±à³ÌÈËÔ±´´½¨Á˶ÀÁ¢µÄUPDATEÃüÁîÀ´³ÉÅúµØ¸üеÚÒ»¸ö±íÖеÄÊý¾Ý¡£Óë´ËÀàËƵÄÈÎÎñ¿ÉÒÔͨ¹ýÔÚset×Ó¾äÖÐʹÓöàÁÐ×Ó²éѯ¶øÔÚÒ»¸öUPDATEÃüÁîÖÐÍê³É¡£µ±Äܹ»ÔÚµ¥Ò»µÄSQLÃüÁîÖÐÍê³ÉÈÎÎñ£¬ºÎ±ØÒªÈÃÊý¾ÝÔÚÍøÉÏÁ÷À´Á÷È¥µÄ£¿ÎÒ½¨ÒéÓû§ÈÏÕæѧϰÈçºÎ×î´óÏ޶ȵط¢»ÓSQLµÄ¹¦ÄÜ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ