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
相关推荐
张哈大1 小时前
【 Redis | 实战篇 秒杀实现 】
数据库·redis·缓存
weixin_472339461 小时前
Postgresql与openguass对比
数据库·postgresql
惊起白鸽4506 小时前
MySQL全量,增量备份与恢复
数据库·mysql
暮雨疏桐7 小时前
MySQL SQL Mode及其说明
数据库·sql·mysql·sql mode
Tangcan-7 小时前
【MySQL】数据库基础
数据库·mysql
蔡蓝8 小时前
Mysql的索引,慢查询和数据库表的设计以及乐观锁和悲观锁
数据库·mysql
jstart千语8 小时前
【Redis】分布式锁的实现
数据库·redis·分布式
一把年纪学编程9 小时前
【牛马技巧】word统计每一段的字数接近“字数统计”
前端·数据库·word
极小狐9 小时前
极狐GitLab 通用软件包存储库功能介绍
java·数据库·c#·gitlab·maven
钢铁男儿9 小时前
C# 方法(可选参数)
数据库·mysql·c#