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