- Published on
How to connect to oracle database xe using ojdbc drivers
- Authors
- Name
- Nikhil Reddy Avuthu
- @anr1920
Before we get stared make sure you have ojdbc drivers with you you can get the latest jdbc drivers from here
Now in your java project copy and paste the jdbc driver file into lib folder
Here is a gist with the java file template to connect to the oracle database xe using ojdbc drivers you can easily modify this for your needs.
//Step 1 : import java.sql package
import java.sql.*;
public class jdbctemplate {
public static void main(String[] args) throws Exception {
try {
// Step 2 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
String dbUserName = "system";// your user name goes here
String dbUserPassword = "nikhil";// your password goes here
// step 3 create the connection object
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", dbUserName,
dbUserPassword);
// step 4 create the statement object
Statement stmt = con.createStatement();
/*
* step 5 Now the connection has been established succesfully Now you can
* execute your necessary commands here
*/
ResultSet rs = stmt.executeQuery("select * from student");
while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getInt(3));
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}