Create a new Java Project connectoracle, under the project to build a package, named Oracletest, under the package to build a class, named Oracletest, the following code into the class file.
Package oracletest;
Import java.sql.*;
public class Oracletest {public
static void Main (string[] args) {
//String serverName = ' localhost ';
try {
class.forname ("Oracle.jdbc.driver.OracleDriver");
System.out.println ("1");
String url = "Jdbc:oracle:thin: @localhost: 1521:icss";
Connection conn = DriverManager
. getconnection (URL, "system", "Manager1");
System.out.println ("2");
Statement stmt = Conn.createstatement ();
ResultSet rs = stmt.executequery ("SELECT * from table1");
while (Rs.next ()) {
System.out.println (rs.getstring ("id"));
System.out.println (rs.getstring ("name"));
}
catch (SQLException e) {
//TODO automatically generates catch blocks
e.printstacktrace ();
} catch (ClassNotFoundException e) {
//TODO automatically generates catch blocks
e.printstacktrace ();}}
If the operation succeeds, the connection is successful.
Errors may be encountered at run time
Java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDriver at
Java.net.urlclassloader$1.run (Unknown Source) at
java.security.AccessController.doPrivileged (Native method)
at Java.net.URLClassLoader.findClass (Unknown source) at
Java.lang.ClassLoader.loadClass (Unknown source)
at Sun.misc.launcher$appclassloader.loadclass (Unknown source) at
Java.lang.ClassLoader.loadClass (Unknown source) At
java.lang.ClassLoader.loadClassInternal (Unknown Source) in
JAVA.LANG.CLASS.FORNAME0 (Native method) At
java.lang.Class.forName (Unknown Source) at
Oracletest. Oracletest.main (Oracletest.java:9)
This is because we did not introduce Oracle's JDBC Connection classes.
Right-click on the project name to select Properties
In the pop-up window, select Java build Path-> libraries-> Add External JARS ...
Then select the Ojdbc14.jar file under the D:\oracle\ora92\jdbc\lib directory (where D:\oracle is my installation directory) and select Open
We see that we have added success
Select OK. Running the program again is OK.