UMLÈí¼þ¹¤³Ì×éÖ¯
±±¾©»ðÁú¹ûÈí¼þ¹¤³Ì¼¼ÊõÖÐÐÄ

ÔÚ Oracle JDBC ·ÃÎÊÖмÓÈëһЩ Spring ÌØÐÔ

 

×÷ÕߣºDustin Marx À´Ô´£ºÍøÂç

 

JDBC ÊÇÒ»¸ö³£ÓÃÓÚ·ÃÎʹØϵÊý¾Ý¿âµÄ±ê×¼Êý¾Ý·ÃÎÊЭÒé¡£JDBC µÄÒ»¸öÏÔÖøÓÅÊÆÊÇÆä±ê×¼»¯µÄ API£¬Îª»ùÓÚ Java µÄÊý¾Ý·ÃÎÊÓ¦ÓóÌÐòµÄ¿ÉÒÆÖ²ÐԵ춨ÁË»ù´¡¡£JDBC ÊDZê×¼ Java (J2SE) ºÍÆóÒµ Java (J2EE) ÖÐÒ»¸ö²»¿É»òȱµÄ²¿·Ö£¬ÔÚ Java ÔçÆڽ׶ξÍÒÑÍƳö¡£

JDBC ÓÐÐí¶àÓÅÊÆ£¬Ê¹µÃËüÄܹ»ÔÚÐí¶à J2SE ºÍ J2EE Ó¦ÓóÌÐòÖз¢»ÓÖØÒª×÷Óᣵ«ËüÒ²ÓÐһЩ²»×ãÖ®´¦£¬Ê¹µÃÎÒÃDz»ÄܳÆÐÄÈçÒâµÄʹÓÃËüÃÇ¡£ÕâЩÂé·³£¨ÓÐʱºòÈÃÈËÑá¶ñ£©µÄ JDBC ÌØÐÔ´ßÉú³öÁËÐí¶à¹«¿ªµÄ JDBC ³éÏó¿ò¼Ü£¨ÀýÈç SQLExecutor ºÍ Apache Jakarta Commons DBUtil£©ÒÔ¼°¸ü¶àµÃ¶àµÄ×ÔÖ÷¿ª·¢µÄ JDBC Ó¦ÓóÌÐò¿ò¼Ü¡£Spring ¿ò¼ÜµÄ JDBC ³éÏó¾ÍÊÇÒ»¸ö¹«¿ªµÄ JDBC ³éÏó¿ò¼Ü¡£

Spring ¿ò¼ÜÊÇÒ»¸öÔÚ Apache Ðí¿ÉÏ·¢²¼µÄ Java/J2EE Ó¦ÓóÌÐò¿ò¼Ü£¬ËüÖ§³Ö J2EE Ó¦ÓóÌÐòÖеĶà¸ö²ã´Î¡£Spring ¿ò¼ÜµÄÒ»¸öÍ»³öÌØÐÔÊÇÖ§³Ö¸üÒ×ÓÚά»¤ºÍ¸üÇ¿½¡µÄ JDBC Êý¾Ý·ÃÎÊ¡£ÔÚ±¾ÎÄÖУ¬Äú½«Á˽⵽ Spring ¿ò¼Ü ¡ª Ëü¿ÉÒÔºÍ Oracle TopLink ¶ÔÏó/¹ØϵӳÉ乤¾ß½áºÏʹÓà ¡ª ÈçºÎ´ó´ó¼õÉÙÓë±àд JDBC ´úÂëÏà¹ØµÄ·³Ëö¹¤×÷ºÍ·çÏÕ¡£Ê¹Óà Spring ¿ò¼Ü£¬¿ª·¢ÈËÔ±±àдµÄ Oracle Êý¾Ý¿â·ÃÎÊ JDBC ´úÂë¿ÉÒÔ¸üΪ¼ò½à¡¢¸ü²»Ò׳ö´íÒÔ¼°¸ü¼ÓÁé»î¡£

ÕýÈ·¹Ø±ÕÊý¾Ý¿â×ÊÔ´

JDBC ´úÂëÖеÄÒ»¸ö³£¼û´íÎóÊÇûÓÐÕýÈ·¹Ø±ÕÁ¬½Ó¡£Õ⽫µ¼ÖÂÊý¾Ý¿â×ÊÔ´µÄ²»ºÏÀí·ÖÅä¡£ÀàËƵأ¬¹Ø±Õ½á¹û¼¯ºÍÓï¾äÒ²ÊÇÓÐÓò¢Í¨³£ÍƼöµÄ²Ù×÷¡£ÎªÁËÈ·±£¼´Ê¹ÔÚÒì³£µÄÔËÐÐÌõ¼þÏÂÒ²ÄÜÕýÈ·Ö´ÐÐÕâЩ¹Ø±Õ²Ù×÷£¬Ò»°ã½«²ÉÓôúÂëÇåµ¥ 1 ÖÐ finally ×Ó¾äÖеĴúÂë¡£

¡¡´úÂëÇåµ¥ 1

try
{
// JDBC Connection/Statement/Result Set
}
catch (SQLException sqlEx)
{
// Handle the exception
}
finally
{
try
{ // Closing connection *should* close statement and result set
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
catch (SQLException sqlEx)
{
System.err.println("SQLException NOT handled");
}
}

finally ×Ó¾äͨ³£±»ÓÃÀ´È·±£¹Ø±ÕÊý¾Ý¿âÁ¬½ÓºÍÓï¾ä¡£µ«¼´Ê¹µ±¿ª·¢ÈËÔ±µÄÈ·ÓÃÕâÖÖ·½·¨È·±£³É¹¦¹Ø±ÕÁ¬½Ó£¬´úÂëÒ²ÊÇÈß³¤¡¢ÅòÕͺÍÖظ´µÄ¡£Spring ¿ò¼Ü¶ÔÁ¬½Ó´¦ÀíºÍÏà¹Ø×ÊÔ´¹ÜÀí½øÐÐÁ˳éÏ󣬿ª·¢ÈËÔ±²»ÓÃÖ±½Ó´¦ÀíÉÏÊöÊÂÏ´Ó¶øʵÏÖ¸üÒ»ÖµÄ×ÊÔ´¹Ø±Õ²¢±àд¸üÒ×ÓÚÀí½âµÄ´úÂë¡£

µÚÒ»¸ö Spring ´úÂëʾÀý

´úÂëÇåµ¥ 2 ÖÐµÄ JDBC ´úÂë¿ÉÒÔÓÃÀ´²éѯ£¨´ó¼Ò¶¼ÊìϤ£©µÄ scott/tiger ģʽÖеÄÔ±¹¤µÄ³ê½ð¡£ÕýÈç֮ǰËùÌÖÂÛµÄÄÇÑù£¬ÔÚ±¾Ê¾ÀýÖгýÁËʵ¼Ê²éѯÊý¾Ý¿âµÄ SQL ´úÂëÖ®Í⣬»¹±ØÐèÒªÓдóÁ¿µÄ¡°ÀýÐС±´úÂë¡£

¡¡´úÂëÇåµ¥ 2

List commissions = new ArrayList();
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = this.myConnection.createStatement();
rs = stmt.executeQuery("SELECT comm FROM emp");
while ( rs.next() )
{
Integer commission = new Integer( rs.getInt("COMM") );
if ( rs.wasNull() )
{
// By assigning the commission to null, this effectively
// represents a null in the database as a Java null.
System.out.println( "\tCommission seen as " + commission +
" is really null");
commission = null;
}
commissions.add( commission );
}
}
catch (SQLException sqlEx) // checked
{
System.err.println( "Message:" + sqlEx.getMessage() );
System.err.println( "Error Code:" + sqlEx.getErrorCode() );
System.err.println( "SQL State:" + sqlEx.getSQLState() );
}
finally
{
try
{
if ( rs != null ) { rs.close(); }
if ( stmt != null ) { stmt.close(); }
}
catch (SQLException sqlEx) // checked
{
System.err.println( sqlEx.getMessage() );
}
}

´úÂëÇåµ¥ 3 ÖÐΪʹÓà Spring ¿ò¼ÜµÄ´úÂ룬ËüÌṩÁËÀàËÆÓÚ´úÂëÇåµ¥ 2 µÄ¹¦ÄÜ¡£

¡¡´úÂëÇåµ¥ 3

List commissions = new ArrayList();
try
{
JdbcTemplate jt = new JdbcTemplate(this.myDataSource);
List commList = jt.queryForList( "SELECT comm FROM emp");

Iterator commIter = commList.iterator();
while ( commIter.hasNext() )
{
Number comm = (Number) ((Map) commIter.next()).get("COMM");
if (comm != null)
commissions.add( new Integer(comm.intValue()) );
else commissions.add( null ); }
}
catch ( DataAccessException ex ) // unchecked exception
{
System.err.println( ex.getMessage() );
}

ÖµµÃ×¢ÒâµÄÊÇÓëÖ±½ÓʹÓà JDBC Ïà±È£¬ÀûÓà Spring ¿ò¼Ü¿ÉÒÔÉٵöàµÄ´úÂëʵÏÖͬÑùµÄ¹¦ÄÜ¡£Èç´úÂëÇåµ¥ 3 Ëùʾ£¬Äú²»ÐèÒª±àдºÍά»¤¹ÜÀí×ÊÔ´£¨Á¬½Ó¡¢Óï¾ä¡¢½á¹û¼¯£©µÄ´úÂë¡£ÉõÖÁ´úÂëÇåµ¥ 3 ÖеÄÉÙÁ¿µÄÒì³£´¦Àí´úÂëÒ²²»ÊǾø¶Ô±ØÐèµÄ£¬ÒòΪ DataAccessException ÊÇÒ»¸ö·ÇÇ¿ÖÆÒì³£¡£ÒòΪ Number ÀàÐÍÓÃÀ´·µ»Ø½±½ð£¬Òò´Ë²»ÐèÒªÏÔʽµ÷Óà ResultSet µÄ wasNull ·½·¨¡£Êµ¼ÊÉÏ£¬ÄúÉõÖÁÔÚ´úÂëÇåµ¥ 3 ÖеÄÈκεط½¶¼ÕÒ²»µ½ ResultSet Óï·¨£¡

