Graphic tutorial on connecting eclipse to oracle 11g Database

Source: Internet
Author: User

Graphic tutorial on connecting eclipse to oracle 11g Database

Eclipse is an open-source and Java-based scalable development platform. Eclipse comes with a standard plug-in set, including Java Development Kit (JDK ).

1. First, create a project DB, right-click the DB, and select Build Path --> Configure Build Path.

Use Add External JARs .. to select ojdbc14.jar under D: \ orcl \ app \ hr \ product \ 11.2.0 \ dbhome_1 \ owb \ wf \ lib, and click OK.

2. Compile the ConnectOracle. java File

Package com. wuy;

Import java. SQL. Connection;

Import java. SQL. DriverManager;

Import java. SQL. SQLException;

Public class ConnectOracle {

Private Connection con;

Private String user = "scott ";

// Private String user = "sys as sysdba ";

Private String password = "18233188050 ";

Private String className = "oracle. jdbc. driver. OracleDriver ";

// Private String url = "jdbc: oracle: oci @ localhost: 1158: orcl"; this url may be invalid

Private String url = "jdbc: oracle: thin: @ hr-PC: 1521: orcl"; hr-PC and 1521 are critical, that is, whether this is in the listener String.

D: \ orcl \ app \ hr \ product \ 11.2.0 \ dbhome_1 \ NETWORK \ ADMIN \ tnsnames. are there any records in this file? According to tnsnames. ora file to configure the url path, otherwise an error will occur !!!

Public ConnectOracle (){

Try {

Class. forName (className );

System. out. println ("database driver loaded successfully! ");

} Catch (ClassNotFoundException e ){

System. out. println ("failed to load the database driver! ");

E. printStackTrace ();

}

}

/** Create a database connection */

Public Connection getCon (){

Try {

Con = DriverManager. getConnection (url, user, password );

System. out. println ("database connection created successfully! ");

} Catch (SQLException e ){

System. out. print (con );

System. out. println ("An error occurred while creating the database connection! ");

Con = null;

E. printStackTrace ();

}

Return con;

}

Public void closed (){

Try {

If (con! = Null ){

Con. close ();

}

} Catch (SQLException e ){

System. out. println ("failed to close con object! ");

E. printStackTrace ();

}

}

Public static void main (String [] args)

{

ConnectOracle c = new ConnectOracle ();

C. getCon ();

C. closed ();

}

}

3. Running result:

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.