For connecting with the Oracle database from JAVA application, you need to follow 5 steps to perform database connectivity. Below are connection information specific to oracle database:
- Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
- Connection URL: The connection URL for the oracle database is jdbc:oracle:thin:@localhost:1521:FacingIssuesOnITDB here jdbc is the API, oracle is the database, localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port number and FacingIssuesOnITDB is the database name. We may use any database, in such case, you need to replace the FacingIssuesOnITDB with your database name.
- Username: The default username for the oracle database is root.
- Password: Password is given by the user at the time of installing the oracle database. In this example, we are going to use root as the password.
import java.sql.*;
class OracleConnection{
public static void main(String args[]){
try{
//Step 1: Register the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//Step 2: Create connection with database
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin@localhost:1521:FacingIssuesOnITDB","root","root");
//here FacingIssuesOnITDB is database name, root is username and password
//Step 3: Create statement
Statement stmt=con.createStatement();
//Step 4: Execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
//Step 5: close connection
con.close();
}
catch(ClassNotFoundException e){
System.out.println(e);
}
catch(SQLException e){
System.out.println(e);
}
}
Here you learn about steps to connect database with application and specific configuration information for Oracle.
See also :
Learn More on JDBC
Follow below links to learn more on JDBC and solving JDBC related issues :
You must be logged in to post a comment.