How to connect eclipse to the Oracle database

Source: Internet
Author: User

Java prerequisites
1. First, there are many (Oracle Installation Directory) under D: \ oracle \ ora92 \ JDBC \ Lib. Jar package file. You must copy the ojdbc14.jar file to your project, right-click the file, and choose "build path"> "import, now you can open and use the class files under the package. (Note: You can also place the driver in the classpath environment variable, and set other class packages similarly)
2. Continue loading and registering the driver
Class. forname ("oracle. JDBC. Driver. oracledriver ");
After the driver program is loaded, a driver object is generally created and automatically registered by calling drivermanager. registerdriver.
3. Establish a connection
Drivermanager. getconnection ("JDBC: oracle: thin: @ localhost: 1521: LC", name, pass );
The standard syntax of jdbc url is as follows:
::
At this point, we can find that there are three parts:
◆ Protocol: main communication protocols
◆ Subprotocol: the name of the driver of the secondary communication protocol.
◆ Data source identifier: Data Source
As shown in the preceding example:
"JDBC: oracle: thin" is the communication protocol, @ "is the valid host address, followed by the port number. The default value is 1521, followed by your data source ,, the user name and password can also be used in the following form:
Connection con = drivermanager. getconnection ("JDBC: oracle: thin: Name/
Pass @ localhost: 1521: LC ");
Returns the con object of a connection.
4. Create an SQL statement object
In this example, a premade statement is created.
Preparedstatement pstmt = con. preparestatement (SQL statement );
You can also use:
Statement stmt = con. createstatement ();
For details about the differences, see the help documentation.
5. Execute this statement
Executeupdate (), executequery (), execute (),
For usage instructions, see the help documentation.
6. Complete cleaning
Close the connection. Complete example:
This class completes loading and connection,
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. sqlexception;
Public class dbconnection {
Public static connection dbconn (string name, string pass ){
Connection c = NULL;
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
} Catch (classnotfoundexception e ){
E. printstacktrace ();
}
Try {
C = drivermanager. getconnection ("jdbcracle: thin: @ localhost: 1521: sumoonbest ",

Name, pass );
} Catch (sqlexception E1 ){
E1.printstacktrace ();
}
Return C;
}
}

Finally, this class completes the sending statement, execution, and cleaning.
Import java. SQL .*;
Public class dB extends dbconnection {
Private Static connection con = NULL;

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.