1. Grant scott user connection permissions: Grant connect to scott;
2. Create a java project in Myeclipse to import the jdbc package (classes12.jar ):
Right-click the project bulid path-> add external archives and select classes12.jar.
3. Create a java file, lianxi01.java
ImportJava. SQL. Connection;
ImportJava. SQL. ResultSet;
ImportJava. SQL. SQLException;
ImportJava. SQL. Statement;
ImportOracle. jdbc.*;
ImportOracle. jdbc. driver.*;
PublicclassLianxi01 {
PublicstaticvoidMain (String [] args)ThrowsSQLException, ClassNotFoundException {
//TODOAuto-generated method stub
Oracle. jdbc. OracleDriver.IsDebug();
Class.ForName("Oracle. jdbc. driver. OracleDriver ");
Java. SQL. DriverManager.RegisterDriver(NewOracle. jdbc. driver. OracleDriver ());
//
String url = "jdbc: oracle: thin: @ 100.100.100.20: 1521: dbsid2 ";
String user = "scott ";
String password = "tiger ";
StringSQL= "Select table_name from user_tables ";
String sql2 = "select * from dept ";
//
Connection con = java. SQL. DriverManager.GetConnection(Url, user, password );
Statement stmt = con. createStatement ();
ResultSet rs = (java. SQL. Statement) stmt1_.exe cuteQuery (sql2 );
While(Rs. next ()){
// String s = rs. getString (2 );
System.Out. Println (rs. getString (1) + "" + rs. getString (2) + "" + rs. getString (3 ));
}
}
}
4. Click Run to query the database.
5. Note: A String url line, @ 100.100.100.20: 1521: dbsid2Modify the IP address and service name.