´úÂëÇåµ¥ 3 »¹ËµÃ÷ÁËÓÉ Spring ¿ò¼ÜµÄ JDBC Ö§³ÖËùÌṩºÍʹÓõĻù´¡ÀàÖ®Ò» ¡ª JdbcTemplate¡£ÎÒÃǽ«Ê¹ÓÃÒ»¸öÊý¾ÝÔ´À´Íê³ÉÕâ¸öÓÉ Spring ÌṩµÄÀàµÄʵÀý»¯£¬È»ºóÔÚÄ£°åÀàÉÏʹÓÃÌṩµÄ SQL ×Ö·û´®µ÷ÓÃËüµÄ±»¸²¸ÇµÄ queryForList ·½·¨Ö®Ò»¡£queryForList ·½·¨½«·µ»ØÒ»¸ö°üº¬ HashMap µÄ ArrayList£¬ÆäÖиà ArrayList ÖеÄÿһ¸öÔªËض¼ÊÇÒ»¸ö·µ»ØµÄÊý¾ÝÐУ¬Ò»¸öÌض¨Êý×éÕóÁÐÔªËØÖеÄÿһ¸ö Map ÌõÄ¿¶¼ÊǸÃÐÐÖеÄÒ»¸öÁÐÖµ¡£

JdbcTemplate ÌṩÁËÐí¶à±»¸²¸ÇµÄ queryForList ·½·¨£¬ËüÃÇ¿ÉÒÔÓÃÀ´²éѯDZÔڵĶàÐÐÊý¾Ý¡£Õâ¸ö·Ç³£ÓÐÓõÄÀ໹ÌṩÁËÖîÈç queryForInt£¨·µ»Øµ¥¸öÕûÊý£©¡¢queryForLong£¨·µ»Øµ¥¸ö long ÐÍÕûÊý£©¡¢query¡¢update Ö®ÀàµÄ·½·¨¡£Òª·Ö±æÕâЩ²»Í¬µÄ±»¸²¸ÇµÄ·½·¨£¬×îÈÝÒ׵ķ½Ê½ÊÇÔĶÁÓë Spring ¿ò¼ÜÒ»ÆðÌṩµÄ»ùÓÚ Javadoc µÄ API ÎĵµÖеġ°·½·¨ÏêÇ顱²¿·Ö¡£ÕâЩ·½·¨µÄ²»Í¬µãÔÚÓÚʹÓõÄÓï¾äµÄÀàÐÍ£¨ÀýÈç Statement »ò PreparedStatement£©ºÍÖ§³ÖµÄÌØÐÔ¡£JdbcTemplate »¹ÌṩÁËһЩ·½·¨£¬ÓëÉÏÃæʹÓõķ½·¨Ïà±È£¬ËüÃÇÐèÒª¸ü¶àµÄ JDBC ֪ʶ£¬µ«ËüÃÇÌṩÁ˸üºÃµÄÁé»îÐÔ¡£ÕâЩ¸üÁé»îµ«ÐèÒª¸ü¶à JDBC ֪ʶµÄ·½·¨½«ÔÚ±¾ÎÄÉÔºó½øÐÐ˵Ã÷¡£

JDBC Òì³£´¦Àí

·µ»Øµ½´úÂëÇåµ¥ 1£¬×¢Òâ java.sql.SQLException ÊÇΨһһ¸öÏÔʽ²¶»ñµÄÒì³£¡£SQLException ÖⶻñÁËÓëÊý¾Ý¿âºÍ SQL Ïà¹ØµÄ¸÷ÖÖÒì³£Çé¿ö¡£ÃèÊö SQLException ÀàµÄ Javadoc ×¢ÊͽéÉÜÁË¿ÉÒÔ´Ó SQLException µÄʵÀýÖлñµÃµÄ»ù±¾ÐÅÏ¢¡£ÕâЩÐÅÏ¢°üÀ¨´íÎóÃèÊö×Ö·û´® [getMessage()]¡¢Ä³¸ö±ê×¼»¯ SQLState Òì³£ String [getSQLState()] ºÍ¹©Ó¦ÉÌÌØÓеÄÕûÐÍ´íÎóÂë [getErrorCode()]¡£ÔÚ´úÂëÇåµ¥ 1 ÖÐʵÏֵļòµ¥µÄÒì³£´¦ÀíÖÐʹÓÃÁËËùÓÐÕâÈýÖÖÐÅÏ¢¡£

SQLException ÊÇÒ»ÖÖÇ¿ÖÆÒì³££¨Ö±½ÓÀ©Õ¹ java.lang.Exception£©¡£Java µÄÇ¿ÖÆÒì³£Ôø¾­ÒýÆðºÜ´óÕùÒ飬ÏÖÔÚ Java ÉçÇøËƺõÕýÔÚÈ¡µÃ¹²Ê¶£ºÖ»Óе±ÔÚÓ¦ÓóÌÐòÄܹ»´¦ÀíÒ쳣ʱ²ÅӦʹÓÃÇ¿ÖÆÒì³£¡£ÈçÓ¦ÓóÌÐò´úÂë²»ÄÜÒÔÓÐÒâÒåµÄ·½Ê½´¦ÀíÒì³££¬Ôò²»Ó¦µ±Ç¿ÖÆ´¦Àí¸ÃÒì³£¡£ÒòΪ SQLException ÊÇÇ¿ÖÆÒì³££¬ËùÒÔÓ¦ÓóÌÐò´úÂë±ØÐë´¦ÀíËü£¬»òÕß²¶»ñËü²¢¶ÔÆä½øÐÐһЩ´¦Àí»òÏÔʽµØ½«ÆäÅ׳ö¸øµ÷ÓôúÂë¡£

SQLException µÄ×îºóÒ»µãϸ΢²î±ðÔÚÓÚËüÊÇʹÓà SQL µÄ¹ØϵÊý¾ÝÔ´ËùÌØÓеÄÒì³£¡£ÕâʹµÃ²»ÊʺϽ«Ëü°üº¬ÔÚÕæÕý¿ÉÒÆÖ²µÄÊý¾Ý·ÃÎʶÔÏó (DAO) ÖУ¬ºóÕßÓ¦µ±¶ÀÁ¢ÓÚÊý¾ÝÐÅÏ¢¿âÀàÐͺͷÃÎÊÓïÑÔ¡£

Spring ¿ò¼Ü¶Ô SQLException µÄ´¦ÀíÊÇÆäÔÚÖ§³Ö¸üÈÝÒ×µÄ JDBC ¿ª·¢ºÍά»¤·½Ãæ×îÓÐÓõÄÌØÐÔÖ®Ò»¡£Spring ¿ò¼ÜÌṩÁËÍê³É SQLException ³éÏó»¯µÄ JDBC Ö§³Ö£¬²¢ÌṩÁËÒ»¸ö¶Ô DAO ÓѺõķǼì²éÒì³£²ã´Î½á¹¹¡£

