資源描述:
《java連接oracle實(shí)例》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、java連接oracle實(shí)例用的oracle版本是oracle92?????先將oracle的JDBC驅(qū)動(dòng)包加到class中,驅(qū)動(dòng)包在:$oracle_home/ora92/jdbc/bin下的ojdbc14.jar加到環(huán)境變量里。//配置文件oracle.propertiesjdbc.driver=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@192.168.2.57:1521:xtjdbc.user=sajdbc.pass
2、word=sa//連接數(shù)據(jù)庫(kù)類packagecom.conn;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Stat
3、ement;importjava.util.Properties;publicclassConn2{privateStringdbUrl=null;privateStringtheUser=null;//用戶名privateStringthePw=null;//密碼privateConnectionc=null;//privateStatementconn;privateResultSetrs=null;privateStringdriver=null;publicConn2(){???try{
4、????//ORACLE的JDBC驅(qū)動(dòng)類????Propertiespro=newProperties();????try{?????pro.load(newFileInputStream("com/conn/oracle.properties"));????}catch(FileNotFoundExceptione1){?????//TODOAuto-generatedcatchblock?????e1.printStackTrace();????}catch(IOExceptione1){?
5、????//TODOAuto-generatedcatchblock?????e1.printStackTrace();????}????System.out.println(pro.getProperty("jdbc.driver"));????dbUrl=pro.getProperty("jdbc.url");????theUser=pro.getProperty("jdbc.user");????thePw=pro.getProperty("jdbc.password");????driv
6、er=pro.getProperty("jdbc.driver");???Class.forName(driver).newInstance();????try{????c=DriverManager.getConnection(dbUrl,theUser,thePw);???}catch(SQLExceptione){????//TODOAuto-generatedcatchblock????e.printStackTrace();???}????try{????conn=c.createSt
7、atement();???}catch(SQLExceptione){????//TODOAuto-generatedcatchblock????e.printStackTrace();???}}catch(InstantiationExceptione){???//TODOAuto-generatedcatchblock???e.printStackTrace();}catch(IllegalAccessExceptione){???//TODOAuto-generatedcatchblock
8、???e.printStackTrace();}catch(ClassNotFoundExceptione){???//TODOAuto-generatedcatchblock???e.printStackTrace();}}publicResultSetexecuteQuery(Stringsql){???rs=null;???try{????rs=conn.executeQuery(sql);???}catch(SQLExceptione){????//TODOAuto-generatedc