Search This Blog

Total Pageviews

JDBC connection Sample code.

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 ;
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
conn.close() ;
}catch(Exception e){
System.out.println(e) ;
conn.close() ;

No comments:

MongoDB Tutorial MongoDB Tutorial Table of Contents Introduction to MongoDB Installation Creating a Database Creati...