This is a sample code to connect to Oracle RDBMS using JDBC.
Here I used Oracle thin driver (Type-4 JDBC Driver) to connect to Oracle.
1. Register driver
2. Establish connection
3. Create a statement
4. Execute statement
5. Extract the results from ResultSet.
import java.sql.*;
class JdbcConnection {
public static void main (String args []) throws SQLException
{
Connection conn = null ;
try{
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection
("jdbc:oracle:thin:@ipaddress:1521:SID", "username ", "password");
// @machineName:port:SID, userid, password
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
while (rset.next())
System.out.println (rset.getString(1)); // Print col 1
stmt.close();
conn.close() ;
}catch(Exception e){
System.out.println(e) ;
conn.close() ;
}
}
}
No comments:
Post a Comment