Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect to ORACLE using JDBC

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to use JDBC to connect to ORACLE. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Format 1: Oracle JDBC Thin using an SID:

Jdbc:oracle:thin:@host:port:SID Example: jdbc:oracle:thin:@localhost:1521:orcl

This format is the simplest and the most widely used.

The sid of your oracle can be obtained through the following command:

Sqlplus / as sysdba select value from v$parameter where name='instance_name';import java.sql.*;public class TestOrclConnect {public static void main (String [] args) {ResultSet rs = null; Statement stmt = null; Connection conn = null; try {Class.forName ("oracle.jdbc.driver.OracleDriver"); String dbURL = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection (dbURL, "admin2", "123") System.out.println ("connected successfully");} catch (ClassNotFoundException e) {e.printStackTrace ();} catch (SQLException e) {e.printStackTrace ();} finally {try {if (rs! = null) {rs.close (); rs = null;} if (stmt! = null) {stmt.close () Stmt = null;} if (conn! = null) {conn.close (); conn = null;}} catch (SQLException e) {e.printStackTrace ();}

Format 2: Oracle JDBC Thin using a ServiceName:

Jdbc:oracle:thin:@//host:port/service_name Example:jdbc:oracle:thin:@//localhost:1521/orcl.city.com

Notice the format here, @ followed by / /, followed by port: replaced with /, this format is recommended by Oracle, because for a cluster, the SID of each node is different, but SERVICE_NAME can contain all nodes.

The service_name of your oracle can be obtained in the following ways:

Sqlplus / as sysdba select value from v$parameter where name='service_names';import java.sql.*;public class TestOrclConnect {public static void main (String [] args) {ResultSet rs = null; Statement stmt = null; Connection conn = null; try {Class.forName ("oracle.jdbc.driver.OracleDriver"); String dbURL = "jdbc:oracle:thin:@//localhost:1521/orcl.city.com"; conn = DriverManager.getConnection (dbURL, "admin2", "123") System.out.println ("connected successfully");} catch (ClassNotFoundException e) {e.printStackTrace ();} catch (SQLException e) {e.printStackTrace ();} finally {try {if (rs! = null) {rs.close (); rs = null;} if (stmt! = null) {stmt.close () Stmt = null;} if (conn! = null) {conn.close (); conn = null;}} catch (SQLException e) {e.printStackTrace ();}

Format 3: Oracle JDBC Thin using a TNSName:

Jdbc:oracle:thin:@TNSName Example: jdbc:oracle:thin:@TNS_ALIAS_NAME

I found some resources on Google, and to achieve this connection, first create a tnsnames.ora file, and then indicate the file path through System.setProperty. Then specify the resources to be used in the file through the @ symbol in the URL above.

I have hardly seen this format now, and there are not many cases in which I can use it. Of course, since the specified resources can be read through a configuration file, you can also directly take them out and put them in URL. The URL template directly placed in URL is as follows (the tnsnames.ora file contains the code after the @ symbol. Of course, the advantage of using files is that you can configure multiple resources for easy management.)

Jdbc:oracle:thin:@ (DESCRIPTION= (ADDRESS_LIST= (ADDRESS= (PROTOCOL=TCP) (HOST=hostA) (PORT= 1522)) (ADDRESS= (PROTOCOL=TCP) (HOST=your host) (PORT=1521) (SOURCE_ROUTE=yes) (CONNECT_DATA= (SERVICE_NAME=your service_name)

The jdbc connection code is as follows:

"`import java.sql.*;public class TestOrclConnect {public static void main (String [] args) {ResultSet rs = null; Statement stmt = null; Connection conn = null; try {Class.forName (" oracle.jdbc.driver.OracleDriver "); String dbURL =" jdbc:oracle:thin:@ (DESCRIPTION= (ADDRESS_LIST= (ADDRESS= (PROTOCOL=TCP) (HOST=localhost) (PORT=1521) "+ (CONNECT_DATA= (SERVICE_NAME=orcl.city.com))" Conn = DriverManager.getConnection (dbURL, "admin2", "123"); System.out.println (" connected successfully ");} catch (ClassNotFoundException e) {e.printStackTrace ();} catch (SQLException e) {e.printStackTrace ();} finally {try {if (rs! = null) {rs.close (); rs = null } if (stmt! = null) {stmt.close (); stmt = null;} if (conn! = null) {conn.close (); conn = null;}} catch (SQLException e) {e.printStackTrace () The above is how to use JDBC to connect to ORACLE. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report