1. Connection Code
Package Util;
Import java.sql.Connection;
Import java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import java.sql.ResultSet;
Import java.sql.SQLException;
Public class DBUtil {
Public static void main (String args[]){
Try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user = "system";
String password = "password";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection connection = null;
Try {
Connection = DriverManager.getConnection(url,user,password);
System.out.println("Connection succeeded");
} catch (SQLException e) {
System.out.println("Connection failed");
e.printStackTrace();
}
}
Public static void close(Connection connection ) {
Try {
If (connection != null) {
Connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Public static void close(PreparedStatement preparedStatement ) {
Try {
If (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Public static void close(ResultSet resultSet ) {
Try {
If (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2. Load Driver
Bulid path->add External JARs find App\administrator\product\11.2.0\dbhome_1\jdbc\lib\ojbc6_g.jar under the Oracle installation path
3. Under Web-int, the Lib replicates a Ojbc6_g.jar
Connecting to an Oracle database