Oracle:JDBC链接Oracle的DEMO

1、引入jar包:

2、DEMO:

java 复制代码
package jdbc;

import java.sql.*;

public class OracleConnectionExample {
	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		PreparedStatement statement = null;
		try {
			// Register JDBC driver
			Class.forName("oracle.jdbc.driver.OracleDriver");

			// Open a connection
			System.out.println("Connecting to database...");
			conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "username", "password");
			System.out.println("Connected!");

			// Do something with the Database here
			String sql = "select * from aa_student";
			statement = conn.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			while (resultSet.next()) {
				System.out.println("id:" + resultSet.getLong("id"));
			}
			statement.close();
			// Close the connection
			conn.close();
		} catch (SQLException se) {
			// Handle errors for JDBC
			se.printStackTrace();
		} catch (Exception e) {
			// Handle errors for Class.forName
			e.printStackTrace();
		} finally {
			// finally block used to close resources
			try {
				if (statement != null)
					statement.close();
				if (conn != null)
					conn.close();
			} catch (SQLException se) {
				se.printStackTrace();
			} // end finally try
		} // end try

		System.out.println("Goodbye!");
	}// end main
}// end OracleConnectionExample
相关推荐
Ha_To34 分钟前
2026.1.20 SQL Server命令
数据库
智在碧得42 分钟前
碧服打造DataOps全链路闭环,定义大数据工程化发布新标杆
大数据·网络·数据库
IvorySQL1 小时前
PostgreSQL 性能:云端与本地的延迟分析
数据库·postgresql
wangbing11251 小时前
分组取前几位
数据库
给我来一根2 小时前
用户认证与授权:使用JWT保护你的API
jvm·数据库·python
_F_y2 小时前
MySQL表的操作
android·数据库·mysql
SmartBrain2 小时前
Agent 知识总结
服务器·数据库·笔记
fenglllle3 小时前
MySQL explain format的差异
数据库·mysql
哈哈不让取名字4 小时前
用Pygame开发你的第一个小游戏
jvm·数据库·python
程序员敲代码吗4 小时前
Python异步编程入门:Asyncio库的使用
jvm·数据库·python