Connecting Java application with oracle database is very easy just follow the below step to perform database connectivity.
In this example I am using oracle database and fetch the data of students.
For connecting Java application with oracle database 4 things needed :
1.Driver class: The driver class for the oracle database is
oracle.jdbc.driver.OracleDriver.
2.JDBC Connection URL: The JDBC connection URL for the oracle database is jdbc:oracle:thin:@localhost:1521:xe
Example: jdbc:mysql://localhost:3306/students
3.Username: Put your username if any.The default username for the mysql database is system.
4.Password: Password is given by the user at the time of installing the Oracle database. In this example, we are going to use Students@123 as the password.
5.Jar File: Download Here
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection(
“jdbc:oracle:thin:@localhost:1521:xe","system","Students@123");
//step3 create the statement object
Statement stmt=con.createStatement();
//step4 execute query
ResultSet rs=stmt.executeQuery("select * from students_data”);
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
//step 5 close the connection
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}