Oracle:JDBC链接Oracle的DEMO

发布时间:2023年12月20日

1、引入jar包:

2、DEMO:

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

文章来源:https://blog.csdn.net/Bof_jangle/article/details/135079500
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。