Connecting to Oracle Server via Different programming Languages

In order to store or access the data inside an Oracle database, you first need to connect to the Oracle database server. We will show you the sample codes to connect your Oracle via Java and Python.

Connecting via Java

Before you start, you need to add Oracle connector package to your Java project. The download link for the Oracle connector is

package oracle_connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleConnection {

    public static void main(String[] args) {
        Connection conn = null;
        String dbServer = ""; // change it to your database server name 
        int dbPort = 4229; // change it to your database server port
        String userName = "your database user name";
        String password = "your database password";
        String url = String.format("jdbc:oracle:thin:@%s:%d:xe", dbServer, dbPort);
        try {

            conn = DriverManager.getConnection(url, userName, password);
            Statement stmt = conn.createStatement();

            // create table
            String sql = "create table teacher(NO char(20), name varchar(20),primary key(NO))";
            int result = stmt.executeUpdate(sql);

            // insert data
            if (result != -1) {
                sql = "insert into teacher(NO,name) values('202001','ben')";
                result = stmt.executeUpdate(sql);
                sql = "insert into teacher(NO,name) values('202002','ethan')";
                result = stmt.executeUpdate(sql);

            // query data
            sql = "select * from teacher";
            ResultSet rs = stmt.executeQuery(sql);
            while ( {
                System.out.println(rs.getString(1) + "\t" + rs.getString(2));
        } catch (SQLException e) {
            System.out.println("Oracle Server connection had an exception");
        } catch (Exception e) {
        } finally {
            try {
            } catch (SQLException e) {


Connecting via Python

Install cx_Oracle

pip install cx_Oracle

Connect to your server

import cx_Oracle

class OracleConnection(object):
    def __init__(self):
        self.SERVER = 'connect server'
        self.PORT = 'server port' # your port
        self.USER = 'login user'
        self.PASSWORD = 'user password'
        self.DATABASE = 'XE'

    def connect_oracle(self):
        conn = cx_Oracle.connect(self.USER, self.PASSWORD,
        '{}:{}/{}'.format(self.SERVER, self.PORT, self.DATABASE),
        return conn

    def operate_database(self):
        # example select login user
        connect = self.connect_oracle()
        curs = connect.cursor()
        sql = "select tablespace_name from dba_tablespaces"
        row = curs.fetchone()

if _name_ == '__main__':
Copyright © 2021 Cloud Clusters Inc. all right reserved,powered by GitbookRevised on 05/07/2021

results matching ""

    No results matching ""