java jdbc连接oracle数据库

本人用的是MyEclipse10 ,JAVA用的是1.7,oracle用的是database 11g

import java.sql.*;


public class TestJDBC {
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
				//new oracle.jdbc.driver.OracleDriver();
				conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
				stmt = conn.createStatement();
				rs = stmt.executeQuery("select * from  dept");
				while(rs.next()) {
					System.out.print(rs.getString("deptno") + " ");
					System.out.println(rs.getString("dname"));
				}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs != null) {
					rs.close();
					rs = null;
				}
				if(stmt != null) {
					stmt.close();
					stmt = null;
				}
				if(conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注