In Java programs, the steps to access an Oracle database through JDBC

Source: Internet
Author: User
Tags stmt

(1) Loading and registering the JDBC driver for the database

Load JDBC Driver:

Class.forName ("Oracle.jdbc.driver.OracleDriver");

Register JDBC Driver:

Java.sql.DriverManager.registerDriver (New Oracle.jdbc.driver.OracleDriver ());

(2) Establish a connection with the database

To establish a connection to a database, you first create a URL for the specified database. The URL object that connects the database is established using the DriverManager getconnection method. The database URL object is similar to the Uniform Resource location of network resources, and its form is as follows:



JDBC indicates that database access is currently performed through a Java database connection;

Subprotocol represents the database connectivity mechanism supported by some driver;

SubName represents the specific name under the current connection mechanism;

Hostname represents the host name;

Port indicates the corresponding connection ports;

DatabaseName represents the name of the database to connect to.

Here for an example of a connection to an Oracle database:

Connect Oracle 8/8i/9i Database (in thin mode)

url = jdbc:oracle:thin: @hostip: 1521:oraclesid;

Note: HostIP refers to the IP address of the host, Oraclesid refers to the SID of the database.

Furthermore, determine the username and password of the connection database, that is, the user and password values:

user = "Ⅹⅹⅹ";

Password = "Ⅹⅹⅹ";

Finally, use the following statement:

Connection con=java.sql.drivermanager.getconnection (Url,user,password);

(3) Create statement objects

For example:

Statement stmt = Con.createstatement ();

(4) Calling and executing SQL statements

For example:

String sql = "Select a,b,c from table1";//table1 for the name of the table you want to query a,b,c for the field you want to query

ResultSet rs = stmt.executequery (SQL);

(5) Accessing the recordset in ResultSet and fetching records from it

For example: ();

Rs.absolute (4);

String col1=rs.getstring (1);


(6) Turn off ResultSet, statement and connection objects in turn

For example:

Rs.close ();

Stmt.close ();

Con.close ();

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: 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.