如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
ThisisdocumentwillassistnewcomersinwritingjavacodetoperformsimpleconnectivitytoanASAdatabaseusingthejConnectJDBCdriver.ThisarticlediscussesasimpleJavaprogramwhichconnectstoanASAdatabaseusingtheSybasejConnectJDBCdriver.Althoughsimple,thiscodeiscommonlyrequested.Havingthiscodeathandwillproveasahandyreference.FurtherresearchintoJDBCandthejava.sqlpackageishighlyrecommendedtobetterunderstandJDBCprogramming.HereisthecodenecessarytoconnecttoanASAdatabaseviajConnect.Thissampleretrievesaresultsetconsistingofalltherowsandcolumnsoftheemployeetable.ThemyConnectioncode:importjava.sql.*;publicclassmyConnection{publicstaticvoidmain(String[]args){try{//UsingSybasejConnect4.2Class.forName("com.sybase.jdbc.SybDriver");//UsingSybasejConnect5.2//Class.forName("com.sybase.jdbc2.jdbc.SybDriver");Connectionconn=DriverManager.getConnection("jdbc:sybase:Tds:testmachine-pc:2638","dba","sql");Statementstmt=conn.createStatement();ResultSetrs=stmt.executeQuery("SELECT*FROMEMPLOYEE");while(rs.next()){System.out.println(rs.getInt(1));}}catch(Exceptione){System.out.println("Anexceptionoccurred.");}}//endofmain}//endofMyConnectionclassBriefexplanationofthecode:Inordertounderstandhowthiscodeworks,importantsectionsofcodewillbediscussed.Section1:importjava.sql.*;Thislineofcodeisnecessarytohaveaccesstothedatabaseconnectivityclasses.Thejava.sqlpackagecontainstheconnection,statement,andresultsetclassesnecessarytoretrieveanddisplaydatafromadatabase.Section2://UsingSybasejConnect4.2Class.forName("com.sybase.jdbc.SybDriver");//UsingSybasejConnect5.2//Class.forName("com.sybase.jdbc2.jdbc.SybDriver");ThesetwolinesofcodeareusedtodynamicallyloadtheSybasejConnectdriver.ChangethecommentingofthiscodeaccordingtowhichversionofjConnectinuse.Oftenpeoplewonderwhysuchcodeisused,andwhyisitnotbettertosimplyimportthenecessarypackages.Dependingonthelogicofyourapplication,itmaybenecessarytoconnecttoyourdatabasefromavarietyofdifferentways.Dynamicallyloadingadri