Java two ways to connect to Oracle

Source: Internet
Author: User

Used to learn. NET connection to a database, it also makes a variety of connection configurations. Now, while using Java to manipulate Oracle, there are also some operations. Here's a summary.


1.JDBC_ODBC Connection Database

This method requires an ODBC data source to be configured, and the native must have an Oracle database

1. Configure the data source

Open Control Panel--management tools--data source



2. Next connect in the code

To connect using JDBC_ODBC bridging, you need to configure the data source public static void main (String [] args) {try{//1. Load driver Class.forName (" Sun.jdbc.odbc.JdbcOdbcDriver ")//2. Gets the connection, where Testdemo is the name of the configured data source connection ct=drivermanager.getconnection (" Jdbc:o Dbc:testdemo "," Scott "," Tiger ");//start with SQL Server statement sm=ct.createstatement (); ResultSet rs=sm.executequery ("SELECT * from T_user"), while (Rs.next ()) {//output user name System.out.println (rs.getstring (2));}} catch (Exception e) {e.printstacktrace ();}}


2. Pure JDBC Driver Connection Oracle

This approach requires the loading of Oracle's jar packages without the need to configure a data source, which can be remotely connected without an Oracle database. The code is as follows:


Use JDBC to connect to the database public static void main (String [] args) {try{//1. Load drive Class.forName ("Oracle.jdbc.driver.OracleDriver"); /2. Get the connection, where Testdemo is the name of the configured data source, the ORCL above is the instance of the database connection ct=drivermanager.getconnection ("jdbc:oracle:thin:@ 192.168.24.202:1521:orcl "," Scott "," Tiger ");//start with SQL Server statement sm=ct.createstatement (); ResultSet rs=sm.executequery ("SELECT * from T_user"), while (Rs.next ()) {//output user name System.out.println (rs.getstring (2));}} catch (Exception e) {e.printstacktrace ();}}

Java two ways to connect to Oracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.