Database °æ (¾«»ªÇø)
×÷ ¼Ò: baby (ÐÄÔÂ) on board 'database'
Ìâ Ä¿: JDBC
À´ Ô´: ¹þ¶û±õ×϶¡ÏãÕ¾
ÈÕ ÆÚ: Tue Jun 24 10:14:09 1997
³ö ´¦: lupy.bbs@melon.gznet.edu.cn
·¢ÐÅÈË: kaler (º£×Ó), ÐÅÇø: Database
±ê Ìâ: [ת]jdbc (תÔØ)
·¢ÐÅÕ¾: »ªÄÏľÃÞÕ¾ (Wed Sep 18 14:14:11 1996)
1. ½éÉÜ
Ðí¶à¿ª·¢ÕߺÍÓû§¶¼ÔÚÑ°ÕÒJava³ÌÐòÖзÃÎÊÊý¾Ý¿âµÄ±ã½Ý·½
·¨¡£ÓÉÓÚJavaÊÇÒ»¸ö½¡×³£¬°²È«£¬Ò×ÓÚʹÓõģ¬Ò×ÓÚÀí½âÇÒ¿É
ÒÔ´ÓÍøÂçÖÐ×Ô¶¯download £¬ËùÒÔËü³ÉΪ¿ª·¢Êý¾Ý¿âÓ¦ÓõÄÒ»ÖÖ
Á¼ºÃµÄÓïÑÔ»ù´¡¡£ËüÌṩÁËC,C ,Smalltalk, BASIC, COBOL,
and 4GLsµÄÐí¶àÓŵ㡣Ðí¶à¹«Ë¾ÒѾ¿ªÊ¼ÔÚJavaÓëDBMSµÄÁ¬½Ó
·½Ãæ×ö¹¤×÷¡£
Ðí¶àJavaÓ¦Óÿª·¢Õ߶¼Ï£ÍûÄܹ»±àд¶ÀÁ¢ÓÚÌض¨DBMSµÄ³Ì
Ðò£¬¶øÎÒÃÇÒ²ÏàÐÅÒ»¸ö¶ÀÁ¢ÓÚDBMSµÄ½Ó¿Ú½«Ê¹µÃÓë¸÷ÖÖ¸÷Ñù
DBMSÁ¬½Ó±äµÃ×îΪ±ã½Ý£¬¿ª·¢¸ü¼ÓѸËÙ¡£ËùÒÔÎÒÃÇÈÏΪ¶¨Òå
Ò»¸öͨÓõÄSQLÊý¾Ý¿â´æÈ¡¿ò¼Ü£¬ÔÚ¸÷ÖÖ¸÷ÑùµÄÌṩÊý¾Ý¿âÁ¬
½ÓÄ£¿éÉÏÌṩͳһµÄ½çÃæÊÇÊ®·ÖÓÐÒâÒåµÄ¡£Õâʹ³ÌÐòÔ±¿ÉÒÔÃæ
¶Ôµ¥Ò»µÄÊý¾Ý¿â½çÃ棬ʹÊý¾Ý¿âÎ޹صÄJava¹¤¾ßºÍ²úÆ·³ÉΪ¿É
ÄÜ£¬Ê¹µÃÊý¾Ý¿âÁ¬½ÓµÄ¿ª·¢Õß¿ÉÒÔÌṩ¸÷ÖÖ¸÷ÑùµÄÁ¬½Ó·½°¸¡£
ÎÒÃÇ¿´µ½ÎÒÃǶ¨ÒåÒ»¸öͨÓõͲãµÄ£¬Ö§³Ö»ù±¾SQL¹¦ÄܵÄJava
DataBase Connectivity (JDBC)APIµÄ½ôÆÈÈÎÎñ¡£
ÐÒÔ˵ÄÊÇÎÒÃDz»±Ø´ÓÍ·Éè¼ÆÒ»¸öSQL API¡£ÎÒÃÇ¿ÉÒÔ°ÑÎÒÃǵÄ
¹¤×÷½¨Á¢ÔÚ X/Open SQL CLI (µ÷Óòã½Ó¿Ú)Ö®ÉÏ(ËüÒ²ÊÇ
Microsoft's ODBC µÄ»ù´¡)¡£
ÎÒÃÇÖ÷ÒªÈÎÎñÊǶ¨ÒåÒ»¸ö×ÔÈ»µÄJava½Ó¿ÚÀ´ÓëX/Open CLIÖж¨
ÒåµÄ»ù±¾µÄ³éÏó²ãºÍ¸ÅÄîÁ¬½Ó¡£
JDBC APIµÃµ½Êý¾Ý¿â¿ª·¢³§ÉÌ£¬Á¬½Ó¿ª·¢³§ÉÌ£¬ISV,ÒÔ¼°Ó¦ÓÃ
¿ª·¢ÕßµÄÖ§³ÖÊÇÊ®·ÖÖØÒªµÄ¡£ÎÒÃÇÏàÐÅ°ÑÎÒÃǵŤ×÷½¨Á¢ÔÚ
ODBC³éÏó²ãµÄ»ù´¡ÉϽ«JDBC¸ü¼ÓÈÝÒ׵õ½´ó¼ÒµÄ½ÓÊÜ¡£¶øÇÒ
´Ó¼¼ÊõÉÏÀ´Ëµ£¬ODBCÊÇÎÒÃÇÉè¼Æ¹¤×÷µÄÒ»¸öÁ¼ºÃ»ù´¡¡£
ÒòΪODBCÊÇÒ»¸öCÓïÑÔ½Ó¿Ú£¬ËùÒÔODBCÔÚJavaÖÐÖ±½ÓʹÓò»
Êʵ±¡£´ÓJavaÖÐÀ´µ÷ÓÃC´úÂëÔÚ°²È«ÐÔ£¬½¡×³ÐÔ£¬ÊµÏֵķ½±ã£¬
¿ÉÒÆÖ²ÐԵȵȷ½ÃæÓÐÐí¶à²»±ã¡£ËüʹµÃJavaÔÚÕâЩ·½ÃæµÄÐí¶à
ÓŵãµÃ²»µ½·¢»Ó¡£
ÎÒÃÇÒѾÔÚ¶ÌÆÚÀïÃæʵÏÖÁËÒ»¸ö½¨Á¢ÔÚODBCÉϵÄAPI¡£³¤Ô¶À´
¿´£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÆäËû·½Ê½ÌṩʵÏÖ¡£
1.1. ×¢Òâ
ÎÒÃǷdz£¸ÐлÔÚÊý¾Ý¿â£¬Êý¾Ý¿âÁ¬½ÓºÍÊý¾Ý¿â¹¤¾ßÁìÓòµÄÐí¶à
ÔçÆڵŤ×÷Õß¡£ËûÃÇΪJDBCµÄÔçÆڲݰ¸ÌṩÁ˺ܺõÄÒâ¼ûºÍ
½¨Òé¡£ËûÃǵŤ×÷¶Ô±¾¹æ·¶ÆðÁ˲»¿É¹ÀÁ¿µÄ×÷Óá£
2. Ä¿±êÓëÕÜѧ
Õâ¸ö²¿·ÖÃèÊöÁËÖ¸ÒýÕâ¸öAPI¿ª·¢µÄÄ¿±êÒÔ¼°ÕÜѧ¡£
2.1. SQL ¼¶ API
ÎÒÃǵÄÖ÷ҪĿ±êÊÇΪJava¶¨ÒåÒ»¸ö¡°µ÷Óü¶¡±(call-level)µÄSQL
½Ó¿Ú¡£×ÅÒâζ×ÅÎÒÃÇÖ÷ÒªµÄ×¢ÒâÁ¦¼¯ÖÐÔÚÖ´ÐÐÔÔ±¾±¾µÄSQL
Óï¾ä²¢ÇÒÈ¡»Ø½á¹û¡£ÎÒÃÇÔ¤¼Æ¸ß²ãµÄAPIÒ²½«±»¶¨Ò壬ÕâЩ¿É
Äܽ«½¨Á¢ÔÚ»ù²ãµÄ½Ó¿ÚÉÏ¡£
ÕâЩ¸ß²ã½Ó¿Ú°üÀ¨ÏóÖ±½ÓµØ¡¢Í¸Ã÷µØ°Ñ±íÀïÃæµÄÊý¾ÝÓ°Éäµ½
JavaÀàÀïÃ棬ÓÃÓï·¨Ê÷±íʾ¸ü¼ÓͨÓõIJéѯ£¬ÒÔ¼°JavaÄÚǶµÄ
SQLÓï·¨¡£
ÎÒÃÇÏ£Íû´óÁ¿µÄÓ¦Óÿª·¢¹¤¾ß½«Ê¹ÓÃÎÒÃǵÄAPI¡£È»¶øÎÒÃÇÒ²
Ï£Íû³ÌÐòÔ±Äܹ»Ê¹ÓÃÎÒÃǵÄAPI£¬ÓÈÆäÊÇÄ¿Ç°ÕâÑùÔÚJavaÀïû
ÓÐÈκÎÆäËûÊÖ¶Î(Ó¦¸ÃÊÇ˵Êý¾Ý¿â·ÃÎÊÊÖ¶Î)µÄÇé¿öÏ¡£
2.2. ×ñÑSQL
Êý¾Ý¿âϵͳ֧³Ö¸÷ʽ¸÷ÑùµÄSQLÓï·¨ºÍÓïÒ壬ËüÃÇÏ໥֮¼äÔÚ
±È½Ï¸ß¼¶µÄ¹¦ÄÜÀýÈçÍⲿÁ¬½Ó£¬ÄÚǶ¹ý³ÌµÈ·½Ãæ²¢²»Ò»Ö£¬¾¡
¹ÜÎÒÃÇÄܹ»ÅÎÍû×ÅËæʱ¼äµÄÍÆÒÆÕâЩ²¿·ÖµÄSQL¿ÉÒÔ»ñµÃ±ê×¼
»¯¡£Í¬Ê±ÎÒÃDzÉÈ¡ÕâÑùµÄ̬¶ÈÓëÁ¢³¡£º
In fact, an application query need not even be SQL, or it may be a
specialized derivative of SQL, e.g. for document or image queries,
designed for specific DBMSs. In order to pass JDBC compliance tests and to be
called "JDBC
COMPLIANT Ô" we require that a driver support at least ANSI
SQL-2 Entry Level. This gives applications that want wide
portability a guaranteed least common denominator. We believe
ANSI SQL-2 Entry Level is reasonably powerful and is reasonably
widely supported today.
l JDBCÔÊÐí²éѯ±í´ïʽֱ½Ó´«µÝµ½µ×²ãµÄÊý¾ÝÇý¶¯£¬ÕâÑùÒ»
¸ö³ÌÐò¿ÉÒÔ»ñµÃ¾¡Á¿¶àµÄSQL¹¦ÄÜ£¬µ«ÊÇ¿ÉÄܱ»DBMS¾Ü
¾ø¡£ÊÂʵÉÏ£¬Ò»¸ö³ÌÐòµÄ²éѯÉõÖÁ¿ÉÒÔ²»ÊÇSQLµÄ£¬»òÕß
ÊÇSQLµÄÒ»¸öÌØÊâÑÝ»¯£¬ÀýÈ磺ΪרÃÅÊý¾Ý¿âÉè¼ÆµÄÎı¾
»òÕßͼÐβéѯ¡£
l ΪÁËͨ¹ýJDBC¼æÈݵIJâÊÔ£¬²¢ÇÒÄܹ»±»³ÆΪJDBC¼æÈÝ£¬
ÎÒÃÇÒªÇóÒ»¸öÇý¶¯ÖÁÉÙÖ§³ÖANSI SQL-2µÄ±ê×¼¡£ÕâʹµÃÄÇ
ЩÐèÒª¹ã·ºÒÆÖ²ÐԵijÌÐò»ñµÃÒ»¸ö×îСµÄ·Öĸ(Õâ¾ä»°µÄÔ
ÎÄÊÇ£ºThis gives applications that want wide portability a
guaranteed least common denominator.)¡£ÎÒÃÇÏàÐÅANSI
SQL-2ÊÇ×㹻ǿ´óµÄ£¬²¢ÇÒÊǵõ½×ã¹»Ö§³ÖµÄ¡£
2.3. JDBC±ØÐë¿ÉÒÔ½¨Á¢ÔÚÏÖÓеÄÊý¾Ý¿â½Ó¿ÚÉÏ
ÎÒÃDZØÐëÄܹ»±£Ö¤ JDBC SQL API Äܹ»½¨Á¢ÔÚÆÕͨµÄSQL
APIÉÏ£¬ÓÈÆäÊÇODBC¡£ÕâЩҪÇóÒѾ¶ÔÕâ¸ö¹æ·¶µÄһЩ²¿·Ö
²úÉúÁËÓ°Ï죬ÓÈÆäÊǶԴ«³ö²ÎÊý(OUT parameter)ºÍ´óÊý¾Ý¿éµÄ
´¦Àí¡£
2.4. ±ØÐë±£Ö¤Õâ¸ö½Ó¿ÚÓëJAVAϵͳµÄÆäËû²¿·Ö±£³ÖÒ»ÖÂ
Ä¿Ç°¶ÔJAVAµÄ»ý¼«»ØÓ¦ÒѾʮ·ÖÈÈÁÒ¡£ºÜ´ó³Ì¶ÈÉÏÊÇÓÉÓÚÕâ
¸öÓïÑÔ±ê×¼ÒÔ¼°±ê×¼ÔËÐÐʱ¿â±»ÈÏΪÊÇÒ»Ö£¬¼òµ¥ºÍÇ¿´óµÄ¡£
ÎÒÃǽ«¾¡ÎÒÃÇËùÄÜ£¬ÌṩÕâ¸öJavaÊý¾Ý¿â½Ó¿Ú£¬Õâ¸ö½Ó¿Ú½«½¨
Á¢ÔÚJavaÄÚºËÏÖÓеÄÕâÖÖ·ç¸ñ£¬²¢ÇÒ½«½øÒ»²½¼ÓÇ¿Ëü¡£
2.5. ±£³Ö¼òµ¥
We would prefer to keep this base API as simple as possible, at
least initially. In general we would prefer to provide a single
mechanism for performing a particular task, and avoid provid-ing
duplicate mechanisms. We will extend the API later if any
important functionality is miss-ing.
ÎÒÃǽ«Á¦ÕùʹµÃ»ù±¾µÄAPI¾¡Á¿¼òµ¥£¬ÖÁÉÙ¿ªÊ¼µÄʱºòÊÇÕâÑù
µÄ¡£Ò»°ãÀ´Ëµ£¬ÎÒÃÇÏ£Íû¶ÔʵÏÖÿ¸öÌض¨µÄÈÎÎñÖ»ÌṩһÖÖ·½
°¸£¬¶ø±ÜÃâÌṩ¶àÖÖ·½°¸¡£Èç¹ûһЩÖØÒªµÄ¹¦ÄÜÒÅ©ÁË£¬ÄÇô
ÎÒÃÇÔÚÍíЩʱºò½«À©³äÕâ¸öAPI¡£
2.6. ¾¡Á¿±£³ÖÇ¿µÄ¡¢¾²Ì¬µÄÀàÐÍ
ÎÒÃÇÏ£ÍûÕâ¸öJDBC API±£³Ö¾¡Á¿Ç¿µÄÀàÐͼì²é£¬Ê¹µÃ¾¡¿ÉÄÜ
¶àµÄÀàÐÍÐÅÏ¢¿ÉÒÔ¾²Ì¬µØ±í´ï¡£×ÅʹµÃ¾¡¿ÉÄܶàµÄ´íÎó¿ÉÒÔÔÚ
±àÒëµÄʱºò±»·¢ÏÖ¡£
ÓÉÓÚSQL±¾ÉíÊǶ¯Ì¬ÀàÐ͵ģ¬ËùÒÔÎÒÃÇ¿ÉÄÜ»áÔÚ³ÌÐòÔËÐеÄʱ
ºòÓöµ½ÀàÐͲ»ÄÜÆ¥ÅäµÄÎÊÌâ¡£ÀýÈ磺µ±Ò»¸ö³ÌÐòÔ±ÔÚÏ£Íû
SELECT·µ»ØÒ»¸öÕûÊý£¬µ«ÊÇʵ¼Ê·µ»ØµÄÊÇÒ»¸ö×Ö·û´®¡°foo¡±.
µ«ÊÇÎÒÃÇÒÀȻϣÍû³ÌÐòÔ±°ÑËûÃÇËùÏ£ÍûµÄÀàÐÍÔÚ±àÒëµÄʱºò¾Í
Äܹ»±í´ïÇå³þ£¬ÕâÑùÎÒÃÇ¿ÉÒÔ×ö¾¡¿ÉÄܶàµÄ¾²Ì¬¼ì²é¡£ÎÒÃÇÒ²
Ï£ÍûÔÚ±ØÒªµÄʱºòÄܹ»Ö§³Ö¶¯Ì¬ÀàÐͽӿڣ¨¼ûµÚËÄÕ£©
2.7. ʹÆÕͨÈÎÎñ¼ò»¯
ÎÒÃÇÏ£ÍûÆÕͨµÄÈÎÎñÄܹ»ÊǼòµ¥µÄ£¬¶ø²»Ò»°ãµÄ¹¤×÷ÊÇ¿ÉÐÐ
µÄ¡£
Ò»¸öÆÕͨÈÎÎñÊÇÖ¸Ò»¸ö³ÌÐòÔ±Ö´ÐÐÒ»¸ö¼òµ¥µÄûÓвÎÊýµÄSQL
Óï¾ä£¨ÀýÈç:SELECT,INSERT,UPDATE,DELETE£©£¬È»ºó(ÀýÈç
SELECT)´¦Àí·µ»ØµÄ¾ßÓмòµ¥ÀàÐ͵ÄÔª×é¡£Ò»¸ö¾ßÓд«Èë²ÎÊý
£¨IN parameter£©µÄSQLÓï¾äÒ²ÊÇÆÕͨµÄ¡£
²»ÄÇôÆÕͨµ«ÊÇÒ²ÊÇÊ®·ÖÖØÒªµÄÇéÐÎÊǵ±³ÌÐòԱʹÓÃÓÐ
INOUT,OUT²ÎÊýµÄSQLÓï¾ä¡£ÎÒÃÇÒ²ÐèÒªÖ§³Ö¶Áд¼¸Õ××Ö½Ú
¶ÔÏóµÄSQLÓï¾ä£¬¸üÌرðһЩµÄÇéÐΰüÀ¨Ò»¸öÓï¾ä·µ»ØÁ˶à¸ö
½á¹û¼¯ºÏ¡£
ÎÒÃÇÏ£ÍûÔªÊý¾Ý(Meatdata)µÄʹÓúÜÉٵģ¬Ö»ÊÇÄÇЩÊìÁ·µÄ³ÌÐò
Ô±ÒÔ¼°¿ª·¢¹¤¾ß²ÅÐèÒª´¦ÀíµÄÎÊÌâ¡£ÔªÊý¾Ý´æÈ¡º¯ÊýÒÔ¼°¶¯Ì¬
ÀàÐÍÊý¾Ý´æÈ¡º¯ÊýÔÚÕâ¸öÎĵµÄ©Î²£¬Ò»°ãµÄ³ÌÐòÔ±¿ÉÒÔ²»±Ø¹Ø
ÐÄÕâЩÕ½ڡ£
2.8. ²»Í¬µÄ¹¦ÄÜÈò»Í¬µÄ·½·¨£¨º¯Êý£©À´ÊµÏÖ
£¨¡°·½·¨¡±µÄÔÎÄÊÇ£ºmethod£¬ÕâÑù·ÒëÊǸúVBµÄ£©
Ò»ÖÖ½çÃæÉè¼Æ·ç¸ñÊÇʹÓúÜÉٵĹý³Ì£¬ÌṩÐí¶à×÷Ϊ²ÎÊý´«µÝ
µÄ¿ØÖƱêÖ¾£¬ÕâÑùËüÃÇ¿ÉÒÔÓÃÀ´Ó°ÏìºÜ´óÒ»¸ö·¶Î§Äڵĸ÷ÖÖÐÐ
Ϊ¡£À´±í´ï²»Í¬µÄ¹¦ÄÜ¡£ÕâÇ÷ÏòÓëʹÓúܶàµÄ·½·¨£¬µ«ÊÇÿ¸ö
·½·¨¶¼±È½ÏͬÒâÀí½â¡£
Ò»°ãÀ´Ëµ£¬JavaÄÚºËÀàʹÓò»Í¬µÄ·½·¨£¨method£©¡£Õâ¸ö²½
ÖèµÄÖ÷ÒªÓŵãÊÇ¿ªÊ¼Ñ§Ï°»ù±¾½çÃæµÄ³ÌÐòÔ±¿ÉÒÔ²»±Ø±»ÄÇЩÓë
¸´ÔÓ¹¦ÄÜÏà¹ØµÄ²ÎÊýËùÀ§ÈÅ¡£ÎÒÃÇÁ¦Í¼ÔÚJDBC½Ó¿ÚÉÏÒ²²ÉÓÃ
ÏàͬµÄ²ßÂÔ¡£Ò»°ãÀ´Ëµ²ÉÓò»Í¬µÄ·½·¨¶ø²»ÊDzÉÓò»Í¬µÄ±êÖ¾
ºÍ¶àÓÃ;µÄ·½·¨¡£
3. ½Ó¿Ú¸Åò
½Ó¿Ú·ÖΪÁ½¸ö²ã´Î£¬Ò»¸öÊÇÃæÏò³ÌÐò¿ª·¢ÈËÔ±µÄJDBC API¡£
ÁíÍâÒ»¸öÊǵײãµÄJDBC Driver API¡£
3.1. JDBC API
JDBC API ±»ÃèÊö³ÉΪÒÍ×å³éÏóµÄJava½Ó¿Ú£¬ËƵÄÓ¦ÓóÌÐòÔ¶
¿ÉÒÔ¶Ôij¸öÊý¾Ý¿â´ò¿ªÁ¬½Ó£¬Ö´ÐÐSQLÓï¾ä²¢ÇÒ´¦Àí½á¹û¡£
´íÎó! ǶÈë¶ÔÏóÎÞЧ¡£
×îÖØÒªµÄ½Ó¿ÚÊÇ£º
l java.sql.DriverManager ´¦ÀíÇý¶¯µÄµ÷Èë²¢ÇÒ¶Ô²úÉúеÄ
Êý¾Ý¿âÁ¬½ÓÌṩ֧³Ö¡£
l java.sql.Connection ´ú±í¶ÔÌض¨Êý¾Ý¿âµÄÁ¬½Ó¡£
l java.sql.Statement ¡¡´ú±íÒ»¸öÌض¨µÄÈÝÆ÷£¬À´¶ÔÒ»¸öÌض¨µÄ
Êý¾Ý¿âÖ´ÐÐSQLÓï¾ä¡£
l java.sql.ResultSet ¡¡¿ØÖƶÔÒ»¸öÌض¨Óï¾äµÄÐÐÊý¾ÝµÄ´æÈ¡¡£
ÆäÖÐjava.sql.StatementÓÖÓÐÁ½¸ö×ÓÀàÐÍ£º
1. java.sql.PreparedStatement ¡¡ÓÃÓÚÖ´ÐÐÔ¤±àÒëµÄSQL
Óï¾ä¡£
2. java.sql.CallableStatement ¡¡ÓÃÓÚÖ´ÐжÔÒ»¸öÊý¾Ý¿â
ÄÚǶ¹ý³ÌµÄµ÷Óá£
ÏÂÃæµÄÕ½ڶÔJDBCÊÇÈçºÎÔËÐеÄÌṩÁ˸ü¶àÃèÊö£¬Õû¸ö¶¨Òå
¼ûµÚ13Õ¡£ÁíÍâµÚ15ÕÂÃèÊöÁËϵͳÈç¹û»ñÈ¡Êý¾Ý¿âµÄÔªÊý¾Ý
ÐÅÏ¢¡£
3.2. JDBC Driver API
java.sql.DriverÔÚµÚ9ÕÂÓÐÍêÕûµÄ¶¨ÒåÁË.´ó²¿·ÖJDBCÇý¶¯Ö»Ðè
ÒªÍê³ÉÕâЩJDBC APIËù¶¨ÒåµÄ³éÏóÀà¾Í¿ÉÒÔÁË¡£ÌرðµØ£¬ËùÓÐ
µÄdriver±ØÐëÌṩ¶Ôjava.sql.Connection, java.sql. State-ment,
java.sql.Prepared£Statement, and java.sql.ResultSetµÄʵÏÖ¡£Èç¹û
Ä¿±êDBMSÌṩÓÐOUT²ÎÊýµÄÄÚǶ¹ý³Ì£¬ÄÇô»¹±ØÐëÌṩ
java.sql.CallableStatement ½Ó¿Ú¡£
ÿ¸ödatabase driver±ØÐëÌṩһ¸öÀࣺjava.sql.DriverÒÔʹµÃϵͳ
¿ÉÒÔÓÉ java.sql.DriverManagerÀ´¹ÜÀí¡£
Ò»¸öÏÔÈ»µÄdriverÊÇÔÚODBCÖ®ÉÏÌṩ¶ÔJDBCµÄʵÏÖ£¬´Ó¶øÌá
¹©ÓëODBC½Ó¿ÚµÄJDBC-ODBC ÇÅ£¬¾ÍÏóÇ°ÃæµÄͼËùÏÔʾµÄ.ÓÉ
ÓÚJDBC·ÅÔÚODBCÖ®ºó£¬ËùÒÔʵÏÖÆðÀ´¼òµ¥¶øÇÒ¸ßЧ¡£
ÁíÍâÒ»¸öÓÐÓõÄÇý¶¯Ö±½Ó½Ó´¥Êý¾Ý¿âÎ޹صÄÍøÂçÐÒé¡£·¢²¼Ò»
¸öÐÒéÔÊÐí¶à¸ö·þÎñÆ÷ʵÏֵķ½·¨£¬ÀýÈçÔÚODBC»òÕßÌض¨µÄ
DBMSÉÏ£¨¾¡¹ÜÒѾÓÐÁËһЩʹÓù̶¨ÐÒéµÄ²úÆ·£¬µ«ÊÇÎÒÃÇ
²»´òËã¶ÔËüÃÇʵÏÖ±ê×¼»¯¡££©£¬ÊÇ¿ÉÈ¡µÄ¡£
4. JDBCʹÓó¡ºÏ
Before looking at specifics of the JDBC API, an understanding of
typical use scenarios is help-ful. There are two common scenarios
that must be treated differently for our purposes: applets and
applications.
ÔÚ¿´JDBC API֮ǰÁ˽âһϵäÐ͵ÄʹÓó¡ºÏÊÇÓаïÖúµÄ¡£Í¨
³£ÓÐÁ½ÖÖÇéÐαØÐë·Ö±ð¶Ô´ý£ºappletºÍapplication.
4.1. Applet
Ä¿Ç°JavaʹÓõÄ×î¶àµÄ´ÓÍøÂçÖÐÏÂÔصÄapplet£¬ËüÃÇ×÷ΪwebÎÄ
¼þµÄÒ»¸ö²¿·Ö¡£µ±ÖÐÓÐÊý¾Ý¿â´æÈ¡appletºÍÄܹ»Ê¹ÓÃJDBCÀ´½Ó
´¥Êý¾Ý¿âµÄapplet¡£
ÀýÈ磬һ¸öÓû§¿ÉÄÜÏÂÔØÒ»¸öÏÔʾ¹ÉƱÀúÊ·¼Û¸ñͼµÄapplet¡£Õâ
¸öappletͨ¹ýinternetÀ´´Ó¹ØϵÊý¾Ý¿âÖлñµÃ¹ÉƱÀúÊ·¼Û¸ñ¡£
×îÒ»°ãµÄÇé¿öÀïÃ棬¶ÔappletµÄʹÓÃÊÇͨ¹ý²»¿É¿¿µÄ±ß½çµÄ¡£Àý
Èç´ÓÁíÍâÒ»¸ö¹«Ë¾»òÕßInternetÉÏ»ñµÃÕâЩapplet¡£ÓÚÊdzÆÕâ¸öÇé
¿öΪ"Internet"³¡ºÏ¡£È»¶øappletÒ²¿ÉÄÜͨ¹ý¾ÖÓòÍøÏÂÔØ¡£ÔÚÕâ¸ö
Çé¿öÀïÃ棬¿Í»§»úµÄ°²È«¶¼»¹ÊÇÒ»¸öÎÊÌâ¡£
µäÐ͵ÄappletÔÚ¼¸¸ö·½ÃæÓ봫ͳµÄÊý¾Ý¿âÓ¦ÓóÌÐòÓÐËù²»Í¬£º
1. ²»¿É¿¿µÄapplet±»ÑϸñµØÏÞÖÆÔÚËûÃDZ»ÔÊÐíÖ´ÐеĵIJÙ×÷
ÉÏ¡£ÌرðµØ£¬²»ÔÊÐíËûÃÇ´æÈ¡±¾µØµÄÎļþ£¬Çв»ÔÊÐíËûÃǶÔ
ÈÎÒâµÄÊý¾Ý¿â½¨Á¢ÍøÂçÁ¬½Ó¡£
2. ¾Í±êʶºÍÁ¬½ÓÍøÉÏÊý¾Ý¿âÀ´Ëµ£¬Internet»·¾³ÀïÃæµÄappletÃæ
ÁÙеÄÎÊÌâ¡£
3. µ±Êý¾Ý¿â¿ÉÄÜÓëÄãÏà¸ôÍòÀïµÄʱºò£¬Ð§ÂʵĿ¼ÂÇÒ²ÓÐËù²»Í¬
ÁË¡£Óë¾ÖÓòÍøÏà±È£¬InternetÉÏÊý¾Ý¿âapplet¿ÉÄÜ»áÅöµ½Ê®·Ö
²»Í¬µÄ·´Ó¦Ê±¼ä¡£
4.2. Application
JavaÒ²¿ÉÒÔÓÃÀ´½¨Á¢ÆÕͨµÄÓ¦Ó㬴ӶøÏëÒ»°ãµÄÓ¦ÓÃÒ»ÑùÔÚ¿Í
»§»úÉÏʹÓá£ÎÒÃÇÏàÐÅËæ×Å¿ª·¢¹¤¾ßÔ½À´Ô½¶à£¬ÈËÃÇ¿ªÊ¼ÈÏʶ
µ½Ìá¸ß³ÌÐòÉú²úЧÂʵıØÒªÐÔ£¬ÒÔ¼°JavaµÄÆäËûÓŵ㣬JavaµÄ
ÕâÖÖÓ÷¨½«Ô½À´Ô½Á÷ÐС£ÔÚÕâÖÖ·½Ê½ÀïÃ棬JavaµÄ´úÂëÊÇ¿ÉÒÔ
ÐÅÀµµÄ£¬ÇÒ±»ÔÊÐí¶ÁдÎļþ´ò¿ªÍøÂçÁ¬½ÓµÈµÈ£¬¾ÍÏëÆäËûµÄÓ¦
ÓóÌÐò´úÂëÒ»Ñù¡£
Ò²ÐíÕâЩJavaÓ¦ÓÃʹÓõÄ×î¶àµÄÊÇÔÚÒ»¸ö¹«Ë¾ÄÚ²¿»òÕßÔÚ
IntranetÉÏ£¬ËùÒÔ²»·Á³ÉΪIntranet³¡ºÏ¡£ÀýÈçÒ»¸ö¹«Ë¾Ï£ÍûÀûÓÃ
Java¼°ÆäGUI¹¹¼þ¹¤¾ßÀ´½¨Á¢ËûµÄ»ùÓÚºÏ×÷Êý¾ÝģʽµÄºÏ×÷Èí
¼þ¡£ÕâЩӦÓóÌÐò½«´æÈ¡¾ÖÓòÍø»òÕß¹ãÓòÍøµÄÊý¾Ý¡£JavaÓ¦ÓÃ
¿ÉÒÔ×÷µ½ÕâЩ¡£
JavaÓ¦ÓóÌÐò³¡ºÏºÍIntranet³¡ºÏÓëapplet³¡ºÏÓÐÖî¶à²»Í¬¡£ÀýÈç
±ê¶¨Ò»¸öÊý¾Ý¿â×î×ÔÈ»µÄ·½Ê½ÊÇÓÃÒ»¸öÊý¾Ý¿âµÄÃû×Ö£¬¾ÍÏó
"Customers" ºÍ"Personnel"ÕâÑù¡£È»ºóÓû§Ï£ÍûϵͳÄܹ»¶¨Î»¾ß
ÌåµÄ»úÆ÷£¬DBMS£¬JDBC driver£¬ºÍJavaÓ¦ÓóÌÐò¡£
4.3. ÆäËû³¡ºÏ
»¹ÓÐÆäËûһЩÓÐȤµÄ³¡ºÏ£º
1. ÒÑÑéÖ¤µÄapplet£¨Trusted applets£©ÊÇÖ¸ÄÇЩÒѾ±»JavaÐéÄâ
»úÆ÷È϶¨ÊÇ¿ÉÒÔÐÅÀµµÄapplet¡£ËûÃÇÖ®ËùÒÔ±»ÈÏΪÊÇ¿ÉÐŵÄ
ÊÇÒòΪËûÃÇÒѾ¶ÔÉÏÁËÌض¨µÄÃܳף¬»òÕßÓû§ÈÏΪ´ÓÌض¨À´
Ô´À´µÄappletÊÇ¿ÉÐŵġ£ÔÚ°²È«µÄ·½ÃæÉÏËûÃÇÓëÓ¦ÓÃ
£¨appliction£©Ïàͬ£¬µ«ÊÇÆäËû·½Ã棨ÀýÈ綨λһ¸öÊý¾Ý¿â£©
ÓëÔòÓëappletÏàËÆ¡£
2. ÓëÖ±½Ó´ÓJava GUI³ö·¢Óÿͻ§/·þÎñÆ÷ģʽÀ´¶ÈÇúDBMS·þ
ÎñÆ÷²»Í¬£¬Èý²ã´æÈ¡·½Ê½¿ÉÄܱ»Ê¹Óá£ÔÚÕâ¸ö³¡ºÏÀïÃ棬Java
Ó¦ÓóÌÐò¶ÔÖмä²ãµÄ·þÎñ·¢³öµ÷Óã¬Öмä²ãµÄ·þÎñÔÚÍøÉÏ£¬
ËüÓÖÔÙÈ¥µ÷ÓÃÊý¾Ý¿â¡£ÕâЩµ÷ÓÿÉÄÜͨ¹ýRPC (remote
procedure call)»òÕßORB (object request broker )¡£ÔÚÕâÁ½ÖÖ³¡
ºÏÀïÃ棬Öмä²ã×îºÃʹÓÃÒ»¸ö¶ÔÏó±ä»¯¡£ÎÒÃÇÏ£ÍûÈý²ã½á¹¹
»á±äµÃÔ½À´Ô½Æձ飬ÒòΪ¶ÔÓÚMIS¹ÜÀíÕßÀ´Ëµ£¬Õâ¿ÉÒÔʹµÃ
ËûÃÇÓлú»áÔÚ¹«¹²Êý¾Ý¿âÉÏÏÔʽµØ¶¨ÒåºÏ·¨²Ù×÷µÈ¡£Í¬Ê±Èý
²ã½á¹¹¿ÉÒÔÌṩÐí¶àЧÂÊÉϵĺô¦¡£
Ä¿Ç°Öмä²ãÒ»°ãÓÃC»òÕßC ÕâÑùµÄÓïÑÔÀ´Íê³É¡£Í¨¹ýÓÅ»¯±àÒë
Æ÷°Ñ°ÑJava ×Ö½Ú´úÂë·Òë³ÉΪ¸ßЧµÄ»úÆ÷´úÂ룬Öмä²ãÒ²
¿ÉÒÔÓÃJavaÀ´ÊµÏÖ¡£JavaÓÐÐí¶àÓÅÁ¼ÌØÐÔ£¨½¡×³ÐÔ£¬°²È«ÐÔ£¬
¶àỊ̈߳©¿ÉÒÔ´ïµ½Öмä²ãÐèÒª´ïµ½µÄÄ¿µÄ¡£
5. °²È«ÐÔ¿¼ÂÇ
×÷ΪÍøÂçÉϵÄÓïÑÔJAVA±ØÐëÊ®·Ö×¢°²È«ÐԵĿ¼ÂÇ¡£»ùÓÚÉÏÃæ
µÄÌÖÂÛ£¬JDBCµÄÁ½ÖÖÖ÷ҪʹÓó¡ºÏÀïÃ棬ÎÒÃDZØÐ뿼ÂÇ°²È«ÐÔ
ÎÊÌ⣺
l ÔÚJava applicationsµÄ³¡ºÏÀïÃæJava´úÂëÊDZ¾µØµÄ£¬ËùÒÔÒ²ÊÇ
"trusted"
l ûÓÐÑéÖ¤µÄJava applet´úÂë²»¿ÉÒÔ´æÈ¡±¾µØµÄÒÔ¼°ÆäËûÍøÂç
µÄÊý¾Ý¡£
5.1. JDBC ºÍδÑéÖ¤µÄapplet
JDBCÊ×ÏȱØÐë·ûºÏJAVAµÄÒ»°ã°²È«¹æÔò¡£ÁíÍ⣺
l JDBC ±ØÐëÈÏΪûÓÐÑéÖ¤µÄappletsÊDz»¿É¿¿µÄ¡£
l JDBC ²»¿ÉÒÔÈò»¿É¿¿µÄapplets´æÈ¡±¾µØÊý¾Ý¿â¡£
l Ò»¸öÒѾÏòJDBC DriverManager×¢²áµÄÊÇJDBC DriverÖ»ÄÜ
´æÈ¡ËüËùÀ´µÄÊý¾ÝÔ´¡£
l Ò»¸öappletÒ²Ö»ÄÜÏòËüËùDownloadÀ´µÄ·þÎñÆ÷À´´æÈ¡Êý¾Ý¡£
Èç¹ûJDBCÇý¶¯²ãÈç¹ûÍêÈ«È·ÐŶÔÒ»¸öÊý¾Ý¿â·þÎñÆ÷´ò¿ªÁ¬½Ó
²»»áÒýÆðÈÏÖ¤»òÕßȨÏÞÎÊÌ⣨¿ÉÄÜÓÉÍøÉÏËæ»úÖ÷»úÉÏÔËÐеijÌ
ÐòÒýÆ𣩣¬ÄÇôËü¾ÍÔÊÐíapplet´ò¿ªÕâÑùµÄÁ¬½Ó¡£Êý¾Ý¿â·þÎñÆ÷
²»Í¨¹ýIPµØÖ·À´ÏÞÖÆ´æÈ¡ÊÇÏ൱Éٵģ¬Ö÷ÒªÊÇΪÁ˾ÙÀý¡££¨µ±
ÐÄ£¬ÕâÒ»¶Î»°ÎÒ¿ÉÄÜ·Òë·´ÁË£¡£¡£¡´ó¼Ò¿´¿´ÔÎÄ¡££©
ÕâЩÏÞÖÆÊÇÏ൱·³ËöµÄ¡£²»¹ýËûÃÇÓë¶ÔÒ»°ãappletµÄÏÞÖÆÊÇÒ»
ÖµÄÎÒÃÇûÓбØÒª·Å¿ªÕâЩÏÞÖÆ¡£
5.2. JDBC ºÍJavaÓ¦ÓóÌÐò
¶ÔÓÚÒ»¸öÆÕͨµÄJavaÓ¦ÓóÌÐò£¨ÀýÈçÈ«²¿ÓÃJava´úÂë¶ø²»ÊDz»
¿É¿¿µÄapplet £©JDBC½«´Ó±¾µØµÄÀà·¾¶ÀïÃæ»ñµÃÇý¶¯£¬²¢ÇÒÔÊ
ÐíÓ¦ÓóÌÐò×ÔÓÉ´æÈ¡Îļþ£¬Ô¶³Ì·þÎñÆ÷µÈµÈ¡£
µ«ÊǺÍappletÒ»Ñù£¬Èç¹ûÓÉÓÚijЩÔÒòÒ»¸öûÓÐÑéÖ¤µÄ
sun.sql.DriverÀà´ÓÔ¶³ÌµÄÀ´Ô´ÀïÃæ»ñµÃ£¬ÄÇôÕâ¸öÇý¶¯Ö»ÄܺÍ
ÏàͬµØ·½À´µÄ´úÂëÅäºÏ¡£
5.3. DriverµÄ°²È«ÔðÈÎ
JDBC driver¿ÉÄÜÔÚ¸÷ÖÖÇé¿öÏÂʹÓã¬ËùÒÔÇý¶¯µÄ±àÖÆÕß×ñÑÒ»
¶¨µÄ¼òµ¥µÄ°²È«¹æÔò£¬´Ó¶ø±ÜÃâapplet×ö·Ç·¨µÄÊý¾Ý¿âÁ¬½Ó¡£
Èç¹ûËùÓеÄÇý¶¯¶¼ÏóappletÒ»Ñù´ÓÍøÉÏÏÂÔØ£¬ÄÇôÕâЩÔÔò½«ÊÇ
²»±ØÒªµÄ£¬ÒòΪÆÕͨµÄ°²È«¹æÔòÒѾ¶ÔËü×öÁËÏÞÖÆ¡£µ«ÊÇÇý¶¯
µÄ±àдÕß±ØÐë¼Çסһµ©ËûÃǵÄÇý¶¯»ñµÃ³É¹¦£¬Óû§½«ÔÚ±¾µØ´Å
ÅÌ°²×°ÕâЩÇý¶¯£¬ÄÇôÇý¶¯½«³ÉΪJava»·¾³ÖÐÒ»¸ö±»ÐÅÈεIJ¿
·Ö£¬ËùÒÔ±ØÐëÈ·ÐÅËü²»»á±»À´·ÃµÄappletËùÀÄÓá£ËùÒÔÎÒÃǹÄÀø
ËùÓеÄÇý¶¯±àдÕß±ØÐë×ñÑÒ»¶¨°²È«ÔÔò¡£
ËùÓÐÕâЩÔÔò¶¼ÊÇÔÚÁ¬½Ó´ò¿ªµÄʱºòʹÓá£ÕâÕýʽÇý¶¯ºÍÐéÄâ
»úÆ÷¼ì²éµ±Ç°µ÷ÓÃÕßÊÇ·ñÕæµÄ¿ÉÒÔÓëÖ¸¶¨µÄÊý¾Ý¿âÁ¬½ÓµÄʱ
¿Ì¡£Ò»µ©Á¬½Ó½¨Á¢¾Í²»±Ø×ö¸ü¶àµÄ¼ì²éÁË¡£
5.3.1. ·ÖÏíTCP/IPÁ¬½ÓµÄʱºò±ØÐë½÷É÷
Èç¹ûÒ»¸öJDBCÇý¶¯ÊÔͼ´ò¿ªÒ»¸ö TCP Á¬½Ó£¬ÄÇôÕâ¸ö´ò¿ª»á
±»Java °²È«¹ÜÀí»úÖÆ×Ô¶¯¼ì²é¡£Õâ¸ö»ú¹¹»á¼ì²éµ±Ç°µ÷ÓÃÕ»Àï
ÃæÓÐûÓÐapplet£¬Èç¹ûÓÐÄÇô¾ÍÏÞ¶¨Ëü¿ÉÒÔ·ÃÎʵĻúÆ÷¼¯ºÏ¡£Ëù
ÒÔÒ»°ãµØJDBCÇý¶¯¿ÉÒÔ°ÑTCP½¨Á¢¼ì²éÁô¸øJavaÐéÄâ»ú¡£
µ«ÊÇÈç¹ûÒ»¸öJDBCÇý¶¯ÊÔͼÔÚ¶à¸öÊý¾Ý¿âÁ¬½ÓÖ®¼ä¹²ÏíÒ»¸ö
TCPÁ¬½Ó£¬ÄÇôÇý¶¯¾Í±ØÐë×Ô¼º¸ºÔð¼ì²éÿ¸öµ÷ÓÃÕßÊÇ·ñÕæµÄ
±»ÔÊÐíÓëÄ¿±êÊý¾Ý¿âÁªÏµ¡£ÀýÈçÈç¹ûÎÒÃÇΪapplet A´ò¿ªÁËÒ»¸ö
ͨÍù»úÆ÷foobah µÄTCPÁ¬½Ó£¬Õâ²¢²»Òâζ×Åapplet B±»×Ô¶¯ÔÊÐí
À´¹²ÏíÕâ¸öÁ¬½Ó¡£applet B¿ÉÄÜûÓÐÈκηÃÎÊ»úÆ÷foobahµÄȨ
Á¦¡£ËùÒÔÔÚÔÊÐíij¸ö³ÌÐòÖØÓÃÒ»¸öÏֳɵÄTCPÁ¬½Ó֮ǰ£¬JDBC
Çý¶¯±ØÐëͨ¹ý°²È«»ú¹¹À´¼ì²éµ±Ç°µÄµÄµ÷ÓÃÕßÊÇ·ñ¿ÉÒÔ·ÃÎÊÕâ
¸öÁ¬½Ó¡£Í¨¹ýÏÂÃæµÄ´úÂë¿ÉÊÇʵÏÖÕâ¸ö¹¦ÄÜ¡£
SecurityManager security = System.getSecurityManager();
if (security != null)
{
security.checkConnect(hostName, portNumber);
}
Èç¹ûÁ¬½ÓÊDz»ÔÊÐíµÄ£¬ÄÇôSecurity.checkConnect·½·¨½«²úÉúÒ»
¸öjava.lang.SecurityException¡£
5.3.2. ¼ì²éËùÓеı¾µØÎļþ·ÃÎÊ
Èç¹ûÒ»¸öJDBCÈ¡µÃÐèÒª·ÃÎʱ¾µØ»úÆ÷ÉϵÄÊý¾Ý£¬ÄÇôËû±ØÐëÈ·
Ðŵ÷ÓÃÕßÊDZ»ÔÊÐí´ò¿ªÕâ¸öÎļþµÄ¡£ÀýÈ磺
SecurityManager security = System.getSecurityManager();
if (security != null)
{
security.checkRead(fileName);
}
Èç¹û¶ÔÌض¨ÎļþµÄ·ÃÎÊÊDz»ÔÊÐíµÄ£¬ÄÇôSecurity.checkRead·½
·¨½«²úÉúÒ»¸öjava.lang.SecurityException¡£
5.3.3. ×÷ºÃ×µÄ×¼±¸
һЩÇý¶¯¿ÉÄÜʹÓñ¾µØµÄ·½·¨À´ÇŽӵײãÊý¾Ý¿â³ÌÐò¡£ÔòÕâЩ
Çé¿öÀïÃæÅжÏÄÇЩ±¾µØÎļþ½«±»µ×²ãº¯ÊýËù·ÃÎÊÊÇÀ§Äѵġ£
ÔÚÕâЩ»·¾³ÀïÃæÓû§±ØÐë×÷ºÃ×µÄ´òË㣬²¢ÇÒ·ñ¾öËùÓÐÏÂÔØ
appletËù·¢³öµÄÊý¾Ý¿â´æÈ¡£¬³ý·ÇÇý¶¯¿ÉÄÜÍêÈ«È·ÐŽ«Òª×ö´æÈ¡
ÊÇûÓÐÎÊÌâµÄ¡£
ÀýÈçÒ»¸öJDBC-ODBCÇŽÓÆ÷±ØÐë¼ì²éODBCÊý¾ÝÔ´µÄµÄÃû
³Æ£¬È·±£appletÖ»¿ÉÒÔ·ÃÎÊËüµÄ¡°ÉúÔ´µØ¡±¡£Èç¹û¶ÔÓеÄÃû×ÖÖÐ
²»ÄÜÅжϳöÊý¾ÝÔ´µÄÖ÷»úÃû£¬ÄÇôֻÄÜ·ñ¾öÕâ¸ö·ÃÎÊ¡£
ΪÁ˾ö¶¨Ò»¸öµ±Ç°µÄµ÷ÓÃÕßÊÇ¿ÉÒÔÐÅÀµµÄÓ¦Óû¹ÊÇÒ»¸ö
applet£¬JDBCÇý¶¯±ØÐëÄܹ»¼ì²éÕâ¸öµ÷ÓÃÕßÊÇ·ñ¿ÉÒÔдһ¸öËæ
»úµÄÎļþ£º
SecurityManager security = System.getSecurityManager();
if (security != null)
{
security.checkWrite("foobaz");
I. }
---------------------------------------------------------------
P.S ±¾ÎÄÈ¡×ÔCZF£¬Ôڴ˱íʾ¸Ðл¡£
°æȨ¹éCZFËùÓС£
--
¡ù À´Ô´:¡¤¹þ¶û±õ×϶¡ÏãÕ¾ bbs1.hit.edu.cn¡¤[FROM: lupy.bbs@melon.gznet]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
Ò³ÃæÖ´ÐÐʱ¼ä£º207.074ºÁÃë