1. Connect Sybase
1) Preparatory work:
Prepare Sybase driver Jconn2.jar To reference this jar package in eclipse:
Right-click on the project >build path>add External Archives ..., find Jconn2.jar, click OK.
Note: There is an article on the internet, put Jconn2.jar into the CLASSPATH environment variable, this method in the command line with Javac manual compilation is feasible. It is necessary to introduce Jconn2.jar into the project in Eclipse.
2) A sample code
Packageconndb;ImportJava.sql.*;Importjava.util.Properties; Public classDataswift { Public Static voidmain (String [] args) {Connection conn=NULL; Statement Statement=NULL; ResultSet RS=NULL; Try{class.forname ("Com.sybase.jdbc2.jdbc.SybDriver"); String URL= "Jdbc:sybase:tds:localhost:5000/cwbase15"; Properties Props=system.getproperties (); Props.put ("User", "sa"); Props.put ("Password", "Sasasa"); Conn=drivermanager.getconnection (url,props); Statement=conn.createstatement (); RS= Statement.executequery ("SELECT * from TEST"); while(Rs.next ()) {System.out.println (rs.getstring ("TEST")); } }Catch(Exception e) {e.printstacktrace (); }finally{ Try{ if(rs!=NULL) Rs.close (); if(statement!=NULL) Statement.close (); if(conn!=NULL) Conn.close (); }Catch(Exception e) {e.printstacktrace (); } } }}
View Code
2. Connect Oracle
1) Preparatory work
Prepare the Oracle driver Ojdbc14.jar to reference this jar package in eclipse. method as above.
2) A sample code:
Packageconndb;ImportJava.sql.*;Importjava.util.Properties; Public classconnoracle { Public Static voidmain (String [] args) {Connection conn=NULL; Statement Statement=NULL; ResultSet RS=NULL; Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); String URL= "Jdbc:oracle:thin: @localhost: 1521:orcl"; Properties Props=system.getproperties (); Props.put ("User", "system"); Props.put ("Password", "abcd1234"); Conn=drivermanager.getconnection (url,props); Statement=conn.createstatement (); RS= Statement.executequery ("SELECT * from TEST"); while(Rs.next ()) {System.out.println (rs.getstring ("NAME")); } }Catch(Exception e) {e.printstacktrace (); }finally{ Try{ if(rs!=NULL) Rs.close (); if(statement!=NULL) Statement.close (); if(conn!=NULL) Conn.close (); }Catch(Exception e) {e.printstacktrace (); } } }}
View Code
Examples of Java connections to Sybase and Oracle in Eclipse