JDBC Connect Oracle

Source: Internet
Author: User

The JDBC Connection database process is:

1, load driver, establish connection

2, creating the SQL statement object

3, Execute SQL statement

4, processing result sets

5, close the connection

These five steps are about 4 major points of knowledge:

1, Drive management DriverManager

Classforname ("Oracle.jdbc.driver.OracleDriver")

2, Connection object

Connection interface: Responsible for the application to the database connection, after loading the driver, using Url,username,password three parameters to create a specific database connection 1

3,sql Statement Object Interface

The statement interface is used to process the SQL statement object sent to the database

4, Result set interface

ResultSet interface: A collection of results returned after executing a query SQL statement.

public void FindAll () {
Connection Con=null;
Statement Stmt=null;
ResultSet Rs=null;
try {
Class.forName ("Oracle.jdbc.OracleDriver");
Con=drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:xe";, "username", "password");
Stmt=con.createstatement ();
String sql= "Select Empno, ename, Sal, hiredate from EMP";
Rs=stmt.executequery (SQL);
while (Rs.next ()) {
System.out.println (Rs.getint ("empno") + ","
+ rs.getstring ("ename") + ","
+ rs.getdouble ("sal") + "," + rs.getdate ("HireDate"));
}
} catch (ClassNotFoundException e) {
System.out.println ("Driver class cannot be found!");
throw new RuntimeException (e);
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Database access Exception!");
throw new RuntimeException (e);
}finally {
try {
if (rs! = null) {
Rs.close ();
}
if (stmt! = null) {
Stmt.close ();
}
if (con! = null) {
Con.close ();
}
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Exception occurred while closing the connection");
}
}
}

JDBC Connect Oracle

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.