java连接oracle范例

 

需要引用:ojdbc6.jar

import java.sql.*;

public class Main {
    static Connection conn;
    static Statement stat;
    static ResultSet rs;
    public static void main(String[]args){
        try{
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.140.110:1521/ora11g","ora","ora");
            
            stat=conn.createStatement();
            rs=stat.executeQuery("select * from all_users");
            while(rs.next()){
                System.out.println(rs.getString("username"));
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                rs=null;    
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }
    }
}

 

posted @ 2014-01-12 11:17  zhizhou  阅读(342)  评论(0编辑  收藏  举报