The most basic Oracle database connection code (for oracle11g only):
1, right-click Project, build path, configuration build path, select the third item "Library", then click "Add External Jar" and select "D:\Oracle\app\oracle\product\11.2.0\server \jdbc\lib\ Ojdbc6_g.jar "(Note: D:\Oracle is the installation path for the database).
2. The following code is a very standard example of Oracle database connection code:
/*** A very standard example code for connecting to an Oracle database*/ Public voidtestoracle () {Connection con=NULL;//Create a connection to a databasePreparedStatement pre =NULL;//Create precompiled statement objects, usually with this instead of statementResultSet result =NULL;//Create a result set object Try{class.forname ("Oracle.jdbc.driver.OracleDriver");//load the Oracle driverSystem.out.println ("Start trying to connect to the database!") "); String URL= "Jdbc:oracle:" + "thin:@127.0.0.1:1521:xe";//127.0.0.1 is a native address, XE is the default database name for Lite OracleString user = "System";//user name, system default account nameString Password = "147";//The password you selected when you installed the settingscon = drivermanager.getconnection (url, user, password);//Get ConnectionsSYSTEM.OUT.PRINTLN ("Connection Successful! "); String SQL= "SELECT * from student where name=?";//precompiled statement, "? The delegate parameterPre = con.preparestatement (SQL);//Instantiating precompiled StatementsPre.setstring (1, "Liu Xianhan");//The parameter is set, and the previous 1 indicates the index of the parameter, not the index of the column name in the tableresult = Pre.executequery ();//Execute the query, note that no additional arguments are required in parentheses while(Result.next ())//When the result set is not emptySystem.out.println ("School Number:" + result.getint ("id") + "Name:" + result.getstring ("name")); } Catch(Exception e) {e.printstacktrace (); } finally { Try { //close each of the above objects individually, because if you do not close it, it will affect performance and consume resources//Note the order of the closing, the last used first close if(Result! =NULL) Result.close (); if(Pre! =NULL) Pre.close (); if(Con! =NULL) Con.close (); System.out.println ("The database connection is closed!" "); } Catch(Exception e) {e.printstacktrace (); } }}