Java初学者笔记:JDBC连接Oracle数据库

本文介绍如何在Windows XP系统中使用JDBC连接Oracle10g数据库,包括编译Java脚本、加载JDBC驱动、设置连接URL及执行SQL语句等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/** * JDBCTest.java * 编译脚本 cd/ cd D:/JavaHome/temp d: javac JDBCTest.java java JDBCTest pause */ import java.sql.*; // WindowsXP使用JDBC连接Oracle10g数据库 // Oracle10g 客户端安装在: "D:/OraClient10g" public class JDBCTest { public static void main(String args[ ]) { Connection connection = null; Statement statement = null; try { // Load the JDBC Driver // 必须在系统环境变量 CLASSPATH 添加 "D:/OraClient10g/jdbc/lib/classes12.zip" String DBDRIVER = "oracle.jdbc.driver.OracleDriver"; // 找到 "D:/OraClient10g/NETWORK/ADMIN/tnsnames.ora" 文件, 将 (DESCRIPTION=...)拷贝到下面 String URL = "jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = mine)(PORT = 1521)) )(CONNECT_DATA =(SID = cheung)(SERVER = DEDICATED)))"; Class.forName(DBDRIVER).newInstance(); // Connect to the database connection = DriverManager.getConnection(URL, "scott", "tiger"); // Obtain a statement object statement = connection.createStatement(); // Execute the SQL String sql = "select * from JDBC_TEST"; ResultSet rs = statement.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1)); // 列的索引: 1-based } rs.close(); } // Don't try this at home, catch SQLException and all others catch( Exception e ) { e.printStackTrace(); } finally { // Time to close everthing up. if( statement != null ) { try { statement.close(); } catch( SQLException e ){ } // nothing we can do } if( connection != null ) { try { connection.close(); } catch( SQLException e ){ } // nothing we can do } } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值