Import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception;/*** JDBC connection to Oracle Database ** @ author zhanqi **/public class Oracle {Private Static string driver = "oracle. JDBC. driver. oracledriver "; Private Static string url =" JDBC: oracle: thin: @ 127.0.0.1: 1521: orcl "; public static void main (string [] ARGs) {connectio N Ct = NULL; preparedstatement PS = NULL; resultset rs = NULL; try {// load the driver class. forname (driver); // obtain the connection Ct = drivermanager. getconnection (URL, "Scott", "Scott"); // obtain the pre-compiled SQL statement object PS = CT. preparestatement ("select count (*) from EMP"); // obtain the result set rs = ps.exe cutequery (); // cyclically retrieve the record while (RS. next () {system. out. println (RS. getint (1) ;}} catch (exception e) {e. printstacktrace ();} finally {// off Close resource try {If (RS! = NULL) {Rs. Close () ;}if (PS! = NULL) {ps. Close () ;}if (CT! = NULL) {CT. Close () ;}} catch (sqlexception e) {e. printstacktrace ();}}}}
Appendix:
- Oracle JDBC driver: Download
- Oracle JDBC driver: Oracle Installation Directory: F: \ app \ zhanqi \ product \ 11.2.0 \ dbhome_1 \ JDBC \ Lib