(1)在MyEclipse中引入Oracle 11g自带的JDBC包(如“G:\app\welcome\product\11.1.0\db_1\jdbc\lib\ojdbc6.jar”):右击java项目,选择“Build Path”、“Configure Build Path”,单击“Add External JARs”按钮,选择你的ojdbc6.jar;
(2)开户Oracle服务;
(3)编写测试代码:
import java.sql.*;
public class TestOracle {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM scott.emp");
while(rs.next()){
System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
}
}catch(Exception err){
System.out.println(err.toString());
}
}
}
(4)运行Java程序:
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER
(2)开户Oracle服务;
(3)编写测试代码:
import java.sql.*;
public class TestOracle {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM scott.emp");
while(rs.next()){
System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
}
}catch(Exception err){
System.out.println(err.toString());
}
}
}
(4)运行Java程序:
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER