Graphic tutorial on connecting eclipse to oracle 11g Database
Eclipse is an open-source and Java-based scalable development platform. Eclipse comes with a standard plug-in set, including Java Development Kit (JDK ).
1. First, create a project DB, right-click the DB, and select Build Path --> Configure Build Path.
Use Add External JARs .. to select ojdbc14.jar under D: \ orcl \ app \ hr \ product \ 11.2.0 \ dbhome_1 \ owb \ wf \ lib, and click OK.
2. Compile the ConnectOracle. java File
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 be invalid
Private String url = "jdbc: oracle: thin: @ hr-PC: 1521: orcl"; hr-PC and 1521 are critical, that is, whether this is in the listener String.
D: \ orcl \ app \ hr \ product \ 11.2.0 \ dbhome_1 \ NETWORK \ ADMIN \ tnsnames. are there any records in this file? According to tnsnames. ora file to configure the url path, otherwise an error will occur !!!
Public ConnectOracle (){
Try {
Class. forName (className );
System. out. println ("database driver loaded successfully! ");
} Catch (ClassNotFoundException e ){
System. out. println ("failed to load the database driver! ");
E. printStackTrace ();
}
}
/** Create a database connection */
Public Connection getCon (){
Try {
Con = DriverManager. getConnection (url, user, password );
System. out. println ("database connection created successfully! ");
} Catch (SQLException e ){
System. out. print (con );
System. out. println ("An error occurred while creating the database connection! ");
Con = null;
E. printStackTrace ();
}
Return con;
}
Public void closed (){
Try {
If (con! = Null ){
Con. close ();
}
} Catch (SQLException e ){
System. out. println ("failed to close con object! ");
E. printStackTrace ();
}
}
Public static void main (String [] args)
{
ConnectOracle c = new ConnectOracle ();
C. getCon ();
C. closed ();
}
}
3. Running result: