jdbc for oracle demo

package com.huawei.oracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OracleTest {
	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
			stmt = conn.prepareStatement("select * from emp where deptno=?");
			stmt.setInt(1, 10);
			rs = stmt.executeQuery();
			while (rs.next()) {
				System.out.println(rs.getInt("empno"));
				System.out.println(rs.getString("ename"));
				System.out.println(rs.getString("job"));
				System.out.println(rs.getInt("mgr"));
				System.out.println(rs.getDate("hiredate"));
				System.out.println(rs.getInt("sal"));
				System.out.println(rs.getInt("comm"));
				System.out.println(rs.getInt("deptno"));
				System.out.println("----------------------------");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			if(rs!= null) rs.close();
			if(stmt!= null) stmt.close();
			if(conn != null) conn.close();
		}
	}
}

  

posted @ 2011-08-24 22:02  水之原  阅读(1573)  评论(0编辑  收藏  举报