码迷,mamicode.com
首页 > 数据库 > 详细

Java连接Oracle数据库

时间:2015-09-14 22:49:55      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

package com.db;  
      
    import java.sql.Connection;  
    import java.sql.DriverManager;  
    import java.sql.PreparedStatement;  
    import java.sql.ResultSet;  
      
    public class DBConnection {  
        // 连接Oracle数据库  
        public void OracleConnection() {  
            Connection con = null;  
            PreparedStatement pre = null;  
            ResultSet rs = null;  
      
            try {  
                // 1. 加载Oracle驱动程序  
                Class.forName("oracle.jdbc.driver.OracleDriver");  
                  
                // 2. 设置Oracle数据库基本信息  
                String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";  
                String user = "scott";  
                String password = "goodluck";  
      
                // 2. 获取连接  
                con = DriverManager.getConnection(url, user, password);  
                System.out.println("----> Connection Success!");  
      
                // 3. 执行SQL语句  
                String sql = "SELECT * FROM EMP";  
                pre = con.prepareStatement(sql);  
      
                // 4. 获取结果集  
                rs = pre.executeQuery();  
                while (rs.next()) {  
                    System.out.println("编号:" + rs.getString("empno")   
                                    + ";姓名:" + rs.getString("ename")  
                                    + "; 工作:" + rs.getString("job")  
                                    + "; 领导:" + rs.getString("mgr")  
                                    + "; 雇佣日期:" + rs.getString("hiredate")  
                                    + "; 工资:" + rs.getString("sal")  
                                    + "; 奖金:" + rs.getString("comm")  
                                    + "; 部门:" + rs.getString("deptno"));  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    if (rs != null)  
                        rs.close();  
                    if (pre != null)  
                        pre.close();  
                    if (con != null)  
                        con.close();  
                    System.out.println("----> Connection End <-----");  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
      
        public static void main(String[] args) {  
            DBConnection db = new DBConnection();  
            db.OracleConnection();  
        }  
    }


Java连接Oracle数据库

标签:

原文地址:http://my.oschina.net/u/2259631/blog/506063

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!