Java 连接oracle11g2


import java.sql.Connection; 

import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
  
public class Test { 
    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:ctl", "scott", "Admin518"); //ctl 为全局数据库名(自己安装数据库时设置的) "scott", "Admin518"分别为用户名和密码
            stmt = conn.prepareStatement("select * from emp where deptno=?"); 
            stmt.setInt(1, 10); 
            rs = stmt.executeQuery(); 
            while (rs.next()) { 
            System.out.print(rs.getInt("empno")); 
                 System.out.print(rs.getString("ename")); 
                 System.out.print(rs.getString("job")); 
                 System.out.print(rs.getInt("mgr")); 
                 System.out.print(rs.getDate("hiredate")); 
                 System.out.print(rs.getInt("sal")); 
                 System.out.print(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(); 
        } 
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
连接Oracle数据库,您需要遵循以下步骤: 1.下载并安装Oracle JDBC驱动程序 首先,您需要下载适用于Oracle数据库版本的JDBC驱动程序。您可以从Oracle官方网站下载JDBC驱动程序。 2.添加JDBC驱动程序到您的项目中 将下载的JDBC驱动程序添加到您的项目中。您可以将JDBC驱动程序复制到项目的lib文件夹中,或者将其添加到项目的classpath中。 3.编写Java代码以连接Oracle数据库 以下是连接Oracle数据库的Java代码示例: import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class OracleConnection { public static void main(String[] argv) { System.out.println("-------- Oracle JDBC Connection Testing ------"); try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { System.out.println("Where is your Oracle JDBC Driver?"); e.printStackTrace(); return; } System.out.println("Oracle JDBC Driver Registered!"); Connection connection = null; try { connection = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "username", "password"); } catch (SQLException e) { System.out.println("Connection Failed! Check output console"); e.printStackTrace(); return; } if (connection != null) { System.out.println("You made it, take control your database now!"); } else { System.out.println("Failed to make connection!"); } } } 在上面的示例中,“jdbc:oracle:thin:@localhost:1521:XE”是Oracle数据库的连接字符串。您需要将其替换为您的Oracle数据库的实际连接字符串。另外,“username”和“password”是您的Oracle数据库的用户名和密码。 4.运行Java代码以连接Oracle数据库 将Java代码编译并运行以连接Oracle数据库。如果一切正常,您应该看到“Oracle JDBC Driver Registered!”和“您成功控制了数据库!”的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值