Java thin way to connect to Oracle database

Source: Internet
Author: User

This article describes how to connect an Oracle database by thin

1. Create Web project and copy the Ojdbc14.jar under the D:\oracle\product\10.2.0\db_1\jdbc\lib (Oracle installation directory) to the LIB path in the project Web-inf directory.

2. Create a database connection file:

Connectdb.java:

Package com.gree.db;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;

public class Connectdb {
Defining Database Connection Drivers
Private final String dbdriver = "Oracle.jdbc.driver.OracleDriver";
Define the database connection URL
Private final String Dburl = "Jdbc:oracle:thin: @localhost: 1521:ORCL";
Define database connection user name
Private final String DBUSER = "180172";
Define the database connection password
Private final String DBPSW = "180172";
Defining Database Connection Objects
PRIVATE Connection conn = null;
Error message string
Private String errmes = null;
Construction method, loading driver
Public Connectdb () {
try {
Class.forName (Dbdriver);
This.conn = Drivermanager.getconnection (Dburl, DBUSER, DBPSW);
} catch (Exception e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("Load driver failed");
Errmes = E.tostring ();
}
}
Get database connection
Public Connection getconnection () {
Return conn;
}
To close a database connection
public void Close () {
try {
Conn.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
System.out.println ("Failed to close database");
}
}

Get error message
Public String Geterrmes ()
{
return errmes;
}
Test database connection
public static void Main (String []args) {
Connectdb db = new Connectdb ();
System.out.println (Db.getconnection ());
Db.close ();
}
}

Verified that the file can connect to the Oracle database

Java thin way to connect to Oracle database

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.