ͼ 1 (ÀûÓà Oracle JDeveloper 10g µÄ UML ½¨Ä£¹¤¾ß»æÖÆ£©Í¼Ê¾ÁËһЩÓÃÓÚ JDBC ºÍ DAO µÄ×îÓÐȤºÍ×îÖØÒªµÄ Spring ¿ò¼ÜÒì³£Àà¡£ÏÂÃæµÄͼ 1 ÖÐÏÔʾµÄËùÓÐÀ඼ÊôÓÚ org.springframework ³ÌÐò°üϵÄÒ»¸ö×Ó³ÌÐò°ü¡£ÕâЩ JDBC ÌØÓеÄÒì³£´¦ÀíÀ඼λÓÚ jdbc ×Ó³ÌÐò°üÖУ¬¸üͨÓÃµÄ DAO Òì³£´¦ÀíÀà°üº¬ÔÚ dao ×Ó³ÌÐò°üÖС£

¡¡´¦Àí¹©Ó¦ÉÌÌØÓеĴíÎóÂë

ÈçÉÏËùÊö£¬±ê×¼µÄ SQLException ÌṩÁËÒ»¸ö±ê×¼»¯µÄÐÅÏ¢¶Î (SQLState) ºÍÒ»¸ö¹©Ó¦ÉÌÌØÓеÄÐÅÏ¢¶Î (ErrorCode)¡£ÕýÈç´ó¶àÊýµÄÊý¾Ý¿âºÍËüÃÇµÄ JDBC Çý¶¯³ÌÐòʵÏÖÒ»Ñù£¬Oracle Êý¾Ý¿âºÍ JDBC Çý¶¯³ÌÐòͨ¹ý¹©Ó¦ÉÌÌØÓеĴíÎóÂëËùÌṩµÄ¹ØÓÚÎÊÌâµÄÏêϸÐÅÏ¢Òª±Èͨ¹ý SQLException µÄÓ빩ӦÉÌÎÞ¹ØµÄ SQLState ×é¼þËùÌṩµÄÐÅÏ¢¶àµÃ¶à¡£

Oracle Êý¾Ý¿â¼°Æä JDBC Çý¶¯³ÌÐòͨ¹ý Error Code ÌṩµÄ¸ü·á¸»µÃ¶àµÄÏêϸÐÅÏ¢µÄÒ»¸öÃ÷ÏÔµÄÀý×ÓÊÇ SQLState ´úÂë 42000 £¨Í¨³£ÕâָʾÓï·¨´íÎó»ò·ÃÎÊÎÊÌ⣩¡£¶ÔÓÚ Oracle JDBC Çý¶¯³ÌÐòµÄ´óÁ¿²»Í¬µÄ Oracle ´íÎóÂ룬SQLException ¶¼½«·µ»ØÏàͬµÄ SQLState Öµ (42000)¡£¶ÔÓ¦ SQLState µÄ 42000 ÖµµÄһЩ Oracle ´íÎóÂë°üÀ¨ 900£¨¡°ÎÞЧ SQL Óï¾ä¡±£©¡¢903£¨¡°ÎÞЧ±íÃû¡±£©¡¢904£¨¡°ÎÞЧ±êʶ·û¡±£©¡¢911£¨¡°ÎÞЧ×Ö·û¡±£©ºÍ 936£¨¡°È±ÉÙ±í´ïʽ¡±£©¡£´ËÍâºÜÃ÷ÏÔÈκÎÀ´Ô´ÓÚ Oracle JDBC Çý¶¯³ÌÐòµÄ´íÎó£¨ÓëÀ´Ô´ÓÚ Oracle Êý¾Ý¿âµÄ´íÎóÏà·´£©¶¼Ã»ÓÐÈκζÔÓ¦µÄ SQLState¡£ÕýÈçÕâЩÀý×ÓËùÏÔʾÄÇÑù£¬Oracle ÌØÓеĴíÎóÂëËùÌṩµÄ¹ØÓÚ´íÎóÇé¿öµÄÏêϸÐÅÏ¢Òª±ÈÓ빩ӦÉÌÎÞ¹ØµÄ SQLState ËùÌṩµÄÐÅÏ¢¸ü·á¸»µÃ¶à¡£

ÓÐʱºòÇø·ÖÀ´Ô´ÓÚ Oracle Êý¾Ý¿âµÄ´íÎóºÍÀ´Ô´ÓÚ Oracle JDBC Çý¶¯³ÌÐòµÄ´íÎó·Ç³£ÖØÒª¡£ÀýÈ磬903£¨¡°ÎÞЧµÄ±íÃû¡±£©´íÎóÂë¶ÔÓ¦ Oracle Êý¾Ý¿â´íÎóÂë ORA-00903¡£Ïà·´£¬17003£¨¡°ÎÞЧµÄÁÐË÷Òý¡±£©´íÎóÂë¶ÔÓ¦ Oracle JDBC Çý¶¯³ÌÐò´íÎóÂë ORA-17003¡£ÕâÁ½ÖÖÀàÐ͵ĴíÎóÂ루Êý¾Ý¿âºÍ JDBC Çý¶¯³ÌÐò£©¶¼ÊÇ Oracle ÌØÓеģ¨Èç ORA- ǰ׺ËùָʾµÄÄÇÑù£©¡£ÒòΪûÓÐΪÀ´Ô´ÓÚ Oracle JDBC Çý¶¯³ÌÐòµÄ´íÎóÌṩ SQLState ´íÎóÂ룬Òò´Ë±ØÐëʹÓà Oracle ÌØÓеĴíÎóÂëÀ´Çø·ÖÓÉ JDBC Çý¶¯³ÌÐòµ¼ÖµĴíÎó¡£

ÔÚÏÂÃæµÄ±í 1 ÖÐÁгöÁË·ÃÎÊ Oracle Êý¾Ý¿âµÄ JDBC ÖеÄһЩ×î³£¼ûµÄ´íÎ󡣡°Ê¾Àý´úÂëºÍ/»ò×¢ÊÍ¡±ÁÐÖеÄʾÀýÏÔʾÁ˵¼ÖÂÕâÖÖ´íÎóµÄ SQL Óï¾äÀàÐÍ»òÌṩÁ˹ØÓÚ±íÖеĸÃÐÐÉÏÏÔʾµÄÌض¨´íÎóµÄÆäËû×¢ÊÍ¡£

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡±í 1

´íÎó±ê¼Ç
Oracle´íÎó
SQLState
ʾÀý´úÂëºÍ/»ò×¢ÊÍ
»ùÓÚÓï¾ä£ºSELECT ename FROM emp ±äÖÖµÄ SQL Ïà¹Ø´íÎó
¡°Î¨Ò»ÐÔÔ¼Êø¡± 1 2300 ÀýÈçÖ÷¼üÎ¥¹æ
¡°×ÊԴæÇÒÖ¸¶¨ NOWAIT »ñÈ¡×ÊÔ´¡± 54 61000 Ö»ÓÐÔÚÖ¸¶¨ÁË NOWAIT ʱ²Å³öÏÖ
¡°ÎÞЧµÄ SQL Óï¾ä¡±
900 42000 ename FROM emp
¡°ÎÞЧµÄ±íÃû¡±
903 42000 SELECT ename FROM
¡°ÎÞЧµÄ±êʶ·û¡±
904 42000 SELECT empname FROM emp
¡°ÎÞЧµÄ×Ö·û¡±
911 42000 SELECT ename FROM emp;
¡°È±ÉÙÁС±
917 42000 ÔÚ INSERT Óï¾äÖÐÐèÒª¶ººÅÀ´·Ö¸ôÁÐʱÒÅ©¶ººÅ¿ÉÄÜÊÇÒ»¸öÔ­Òò¡£
¡°ÔÚÆÚÍûµÄλÖÃûÓÐÕÒµ½ FROM ¹Ø¼ü×Ö¡±
923 42000 SELECT ename emp
¡°È±ÉÙ±í´ïʽ¡±
936 42000 SELECT FROM emp
¡°±í»òÊÓͼ²»´æÔÚ¡±
942 42000 SELECT ename FROM empp
¡°²»ÄܲåÈë¿ÕÖµ¡± 1400 23000 ÊÔͼÏò°üº¬ NOT NULL Ô¼ÊøµÄÁÐÖвåÈë¿ÕÖµ
¡°Öµ´óÓÚ¸ÃÁеÄÖ¸¶¨¾«¶È¡± 1438 22003 ÊÔͼ²åÈë±ÈÁÐÔÊÐíµÄ¾«¶È¸ü¶àµÄÊý×ÖλÊý
¡°ÎÞЧµÄÊý×Ö¡± 1722 42000 ÊÔͼ¶Ô×Ö·ûÖ´ÐÐÊýÖµº¯Êý
¡°ÍêÕûÐÔÔ¼Êøʧ°Ü¡± 2291 23000 ÊÔͼ²åÈë°üº¬ÓëÏÖÓÐÖ÷¼ü²»Æ¥ÅäµÄÍâ¼üµÄÐÐ
¡°ÖµÌ«´ó£¬¡± 12899 72000 ÊÔͼ²åÈ볬³öÁÐÔÊÐí·¶Î§µÄµÄÖµ£¨ÀýÈç¹ý¶àµÄ×Ö·û£©
¡°Io Òì³£¡± 17002 ÎÞ À´Ô´ÓÚ Oracle JDBC Çý¶¯³ÌÐòµÄ´íÎóûÓжÔÓ¦µÄ SQLState (null)
¡°ÎÞЧµÄÁÐË÷Òý¡±
17003 ÎÞ  
¡°ÎÞЧµÄÁÐÃû¡±
17006 ÎÞ  
¡°ÊýÖµÒç³ö¡±
17026 ÎÞ  

±¾ÎÄĩβµÄÔÚÏß×ÊÔ´²¿·Ö°üº¬Á˵½Ò»¸öÍøÕ¾µÄÁ´½Ó£¬¸ÃÍøÕ¾Ïêϸ½éÉÜÁËÓû§¿ÉÄÜÓöµ½µÄ¸÷ÖÖ Oracle Êý¾Ý¿âÒì³£¡£Oracle JDBC Çý¶¯³ÌÐò´íÎóÂë¿ÉÒÔÔÚ Oracle JDBC ¿ª·¢ÈËÔ±Ö¸ÄϺͲο¼µÄ¸½Â¼ B ÖÐÕÒµ½£¬¼¸ÖÖ³£¼ûµÄ Oracle Êý¾Ý¿â²úÉúµÄ´íÎóÂë¿ÉÒÔÔÚ Oracle Êý¾Ý¿â´íÎóÏûÏ¢ÎĵµÖÐÕÒµ½£¨Ã»ÓÐÁгö²úÆ·ÌØÓÐµÄ ORA ÏûÏ¢£©¡£

Spring ¿ò¼Ü¼ÈÖ§³Ö»ùÓÚ±ê×¼µÄ SQLState ÓÖÖ§³Ö¹©Ó¦ÉÌÌØÓеĴíÎóÂë¡£Óë×ÔÖ÷¿ª·¢µÄÊý¾Ý·ÃÎÊÈí¼þÏà±È£¬¸Ã¿ò¼Ü¶Ô¹©Ó¦ÉÌÌØÓеĴíÎóÂëµÄÖ§³ÖÀûÓÃÁËÓëÌØÓÐÊý¾Ý¿âµÄ¸üËÉÉ¢µÄñîºÏÀ´ÊµÏÖ¡£Spring ¿ò¼ÜÒýÈëÁËÒ»¸ö XML ÅäÖÃÎļþ£¬ÀûÓÃËü½«ÔÚ JDBC ´úÂëÖо­³£Óöµ½µÄijЩ¹©Ó¦ÉÌÌØÓеĴíÎóÓë Spring Ö§³ÖµÄÒì³£ÀàÁ¬½ÓÆðÀ´¡£Spring ÌṩµÄ sql-error-codes.xml ÅäÖÃÎļþÄ¿Ç°°üº¬ÁË´úÂëÇåµ¥ 4 ÖÐËùʾÕë¶Ô Oracle Êý¾Ý¿âµÄÅäÖᣣ¨ÔÚ¸ÃÎļþÖл¹Éæ¼°ÆäËûµÄÊý¾Ý¿â¹©Ó¦ÉÌ£¬µ«²¢°üº¬ÔÚ´Ë´úÂëÇåµ¥ÖС££©

¡¡´úÂëÇåµ¥ 4

<bean id="Oraclea€?
class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="badSqlGrammarCodes">
<value>900,903,904,917,936,942,17006</value>
</property>
<property name="invalidResultSetAccessCodes">
<value>17003</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>17002</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>1,1400,1722,2291</value>
</property>
<property name="cannotAcquireLockCodes">
<value>54</value>
</property>
</bean>

sql-error-codes.xml ÖÐµÄ value ÔªËصÄÖ÷ÌåÖеÄÕûÊýÓɶººÅ·Ö¸ô£¬ËüÃǶÔÓ¦ÓÚÇ°ÃæËùÊöµÄ¹©Ó¦ÉÌÌØÓеĴíÎóÂëµÄÊý×Ö²¿·Ö¡£ÔÚ±í 1 ÖÐÁгöÁË "badSqlGrammarCodes" Àà±ðÖеÄÐí¶àÊý×ÖÂë¡£17006 ´úÂëÊÇÒ»¸ö JDBC Çý¶¯³ÌÐò´íÎóÂ룬Ëüָʾ¡°ÎÞЧµÄÁÐÃû¡±¡£´úÂëÇåµ¥ 4 ÖÐµÄ property ÔªËرê¼ÇµÄ name ÊôÐÔָʾ Spring ¿ò¼ÜʹÓÃÄÄÖÖÀàÐ͵ÄÒì³£À´´¦ÀíÄÇЩÌض¨µÄ´íÎóÂë¡£ÀýÈ磬917 (ORA-00917) ´íÎ󽫵¼Ö Spring ¿ò¼ÜÅ׳öÒ»¸ö·ÇÇ¿ÖÆµÄ BadSqlGrammarException¡£ÒòΪ¸ÃÅäÖÃÎļþÊÇ XML ¸ñʽµÄ²¢ÇÒÔÚ´úÂëµÄÍⲿ£¬Òò´Ë¿ÉÒÔºÜÈÝÒ׵ؽ«ÆäËû´úÂëÌí¼Óµ½¸ÃÎļþÖУ¬ÓÃÓÚÅ׳ö×îÊʺÏÓÚÌض¨¹©Ó¦ÉÌ´íÎóÂëµÄ»ùÓÚ Spring µÄ JDBC Òì³£¡£

³öÓÚ¸÷ÖÖÔ­Òò£¬Äú¿ÉÄÜÏ£ÍûÅ׳öÓëÊý¾Ý¿âµÄ´íÎóÂë¶ÔÓ¦µÄÌض¨Òì³£¡£ÀýÈ磬Äú¿ÉÄÜÏ£ÍûÑ¡Ôñ´¦Àí SQLException Õý³£Å׳öµÄÇé¿ö£¬¶ø²»ÊÇ´¦ÀíËùÓеÄÇé¿ö¡£ÒòΪÔÚÐí¶àÇé¿öÏ£¬ÔÚÔËÐÐʱÄúÎÞ·¨¶Ô´úÂë×÷Èκδ¦Àí¡£Í¨¹ýΪÊý¾Ý¿â¿ª·¢ÈËÔ±´´½¨Ò»¸ö¸üϸÁ£»¯µÄÒì³£²ã´Î½á¹¹£¬ÒÔ¼°Í¨¹ýÌṩÌض¨Êý¾Ý¿â´íÎóºÍÌض¨Òì³£Ö®¼äµÄÒ»¸öËÉÉ¢ñîºÏµÄÁ¬½Ó£¬Spring ¿ò¼ÜʹÄúÄܹ»¸üÇáËɵش¦ÀíÄÇЩÈÝÒ×´¦ÀíµÄÒì³££¬¶øÑ¡ÔñºöÂÔ²»ÄܺÏÀí´¦ÀíµÄ·ÇÇ¿ÖÆÒì³£¡£

Spring Ϊ JDBC Ö§³ÖÌṩµÄÌر𷽱ãµÄÒì³£ÀàÖ®Ò»ÊÇ BadSqlGrammarException¡£¸ÃÒì³£ÀàÌṩÁËÒ»¸öÃû³ÆΪ getSql() µÄ·½·¨£¬¸Ã·½·¨½«·µ»ØÔÚÅ׳öÒ쳣ʱÕý±»µ÷ÓÃµÄ SQL Óï¾ä¡£ÒòΪ¸ÃÀà¿ÉÒÔʶ±ð SQL µÄÌØÕ÷£¨Ëü²»ÊÇÒ»¸öͨÓÃµÄ DAO Àࣩ£¬Òò´ËËü»¹Í¨¹ý getSQLException() ·½·¨ÌṩÁ˱ê×¼ SQLException µÄÒ»¸ö¾ä±ú¡£

³ýÁ˽«ÆäËûµÄ Oracle ÌØÓеĴíÎóÂëÌí¼Óµ½ sql-error-codes.xml ÎļþÖÐÒÔ½«ËüÃÇÓ³ÉäÖÁÏÖÓеÄÓÉ Spring ÌṩµÄÒì³£ÀàÖ®Í⣬Äú»¹¿ÉÒÔ´´½¨¶¨ÖƵÄÒì³£´¦ÀíÀࡣȻºó¿ÉÒÔ±àдһ¸ö¶¨ÖÆµÄ SQLExceptionTranslator ÀàÀ´½« Oracle ´íÎóÂëÓëÕâЩ¶¨ÖƵÄÒì³£´¦ÀíÀàÁ¬½ÓÆðÀ´¡£

½« PreparedStatement ÓÃÓÚ Spring

´úÂëÇåµ¥ 3 ÖÐµÄ Spring ʾÀýÒÀ¿¿ Spring µÄ Statement °ü×°À´Ö´ÐÐ SQL Óï¾ä¡£È»¶ø£¬Í¨³£ÍƼöʹÓà PreparedStatement ¶ø²»ÊÇ Statement À´¶ÔÊý¾Ý¿âÖ´ÐÐ SQL Óï¾ä¡£Spring JdbcTemplate ÀàΪÐí¶à·½·¨ÌṩÁËÔÚ Statement ºÍ PreparedStatement Á½ÕßÉϹ¹½¨µÄÏàͬµÄ¹¦ÄÜ£¬ÕâÑù±ãÓÚÄú°´ÐèÑ¡Ôñ JDBC Óï¾äµÄµ×²ãÀàÐÍ¡£

Spring µÄ»ùÓÚ Javadoc µÄ API ÎĵµÏêϸ˵Ã÷Á˸÷¸ö·½·¨ÊÇʹÓà Statement »¹ÊÇ PreparedStatement¡£Äú»¹¿ÉÒÔ¸ù¾ÝÊÇ·ñºÍ SQL ×Ö·û´®Ò»Æð´«µÝÁË SQL ²ÎÊý¸ø·½·¨À´·Ö±æ JdbcTemplate ʹÓÃÁËÄÄÒ»ÖÖÀàÐÍ¡£Èç¹ûÖ»´«ÈëÁË SQL ×Ö·û´®£¬ÄÇô·½·¨Ò»°ãʹÓà Statement¡£Èç¹û·½·¨½ÓÊÕÁË SQL ×Ö·û´®µÄ²ÎÊýÒÔ¼° SQL Óï¾ä£¬ÄÇô·½·¨Ò»°ãʹÓà PreparedStatement¡£ÏÂÃæµÄÁ½¸ö´úÂëÇåµ¥£¨´úÂëÇåµ¥ 5 ºÍ´úÂëÇåµ¥ 6£©ÏÔʾÁËʹÓà PreparedStatement µÄ±ê×¼ JDBC ·ÃÎʺͰü×° PreparedStatement µÄ»ùÓÚ Spring µÄ·ÃÎÊ¡£

¡¡´úÂëÇåµ¥ 5

String updateStr =
"UPDATE salgrade SET losal = ?, hisal = ?WHERE grade = ?";
PreparedStatement stmt = null;

try
{
stmt = this.myConnection.prepareStatement(updateStr);
stmt.setInt(1,aLowSal);
stmt.setInt(2,aHighSal);
stmt.setInt(3,aSalGrade);

updateStatus = stmt.execute();
stmt.close();
} // lots of catch and finally code typically follows here

´úÂëÇåµ¥ 6

String updateStr =
"UPDATE salgrade SET losal = ?, hisal = ?WHERE grade = ?";

JdbcTemplate jt = new JdbcTemplate(this.myDataSource);
jt.update( updateStr,
new Object[] { new Integer(aLowSal),
new Integer(aHighSal),
new Integer(aSalGrade) } );
// No handling/closing of PreparedStatement or catch/finally needed

´úÂëÇåµ¥ 6 ΪʹÓà Spring ¿ò¼ÜÀ´¸üÐÂÊý¾Ý¿âµÄÒ»¸öʾÀý¡£ËäÈ» "PreparedStatement" ÓïÒåûÓÐÔÚ´úÂëÇåµ¥ÖгöÏÖ£¬µ«±¾ÀýÖÐʹÓÃµÄ JdbcTemplate µÄÌØÊâµÄ¸üз½·¨µÄȷʹÓÃÁË PreparedStatement¡£JDBC µÄ±ê×¼Ó÷¨ÒªÇ󲶻ñ SQLException£¬²¢ÇÒ±ØÐèÒ»¸ö finally ³ÌÐò¿éÀ´È·±£¹Ø±ÕÓï¾ä¡£´úÂëÇåµ¥ 6 ÖеĻùÓÚ Spring µÄ´úÂëÔò²»ÐèÒªÕâЩ¡£

×¢ÒâÔÚ´úÂëÇåµ¥ 6 ÖеĿÉÖ´ÐдúÂëÖÐûÓÐÏÔʽµØÌá¼° PreparedStatement¡£Ê¹ÓÃÕâ¸ö·½±ãµÄ JdbcTemplate ¸üз½·¨µÄ¿ª·¢ÈËÔ±²»ÐèÒª¹ØÐÄ PreparedStatement µÄ¾ßÌåÓ÷¨¡¢Æä API »ò SQLException¡£×¢Ò⿪·¢ÈËԱʹÓÃÁËÒ»¸öÄäÃûÄÚ²¿À࣬ÒÔÌṩҪºÍ SQL ×Ö·û´®Ò»Æ𴫵ݸø JdbcTemplate.update ·½·¨µÄÖµ¡£

ʹÓà Oracle ÌØÓÐµÄ SQL Óï¾ä

Spring ¿ò¼ÜµÄÒ»¸öÓÐÓõÄÌصãÊÇËü½öרעÓÚ¡°°ü×°¡±JDBC ¿ª·¢µÄ×î³£ÓúÍ×îÂé·³µÄ·½Ã棬¶ø²»»á¹ý¶È×èÖ¹ÔÚÐèÒªµÄʱºòʹÓÃרÓÐµÄ SQL/JDBC¡£ËäÈ»ÎÒÃǶ¼Ï£ÍûʹÎÒÃǵĴúÂëÍêÈ«±ê×¼»¯£¨Èç¹ûÕâÑù×ö¶ÔÎÒÃÇÎÞÈκÎÓ°Ïì»òÓÐЩһ¶¨Ó°Ï죩£¬µ«ÓкܶàʱºòʹÓÃÌØÊâµÄ¹©Ó¦ÉÌÌØÓеÄÌØÐÔ½«Êǽ÷É÷ÉõÖÁ±ØÐëµÄ×ö·¨¡£

ÔÚ Oracle ·¶³ëÖеÄÒ»¸öʾÀý¾ÍÊÇʹÓà Oracle µÄ ROWID À´Î¨Ò»ÃèÊö Oracle ±íÖеÄÐС£´úÂëÇåµ¥ 7 ºÍ 8 ÏÔʾÁË´«Í³µÄ»ùÓÚ JDBC ºÍ Spring µÄ JDBC ´úÂ룬ËüÃÇ·Ö±ð¸ù¾ÝÌṩµÄÔ±¹¤ºÅ´Ó scott/tiger EMP ±íÖмìË÷ ROWID¡£ÔÚÁ½ÖÖÇé¿ö϶¼ÌṩÁËÒ»¸ö×÷Ϊ×Ö·û´®·µ»ØµÄ ROWID¡£

¡¡´úÂëÇåµ¥ 7

String queryStr = "SELECT rowid FROM emp WHERE empno = "
+ aEmpNum; // aEmpNum set previously
String rowId = null;
try
{
stmt = this.myConnection.createStatement();
rs = stmt.executeQuery(queryStr);
while ( rs.next() )
{
rowId = rs.getString("ROWID");
}
} // lots of catch-finally code needed after this

´úÂëÇåµ¥ 8

String queryStr = "SELECT rowid FROM emp WHERE empno = "
+ aEmpNum;
String rowId = null;

try
{
JdbcTemplate jt = new JdbcTemplate(this.myDataSource);
oracle.sql.ROWID oraRowId =
(ROWID) jt.queryForObject(queryStr, ROWID.class);
rowId = oraRowId.stringValue();
}
catch ( IncorrectResultSizeDataAccessException wrongSizeEx )
{
// This unchecked exception is thrown in this case if more
// than one result is returned from the query.
// Explicitly printing out the results of this exception's
// methods getExpectedSize() and getActualSize() is really not
// necessary in this case because this exception's getMessage()
// returns this same information in sentence form.
System.err.println( wrongSizeEx.getMessage() );
System.err.print( "Expected " + wrongSizeEx.getExpectedSize()
+ " results, but actually got back "
+ wrongSizeEx.getActualSize() + " results.");
}

³ýÁËÏÔʾ Spring ¿ò¼ÜÖ§³Ö Oracle ÌØÓеĹؼü×ÖµÄÁé»îÐÔÖ®Í⣬´úÂëÇåµ¥ 8 »¹ÏÔʾÁË Spring µÄ DAO Òì³£Ö®Ò»µÄÓÃ;¡£ÔÚ´úÂëÇåµ¥ 8 ÖУ¬Èç¹û²»Ð¡Ðı༭ÁË queryStr À´·µ»ØËùÓÐµÄ ROWID£¬ÄÇô½«Å׳ö IncorrectResultSizeDataAccessException¡£

רÓÐ Oracle SQL µÄ×îΪ´ó¼ÒËùÊìϤµÄÀý×Ó¿ÉÄÜÊÇÎÞ´¦²»ÔڵIJéѯ SELECT sysdate FROM dual¡£´úÂëÇåµ¥ 9 ÏÔʾÁËÕâ¸ö Oracle ÌØÓеIJéѯ£¨²»ÊÇ ANSI ±ê×¼µÄÒ»²¿·Ö£©ÈçºÎÓë Spring ¿ò¼ÜÒ»ÆðʹÓá£

¡¡´úÂëÇåµ¥ 9

String queryStr = "SELECT sysdate FROM dual";
Date date = null;

try
{
JdbcTemplate jt = new JdbcTemplate(this.myDataSource);
date = (Date) jt.queryForObject(queryStr, Date.class);
}
catch ( BadSqlGrammarException badSqlEx ) // unchecked
{
System.err.println( badSqlEx.getMessage() );
System.err.println( "Bad SQL:" + badSqlEx.getSql() );
}

DDL Óï¾äÓë Spring ºÍ JDBC

Ç°ÃæµÄ´úÂë´úÂëÇåµ¥ÑÝʾÁËʹÓà Spring ¿ò¼ÜÀ´´¦Àí DML Óï¾ä¡£Spring ¿ò¼ÜÌṩÁ˷dz£¼òµ¥µÄÓïÒåÀ´Ö§³Ö DDL Óï¾ä¡£´úÂëÇåµ¥ 10 ºÍ 11 ÑÝʾÁËÓÃÀ´Ö´ÐÐ´Ó Oracle Êý¾Ý¿âÖÐɾ³ýºÍÇå³ý±íµÄÓï¾äµÄ±ê×¼ JDBC ´úÂëºÍ Spring °ü×°µÄ JDBC ´úÂë¡£

¡¡´úÂëÇåµ¥ 10

Statement stmt = null;

try
{
stmt = this.myConnection.createStatement();
stmt.executeUpdate("DROP TABLE salgrade PURGE");
}
catch ( SQLException sqlEx )
{
System.err.println("Message:" + sqlEx.getMessage());
System.err.println("Error Code:" + sqlEx.getErrorCode());
System.err.println("SQL State:" + sqlEx.getSQLState());
}
finally
{
try
{
if (stmt != null)
{
stmt.close();
}
}
catch (SQLException finallySqlEx) // checked exception
{
System.err.println(finallySqlEx.getMessage());
}
}

´úÂëÇåµ¥ 11


try
{
JdbcTemplate jt = new JdbcTemplate(this.myDataSource);
jt.execute("DROP TABLE salgrade PURGE");
}
catch ( BadSqlGrammarException badSqlEx ) // unchecked
{
System.err.println( badSqlEx.getMessage() );
System.err.println( "BadSQL:" + badSqlEx.getSql() );
}

ÕâʱӦ¸ÃºÜÃ÷ÏÔ£¬»ùÓÚ Spring µÄ´úÂë±ÈÖ±½ÓµÄ JDBC ´úÂëÒª¸üÒ×ÓÚÔĶÁ£¨ÒÔ¼°±àдºÍά»¤£©¡£Êµ¼ÊÉÏ£¬Õâ¸ö´úÂëÇåµ¥ÖÐÖ»ÓÐÁ½ÐÐÊǾø¶Ô±ØÐèµÄ£¬ÒòΪ²¶»ñµÄÒì³£ÊÇÒ»¸ö·ÇÇ¿ÖÆÒì³£¡£

ÀûÓà Spring ¿ò¼Ü·ÃÎÊ´æ´¢¹ý³Ì

´úÂëÇåµ¥ 13 ºÍ 14 ·Ö±ðÑÝʾÁË´ÓÖ±½ÓµÄ JDBC ºÍ Spring °ü×°µÄ JDBC À´·ÃÎÊÒ»¸ö¾­¹ýÉè¼ÆµÄ´æ´¢¹ý³Ì£¨Èç´úÂëÇåµ¥ 12 Ëùʾ£©¡£

¡¡´úÂëÇåµ¥ 12

CREATE OR REPLACE PROCEDURE salary_percentile (
salary IN emp.sal%TYPE,
low IN salgrade.losal%TYPE,
high IN salgrade.hisal%TYPE,
percentile OUT NUMBER )
AS
BEGIN
IF salary < 0 THEN
percentile := null;
ELSE
percentile := (salary - low) / (high - low);
END IF;
END;

´úÂëÇåµ¥ 13

String escapeString = "{call salary_percentile (?,?,?,?)}";
CallableStatement cStmt = null;
double percentile = -1.0;
final int PERCENTILE_INDEX = 4;

try
{
cStmt = this.myConnection.prepareCall(escapeString);
cStmt.setInt(1, aSalary); // aSalary passed into this method
cStmt.setInt(2, aLow); // aLow passed into this method
cStmt.setInt(3, aHigh); // aHigh passed into this method
cStmt.registerOutParameter(PERCENTILE_INDEX, Types.DOUBLE);
cStmt.execute();
percentile = cStmt.getDouble(PERCENTILE_INDEX);
}
catch ( SQLException sqlEx )
{
System.err.println("Message:" + sqlEx.getMessage());
System.err.println("Error Code:" + sqlEx.getErrorCode());
System.err.println("SQL State:" + sqlEx.getSQLState());
}
finally
{
if ( cStmt != null )
{
try
{
cStmt.close();
}
catch (SQLException finallySqlEx)
{
System.err.println(finallySqlEx.getMessage());
}
}
}
return percentile;

´úÂëÇåµ¥ 14 ÖеÄʹÓûùÓÚ Spring µÄ´úÂëÀ´·ÃÎÊ´æ´¢¹ý³ÌµÄʾÀýÑÝʾÁË org.springframework.jdbc.object.StoredProcedure ÀàµÄÓ÷¨¡££¨°üº¬Õâ¸ö StoredProcedure ÀàµÄ Spring ³ÌÐò°üÒ²°üº¬Á˳ý´æ´¢¹ý³Ìµ÷ÓÃÖ®ÍâµÄÆäËûÀàÐ굀 SQL Óï¾äµÄ¶ÔÏó±íʾ¡£¹ØÓÚÆäËû SQL Óï¾äÀàÐ͵ĶÔÏó³ÌÐò°üºÍ¶ÔÏó±íʾµÄ¸ü¶àÏêϸÐÅÏ¢£¬Çë²Î¼û Spring µÄÎĵµ¡££©

¡¡´úÂëÇåµ¥ 14

private class SalaryCalculator extends StoredProcedure
{
/** Name of procedure in database. */
public static final String PROC_NAME = "salary_percentile";

/**
* Constructor for this StoredProcedure class.
* @param ds Data Source.
*/
public SalaryCalculator(DataSource ds)
{
setDataSource(ds);
setSql(PROC_NAME);

// Parameters should be declared in same order here that
// they are declared in the stored procedure.

declareParameter(new SqlParameter("salary", Types.DOUBLE));
declareParameter(new SqlParameter("low", Types.INTEGER));
declareParameter(new SqlParameter("high", Types.INTEGER));
declareParameter(new SqlOutParameter( "percentile",
Types.DOUBLE ) );
compile();
}

/**
* Execute stored procedure.
* @return Results of running stored procedure.
*/
public Map executeCalculation( double aSalary,
int aLow,
int aHigh )
{
Map inParameters = new HashMap();
inParameters.put( "salary", new Double(aSalary) );
inParameters.put( "low", new Integer(aLow) );
inParameters.put( "high", new Integer(aHigh) );
Map out = execute( inParameters ); // Call on parent class
return out;
}
}

// . . .
// Code below is all that is needed to call your Stored Procedure
// created above.
// . . .

SalaryCalculator calcPercentile =
new SalaryCalculator(this.myDataSource);
Map calcResults =
calcPercentile.executeCalculation(aSalary, aLow, aHigh);

return ((Double)calcResults.get("percentile")).doubleValue();

// . . .
// remainder of class
// . . .

´úÂëÇåµ¥ 14 ÖеijÌÐòÔÚµ¥¸öÀàÖбíʾ´úÂë¡£¸Ã´úÂëÇåµ¥ÖÐÏÔʾµÄ´úÂëµÄÖ÷ÌåÊÇÒ»¸öÀ©Õ¹ÁË Spring µÄ StoredProcedure µÄÄÚ²¿Àà (SalaryCalculator)¡£Õâ¸öÓÉ¿ª·¢ÈËÔ±´´½¨µÄÀà°ü×°ÁË´úÂëÇåµ¥ 12 ÖÐÏÔʾµÄ´æ´¢¹ý³Ì¡£µ÷Óà SalaryCalculator ÀàÖ»ÐèÒª¼¸ÐдúÂë¡£Òò´Ë£¬SalaryCalculator Àà¶Ôµ÷Óô洢¹ý³ÌËùÉæ¼°µÄ´ó²¿·ÖÎÊÌâ½øÐÐÁ˳éÏ󻯡£

ËäÈ»¿ª·¢ÈËÔ±±ØÐë±àдÀ©Õ¹ StoredProcedure µÄÀ࣬µ«Õâô×ö£¨¶ø²»ÊÇÖ±½Ó±àд´æ´¢¹ý³Ì·ÃÎÊ£©½«´øÀ´ÏàÓ¦µÄºÃ´¦¡£Ò»¸öºÃ´¦ÊÇÄܹ»Ê¹Óà Spring µÄÌØÓеķÇÇ¿ÖÆ DAO ºÍ JDBC Òì³£¶ø²»ÊÇͨÓõÄÇ¿ÖÆ SQLException¡£´ËÍ⣬ÕýÈç´úÂë´úÂëÇåµ¥Ëùʾ£¬´¦Àí¹Ø±Õ×ÊÔ´µÄ·³Ëö¹¤×÷±»³éÏó»¯ÁË¡£

×¢ÒâÔÚ½«Ð¡ÓÚ 0 µÄнˮֵ´«¸ø´æ´¢¹ý³Ìʱ´úÂëÇåµ¥ 13 ºÍ´úÂëÇåµ¥ 14 ·µ»ØµÄ½á¹ûµÄ²îÒìÊǺÜÓÐȤµÄ¡£ÔÚÖ±½ÓµÄ JDBC µÄÇé¿öÏ£¨´úÂëÇåµ¥ 13£©£¬½«·µ»ØÖµ 0.0£¬¶ø wasNull() ÊÇÈ·¶¨½á¹ûÊÇ·ñÕæÕýΪ¿ÕµÄΨһ·½Ê½¡£ÔÚ»ùÓÚ Spring µÄ´úÂëÖУ¨´úÂëÇåµ¥ 14£©£¬ÕâÖÖÇé¿öϽ«·µ»ØÒ»¸ö Java null£¬ÎÞÐè wasNull() µ÷Óá£

ÀûÓÃ Spring ·ÃÎÊ Oracle ¶ÔÏó

Spring Framework µÄ JDBC ³éÏó¿ÉÒÔÓë Oracle ¶ÔÏó£¨ÀýÈçÔÚ´úÂëÇåµ¥ 15 Öд´½¨µÄ¶ÔÏ󣩽áºÏʹÓá£

¡¡´úÂëÇåµ¥ 15

CREATE OR REPLACE TYPE address_type AS OBJECT
(
STREET VARCHAR2(20),
CITY VARCHAR2(15),
STATE CHAR(2),
ZIP CHAR(5)
);
/

CREATE TABLE emp_address_table
(
EMP_NUM NUMBER(4),
ADDRESS ADDRESS_TYPE
);

ÀûÓà JDBC ·ÃÎÊ Oracle ¶ÔÏó´æÔÚÁ½ÖÖ³£Óõķ½·¨¡£Ò»ÖÖ·½·¨Êǽ«±ê×¼µÄ JDBC ½Ó¿Ú java.sql.Struct ÓëÆä Oracle Çý¶¯³ÌÐòÌØÓеÄÀàʵʩ oracle.sql.STRUCT ½áºÏʹÓᣵڶþÖÖ·½·¨ÊÇ´´½¨Óë Oracle ¶ÔÏóÀàÐÍÓ³ÉäµÄ Java Àà¡£½« Oracle ¶ÔÏóÓë Oracle JDeveloper 10g IDE ºÍ JPublisher ½áºÏʹÓÃÊǼþÇá¶øÒ×¾ÙµÄÊ¡£

ʹÓà java.sql.Struct ·½·¨»ò JPublisher ·½·¨µÄÒ»¸öÓÐȤµÄ¡°¸±×÷Óá±£ºÈç¹ûÄúÏëÔÚÕâЩ¶ÔÏóÖзÃÎÊÊý¾Ý£¬ÄÇô±ØÐë´¦Àí SQLException¡£ÀýÈ磬ʹÓà java.sql.Struct ·½Ê½£¬getAttributes() ·½·¨½«Å׳ö SQLException¡£Í¬Ñù£¬JDeveloper/JPublisher ´´½¨µÄ Java ÀàÒ²½«°üº¬Å׳ö SQLException µÄ·½·¨¡£·ÃÎÊÕâЩ Java ¶ÔÏóµÄ¿ª·¢ÈËÔ±½«±ØÐë´¦ÀíÕâЩ SQLException£¬»òÕß¿ÉÒÔʹÓà Spring£¨Èç´úÂëÇåµ¥ 16 ºÍ 17 Ëùʾ£©¡£

¡¡´úÂëÇåµ¥ 16

String queryStr = "SELECT address FROM emp_address_table WHERE "
+ "emp_num = " + aEmpNum; // aEmpNum passed in
final List addresses = new ArrayList();

JdbcTemplate jt = new JdbcTemplate(this.myDataSource);
jt.query( queryStr,
new RowCallbackHandler()
{
public void processRow(ResultSet rs)
throws SQLException
{
// The Struct and ResultSet methods throw
// SQLException, so throws above is necessary
java.sql.Struct address =
(java.sql.Struct) rs.getObject(1);
String street =
address.getAttributes()[0].toString();
String city =
address.getAttributes()[1].toString();
String state =
address.getAttributes()[2].toString();
String zipCode =
address.getAttributes()[3].toString();
String addressStr = street + ", " + city + ", "
+ state + " " + zipCode;
addresses.add( addressStr );
}
}
);

´úÂëÇåµ¥ 17

String updateStr = "UPDATE emp_address_table SET address = ? "
+ "WHERE emp_num = ?";

JdbcTemplate jt = new JdbcTemplate( getDataSource() );
jt.update( updateStr,
new PreparedStatementSetter()
{
public void setValues(PreparedStatement ps)
throws SQLException
{
Address address = new Address();
address.setStreet(aStreet);
address.setCity(aCity);
address.setState(aState);
address.setZip(aZipCode);

ps.setObject(1, address);
ps.setInt(2, aEmpNum);
}
}
);

ÔÚÀûÓà JDBC ·ÃÎÊ Oracle ¶ÔÏóµÄÁ½ÖÖ·½Ê½Ï£¨Struct ºÍ SQLData£©¶¼½«Ê¹ÓÃÅ׳ö SQLException µÄ·½·¨À´·ÃÎÊ·µ»ØµÄÀà¡£´úÂëÇåµ¥ 16 ºÍ 17 ÏÔʾÁËÈçºÎʹÓÃÄäÃûÄÚ²¿»Øµ÷ÀàÀ´½« SQLException Òþ²ØÔÚÌØÓеķÇÇ¿ÖÆ Spring ¿ò¼ÜÒì³£²ã´Î½á¹¹Ö®ºó¡£ÕâЩÖØÐÂÅ׳öµÄÒì³£ÀûÓÃÁËÓë±¾ÎÄÖеÄÆäËûʾÀýÏàͬµÄÒ쳣ת»»¡£ÕâЩ´úÂëÇåµ¥²»½öרÃÅÑÝʾÁËÈçºÎ´¦Àí Oracle ¶ÔÏóµÄ»ùÓÚ Spring µÄ·ÃÎÊ£¬»¹ÑÝʾÁËÔÚÆäËûµÄ JdbcTemplate ³£¹æ·½·¨²»ÊÊÓÃʱÈçºÎʹÓÃÄäÃûÄÚ²¿»Øµ÷Àà¡£

ÔÚ´úÂëÇåµ¥ 16 ÖУ¬Äú½«ÔÚ±¾ÎÄÖеÚÒ»´Î¿´µ½ÔÚ»ùÓÚ Spring µÄ´úÂëÖгöÏÖ ResultSet ºÍ SQLException¡£²»¹ý£¬×¢ÒâÉõÖÁÔÚÕâЩ´úÂëÖÐҲûÓÐÖ±½ÓʹÓà SQLException¡£Ïà·´£¬Spring ¿ò¼Üͨ¹ýÆäÒì³£´¦Àí»úÖÆÀ´´¦ÀíÈκÎÅ׳öµÄ SQLException£¬ÄúÖ»Ðè¹ØÐÄÄúÏ£Íû²¶»ñºÍ´¦ÀíµÄ Spring Òì³£¡£

´úÂëÇåµ¥ 17 ÑÝʾÁ˱¾ÎÄÖеÚÒ»´ÎÔÚ»ùÓÚ Spring µÄ´úÂëÖÐʹÓà PreparedStatement£¬²¢ÏÔʾÁË¶Ô SQLException µÄÁíÒ»ÖÖÒýÓá£ÕýÈç´úÂëÇåµ¥ 16 µÄÇé¿öÒ»Ñù£¬SQLException Ö÷ÒªÓÃÓÚÒýÓà Spring ¿ò¼ÜµÄ JdbcTemplate À࣬ºóÕß½«´¦ÀíËü²¢½«ÈκÎÒì³£×÷Ϊ·ÇÇ¿ÖÆ Spring Òì³£Ìṩ¡£

´úÂëÇåµ¥ 16 ºÍ 17 ÑÝʾÁË Spring µÄ RowCallbackHandler ºÍ PreparedStatementSetter »Øµ÷½Ó¿ÚµÄÓ÷¨¡£ÔÚÕâЩ´úÂëÇåµ¥ÖÐʹÓÃÄäÃûÄÚ²¿ÀàʵÏÖÁËÕâЩ½Ó¿Ú¡£ËäÈ»ÓëÇ°ÃæµÄ´úÂëÇåµ¥ÖÐÏÔʾµÄ JdbcTemplate µÄ¸ü¼òµ¥µÄÓ÷¨Ïà±È£¬¿ª·¢ÈËÔ±±àдµÄÄÚ²¿Àà±ØÐëÖªµÀ¹ØÓÚ ResultSet ºÍ PreparedStatement ÒÔ¼°ËüÃǵĸ÷¸ö API µÄ¸ü¶àÐÅÏ¢£¬µ«ÄúÈÔÈ»ÎÞÐè¹ØÐÄ SQLException µÄ´¦Àí£»JdbcTemplate ½«Ö´ÐÐÒì³£´¦Àí¡£

Ç°ÃæµÄ»ùÓÚ Spring µÄ´úÂëÇåµ¥£¨ÀýÈç´úÂëÇåµ¥ 3 ºÍ 6 ÖÐʹÓÃµÄ JdbcTemplate£©ÉõÖÁûÓÐÌáµ½ ResultSet¡¢Statement¡¢PreparedStatement »ò SQLException¡£ÕâЩ¸ß¶È³éÏóµÄ·½·¨¶ÔÓÚ²»Ïë¹ØÐÄ JDBC µÄ¾ßÌåÓ÷¨µÄ¿ª·¢ÈËÔ±ÌرðÓÐÓᣲ»¹ý£¬ÕâЩ¼«Æä·½±ãµÄ·½·¨Ã»ÓдúÂëÇåµ¥ 16 ºÍ 17 ËùÑÝʾµÄÄÚ²¿Àà·½·¨Áé»î¡£´úÂëÇåµ¥ 16 ºÍ 17 ÖÐÏÔʾµÄ¸üÁé»îµÄ·½·¨¿ÉÒÔÔÚÐèҪʱʹÓã¨Ö»ÐèÉÔ΢Á˽â»ù±¾µÄ JDBC API£©¡£ÔÚËùÓÐÇé¿öÏ£¬Òì³£´¦Àí¶¼ÓÉ Spring Òì³£²ã´Î½á¹¹À´Ò»ÖµØÖ´ÐУ¬Äú²»ÐèÒª¹ØÐÄ SQLException¡£

ÆäËûºÃ´¦

Spring ¿ò¼ÜΪ JDBC ´øÀ´µÄºÃ´¦³ýÉÏÃæÉÔ΢Ïêϸ½éÉܵÄÖ®Í⣬»¹´æÔÚÆäËû¼¸¸ö½« Spring ¿ò¼ÜÓÃÓÚ JDBC µÄºÃ´¦£¨Ôڴ˲»ÔÙ½øÒ»²½ÌÖÂÛ£©¡£ÕâЩºÃ´¦°üÀ¨£º

  • Spring µÄ JdbcTemplate ÀàÖ§³ÖÆäËû¼¸¸öÓÉ Spring ÌṩµÄÓë±¾ÎÄËùÌÖÂ۵ĽӿÚÀàËƵĽӿڡ£ÕâЩ½Ó¿Ú°üÀ¨ ResultSetExtractor ºÍ PreparedStatementCreator£¬ËüÃÇ·Ö±ðÀàËÆÓÚ RowCallbackHandler ºÍ PreparedStatementSetter¡£JdbcTemplate Ö§³ÖµÄÐí¶à½Ó¿ÚÖеÄÁíÒ»¸öÊǷdz£ÓÐÓÃµÄ BatchPreparedStatementSetter¡£
  • ËäÈ»²»ÐèÒªÔÚ Spring Ó¦ÓóÌÐòÉÏÏÂÎÄÖÐʹÓà Spring JDBC ³éÏ󣬵«¿ÉÒÔÑ¡Ôñ½« JDBC ³éÏóÓÃÓÚ¸ÃÉÏÏÂÎÄ£¬ÕâÌṩÁ˶îÍâµÄºÃ´¦¡£¿ÉÒÔʹÓà Spring ¿ò¼Üͨ¹ýÅäÖÃÎļþÀ´½«Êý¾Ý·ÃÎʶÔÏóÓëÒµÎñ¶ÔÏóÏàÁ¬£¨¶ø²»ÊÇÔÚ´úÂëÖÐÖ±½ÓñîºÏ£©¡£
  • org.springframework.jdbc.object ³ÌÐò°üÖ§³Ö½«¹ØϵÊý¾Ý¿â²Ù×÷£¨°üÀ¨ DML Óï¾äºÍ´æ´¢¹ý³Ì£©×÷Ϊ¿ÉÖØÓõĶÔÏó¡£ÕâЩḬ̈߳²È«µÄ¶ÔÏóΪ¿ª·¢ÈËÔ±ÌṩÁ˸ü¸ß¼¶±ðµÄ¹ØϵÊý¾Ý¿â³éÏ󣨳¬¹ý±¾ÎÄ֮ǰ½éÉܺÍÔÚËùÓдúÂëʾÀýÖÐʹÓÃµÄ Spring µÄ JdbcTemplate£©¡£ÎÒ¸öÈËϲ»¶ JdbcTemplate ÌṩµÄ³éÏ󼶱ð£¬ÎÒ¾õµÃËü¶ÔÀýÐÐÎÊÌ⣨ÈçÒì³£¡¢½á¹û¼¯ºÍ×ÊÔ´¹ÜÀí£©µÄ´¦ÀíËùÌṩµÄÖ§³Ö¼¶±ðÕýÊÇÎÒÏëÒªµÄ¡£²»¹ý£¬Èç¹û»ú¹¹»òÆ俪·¢ÈËÔ±ÏëÏíÊܸü¸ß¼¶±ðµÄ¹ØϵÊý¾Ý¿â¸ÅÄîµÄ³éÏ󣬶ø²»±Ø²ÉÓÃÍêÕûµÄ¶ÔÏó-¹Øϵ (O/R) Ó³Éä¼¼Êõ£¨ÀýÈç TopLink »ò Hibernate£©£¬ÄÇô org.springframework.jdbc.object ³ÌÐò°ü½«ÖµµÃ¿¼ÂÇ¡£´úÂëÇåµ¥ 12 ÖеĴ洢¹ý³ÌʾÀýÌṩÁ˶ԸóÌÐò°üµÄ¼òµ¥³¢ÊÔ¡£
  • Spring ¿ò¼ÜÌṩÁËÒ»¸ö¿ÉÒÔÔÚÈÝÆ÷ÍⲿʹÓõļòµ¥µÄ DataSource ʵÏÖ£¬²¢ÌṩÁËÒ»¸ö¿ÉÒÔ¸ù¾ÝÐèÒª¸²¸ÇµÄ³éÏóÊý¾ÝÔ´Àࣨ²Î¼û Spring ²Î¿¼ÎĵµµÄµÚ 10 Õ£©¡£
  • ³ý JDBC Ö®Í⣬Spring ¿ò¼Ü»¹Ö§³Ö¶àÖÖ O/R Ó³Éä¼¼Êõ£¨ÀýÈç Hibernate¡¢iBatis ºÍ Java Êý¾Ý¶ÔÏó (JDO)£©¡££¨²Î¼û Spring ²Î¿¼ÎĵµµÄµÚ 11 Õ¡££©±¾Îĵײ¿µÄÔÚÏ߲ο¼²¿·Ö°üº¬ÁËÒ»Ìõµ½¹ØÓÚ½« Spring Óë Oracle µÄ TopLink ¶ÔÏó-¹ØϵӳÉä²úÆ·½áºÏʹÓõÄÆäËûÎÄÕµÄÁ´½Ó¡£
  • Spring ¿ò¼Ü¼ò»¯Á˹²ÏíÏàͬµÄÒì³£²ã´Î½á¹¹µÄ¼¼Êõ²»¿ÉÖªµÄ DAO ½Ó¿ÚºÍʵÏֵĿª·¢¡££¨²Î¼û Spring ²Î¿¼ÎĵµµÄµÚ 9 Õ¡££©±¾ÎÄÖеļ¸¸öµØ·½¼òµ¥Ìáµ½ÁËÕâ¸öÖ÷Ìâ¡£
  • Spring ¿ò¼ÜÌṩÁ˶ÔÔÚÐèҪʱ½« SQLWarning ×÷ΪÒì³£ (SQLWarningException) À´²¶»ñµÄÖ§³Ö¡£Õâ¶Ô Oracle Êý¾Ý¿âºÍ JDBC Çý¶¯³ÌÐò²»ÊÇÌرðÓÐÓã¬ÒòΪËüÃÇÇãÏòÓÚ¶Ô¼¸ºõÿһÖÖÇé¿öÅ׳ö SQLException¡£Ëü¶ÔÓÚÐèÒªÌṩ¾¯¸æ²¢ÔÚʹÓÃÒ»¸ö¸üÃܼ¯Ê¹Óà SQLWarning µÄÊý¾Ý¿âµÄ¿ª·¢ÈËÔ±¶øÑÔÊÇÒ»ÖÖ¸üÓÐÓõÄÌØÐÔ¡£ÔÚÕâЩÇé¿öÏ£¬Ö±½ÓʹÓà JDBC µÄ¿ª·¢ÈËÔ±±ØÐëÌرðѯÎÊÊÇ·ñ³öÏÖÁË SQLWarning¡£Spring ¿ò¼Ü¿ÉÒÔÓÃÀ´´¦ÀíÕâЩ¾¯¸æ£¨ºÍ´¦ÀíÈκÎÆäËûµÄ Spring Òì³£Ò»Ñù£©£¬ÒÔʵÏÖ¸üÈÝÒ׺͸üÒ»ÖµĴ¦Àí¡£
  • Spring ¿ò¼ÜÌṩÁË org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor ½Ó¿ÚºÍ¸Ã½Ó¿ÚµÄһЩʵÏÖ£¨ÀýÈç SimpleNativeJdbcExtractor£©¡£µ±Á¬½Ó±»ÁíÒ»¸ö DataSource °ü×°»òÕßҪͨ¹ýÌض¨µÄÁ¬½Ó³ØÀ´»ñȡʱ£¬ÕâЩ½Ó¿ÚºÍʵÏÖ¶ÔÓÚͨ¹ý Oracle Á¬½Ó»ò ResultSet À´·ÃÎÊ Oracle ÌØÐԷdz£ÓÐÓá£
  • Spring ÌṩÁË org.springframework.jdbc.support.lob.OracleLobHandler ÀàÀ´´´½¨ oracle.sql.BLOB£¨¶þ½øÖÆ´ó¶ÔÏó£©ºÍ oracle.sql.CLOB µÄʵÀý¡£
  • Spring ÌṩµÄ OracleSequenceMaxValueIncrementer ÀàÌṩÁË Oracle ÐòÁеÄÏÂÒ»¸öÖµ¡£Ëü¿ÉÒÔÓÐЧµØÌṩһЩÐÅÏ¢£¬ÕâЩÐÅÏ¢ÓëÄúÖ±½ÓʹÓÃÒÔÏÂÃüÁîʱÌṩµÄÐÅÏ¢Ïàͬ£ºselect someSequence.nextval from dual £¨ÆäÖÐ someSequence ÊÇ Oracle Êý¾Ý¿âÖеÄÐòÁÐÃû³Æ£©¡£¸Ã·½·¨µÄÒ»¸öºÃ´¦ÊÇ¿ÉÒÔÔÚ DAO ²ã´Î½á¹¹ÖÐʹÓà DataFieldMaxValueIncrementer ½Ó¿Ú£¬¶øÎÞÐè½ôÃܵØñîºÏ Oracle ÌØÓеÄʵÏÖ¡£
  • Spring ¿ò¼ÜµÄÆäËûÓÃ;

±¾ÎÄרעÓÚʹÓà Spring À´±àд¸üÒ×ÓÚά»¤ºÍ¸ü²»Ò׳ö´íµÄ JDBC ´úÂë¡£Spring ¿ò¼Ü»¹¿ÉÒÔÓÃÀ´×ö±ÈÕâ¸ü¶àµÃ¶àµÄÊÂÇé¡£³ýÁËÄܹ»Îª JDBC ×öºÍÐí¶à¶ÔÏó-¹ØϵӳÉä¼¼ÊõËùÄÜ×öµÄͬÑùµÄÊÂÇéÖ®Í⣬Spring »¹ÓµÓÐÖ§³Ö¿ÉÒÔÔÚÆóÒµÓ¦ÓóÌÐòÖÐÕÒµ½µÄËùÓÐÆäËû²ãµÄÌØÐÔ¡£ÕâЩÌØÐÔ°üÀ¨ÇáÁ¿¼¶µÄ IoC£¨·´×ª¿ØÖÆ£©ÈÝÆ÷Ö§³Ö¡¢Ò»¸ö Web ¿ò¼ÜºÍÆäËûµÄ J2EE Ó¦ÓóÌÐòÖ§³Ö¡£

Spring ¿ò¼Ü±»Éè¼ÆΪ·ÇÇÖÈëÐÔ»òÕß×îСÏ޶ȵÄÇÖÈëÐÔ¡£Äú¿ÉÒÔʹÓà Spring ¿ò¼ÜÀ´×öÐí¶àÊÂÇ飬µ«²¢²»Ç¿ÆÈÓ¦ÓóÌÐòÈ«ÒÀÀµÓÚ Spring ¿ò¼Ü¡£ÒòΪ Spring ÊÇ¿ª·ÅÔ´´úÂëµÄ£¬ÊÇÒ»¸öºÜÓÐÓõŤ¾ß£¬¿É×÷ΪÓÅÐãÓ¦ÓóÌÐòÉè¼Æ×¼ÔòµÄ¾ßÌåʾÀý¡£¼´Ê¹Ä¿Ç°Äú²»ÄÜÖ±½ÓÔÚÓ¦ÓóÌÐòÖÐʹÓà Spring ¿ò¼Ü£¬Ñ§Ï°Ê¹Óà Spring ¿ò¼Ü×÷ΪÓÅÐãÉè¼ÆºÍ´úÂë×¼ÔòµÄʾÀýÒ²ÊǺÜÓÐÊÕ»ñµÄ¡£Spring ÌṩµÄ JDBC Ö§³Ö²»ÐèҪʹÓà Spring µÄÈκÎÆäËûÌØÐÔ£¬ÊǼ«¼ÑµÄ Spring ¿ò¼ÜʹÓÃÈëÃÅ;¾¶£¬Ëü²»ÓÃÓë Spring µÄÆäËû²¿·Ö½ôÃܽáºÏ¡£

 


°æȨËùÓУºUMLÈí¼þ¹¤³Ì×éÖ¯