先把ojdbc14.jar添加到引用库
import java.sql.*;
public class JdbcHelloWorld {
public static void main(String[] args) throws SQLException, ClassNotFoundException{
// 任选三种方式之一,注释其他两行就行
// 注册驱动方式1
// DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // mysql 是 com.mysql.jdbc.Driver()
// 注册驱动方式2
// System.setProperty("jdbc.drivers", "oracle.jdbc.driver.OracleDriver");
// 注册驱动方式3
Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:zhongyj", "scott", "tiger");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from EMP");
System.out.println("EMPNO ENAME"); while (rs.next()) { System.out.println(rs.getString("EMPNO") + " " + rs.getString("ENAME")); } rs.close(); stmt.close(); conn.close(); } }
|
输出
EMPNO ENAME
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
阅读(1047) | 评论(0) | 转发(0) |