Eclipse is an open source, java-based, extensible development platform. Eclipse comes with a standard set of plug-ins, including Java development tools (Java Development kit,jdk).
1. First set up a project DB, right click DB, select Build Path-->configure built Path into
Through Add External JARs. Select D:orclapphrproduct11.2.0dbhome_1owbwflib under the Ojdbc14.jar, click OK.
2. Preparation of Connectoracle.java documents
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 not be valid
Private String URL = "Jdbc:oracle:thin: @hr-PC:1521:ORCL"; hr-pc and 1521 are critical, i.e. there is no this in the listening string,
D:orclapphrproduct11.2.0dbhome_1networkadmintnsnames.ora This file is not, must be based on Tnsnames.ora this file to configure the URL path, otherwise there will be error!!!
Public Connectoracle () {
try{
Class.forName (ClassName);
SYSTEM.OUT.PRINTLN ("Load Database driver successful!");
}catch (ClassNotFoundException e) {
SYSTEM.OUT.PRINTLN ("Load Database driver failed!");
E.printstacktrace ();
}
}
/** CREATE DATABASE Connection * *
Public Connection Getcon () {
try {
Con=drivermanager.getconnection (Url,user,password);
SYSTEM.OUT.PRINTLN ("CREATE DATABASE connection succeeded!");
catch (SQLException e) {
System.out.print (con);
SYSTEM.OUT.PRINTLN ("CREATE DATABASE connection Failed!");
Con=null;
E.printstacktrace ();
}
return con;
}
public void closed () {
try{
if (con!=null) {
Con.close ();
}
}catch (SQLException e) {
System.out.println ("Close Con object failed!");
E.printstacktrace ();
}
}
public static void Main (string[] args)
{
Connectoracle c=new connectoracle ();
C.getcon ();
C.closed ();
}
}
3. Operation Result: