Connect to the Oracle database via JDBC

on 1:49 PM

For connecting Java application with the Oracle Database :

Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
Connection URL: The connection URL for the oracle10G database is 

jdbc:oracle:thin:@localhost:1521:xe 


 jdbc : API, 

oracle : database, 
thin : driver, 
localhost : is the server name on which oracle is running, we may also use IP address, 
1521 : is the port number 
XE : is the Oracle service name. 

You may get all these informations from the tnsnames.ora file.


Username: The default username for the oracle database is system.

Password: Password is given by the user at the time of installing the oracle database.


In this example, system is the username and oracle is the password of the Oracle database.
  1. import java.sql.*;  
  2. class OracleCon{  
  3. public static void main(String args[]){  
  4. try{  
  5. //step1 load the driver class  
  6. Class.forName("oracle.jdbc.driver.OracleDriver");  
  7.   
  8. //step2 create  the connection object  
  9. Connection con=DriverManager.getConnection(  
  10. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  11.   
  12. //step3 create the statement object  
  13. Statement stmt=con.createStatement();  
  14.   
  15. //step4 execute query  
  16. ResultSet rs=stmt.executeQuery("select * from emp");  
  17. while(rs.next())  
  18. System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
  19.   
  20. //step5 close the connection object  
  21. con.close();  
  22.   
  23. }catch(Exception e){ System.out.println(e);}  
  24.   
  25. }  
  26. }  

1 comments:

Unknown said...



Hi,
we are the best institute in hyderabad well we offer courses oracle fusion scm and oracle fusion procurement with online and class room trainings
for more information
http://www.erptree.com/course/oracle-fusion-procurement-online-training-in-kolkata/

Post a Comment