Solutions

Also View:

Tuesday, 16 January 2018

How to connect Android App with Oracle Database

You can not directly access the Oracle database from android Application. you need to create webservices in PHP, .net or in Java to make connection with Oracle Database. After creating this webservice , you need to connect your application with the webservice.

Or, use a Virtual JDBC Driver that uses a three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code.

This is the simplest way of connecting with Oracle Database. 

To connect oracle database using JDBC you need to download and sync ojdbc14.jar.
you can review my following post.

Now apply following code to connect oracle database.

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.connect_ora.MainActivity">


    <TextView
        android:id="@+id/hello"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>


JAVA

package com.example.administrator.connect_ora;

import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

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

import static oracle.net.aso.C12.e;

public class MainActivity extends AppCompatActivity {

    private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String DEFAULT_URL = "jdbc:oracle:thin:@192.168.2.32:1521:dev";
    private static final String DEFAULT_USERNAME = "scott";
    private static final String DEFAULT_PASSWORD = "tiger";

    private Connection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        TextView tv = (TextView) findViewById(R.id.hello);
        try {
            this.connection = createConnection();
            Toast.makeText(MainActivity.this, "Connected",
                    Toast.LENGTH_SHORT).show();
            Statement stmt=connection.createStatement();
            StringBuffer stringBuffer = new StringBuffer();
            ResultSet rs=stmt.executeQuery("select * from cat");
            while(rs.next()) {
                stringBuffer.append( rs.getString(1)+"\n");
            }
            tv.setText(stringBuffer.toString());
            connection.close();
        }
        catch (Exception e) {

            Toast.makeText(MainActivity.this, ""+e,
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {

        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }

    public static Connection createConnection() throws ClassNotFoundException, SQLException {
        return createConnection(DEFAULT_DRIVER, DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
    }

}



23 comments:

  1. Do you have a full tutorial on how to make the connection from android studio to oracle apex on cloud?? i really need help on this please???

    ReplyDelete
  2. Will this code automatically connect the android app to Oracle Database?

    ReplyDelete
  3. Thanks! I went through a lot of online resources and only this worked in the end.

    ReplyDelete
    Replies
    1. I've posted the full Android Studio project here, building from the code above: https://github.com/cseas/android-ojdbc

      Delete
  4. Hi i undestand this is the easiest way to connect to oracle.
    But if i need a safe option that support multiple users conection. what should i do?

    ReplyDelete
  5. its showing me error : SQLException- Network Adapter could not establish connection. Please help

    ReplyDelete
  6. Perfect this actually work thanks you actually saved my ass...dude...please upload full tutorial about how to fetch data and all....

    ReplyDelete
  7. java.sql.SQLException:The Network Adapter could not establish the connection

    ReplyDelete
  8. Thanx again...this is the second time you saved my ass

    ReplyDelete
  9. Hi, thanks for github demo. small question, can i access oracle db from android with oracle 18c using android 8.1 ? from few days of testing i see that only ojdbc14.jar is working and what i understand it only support oracle 11g version, am i right ?

    ReplyDelete
  10. Hi,
    i get an error message 'cannot find Symbol static e'.
    I think the error is by this Import 'import static oracle.net.aso.C12.e;'.

    Can someone help me?
    THX

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! via app cloner

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... hoverwatch cost

    ReplyDelete
  15. That is really nice to hear. thank you for the update and good luck. HDfilme

    ReplyDelete
  16. How can i connect with localhost? I tried putting locahost instead of the ip but it didnt work ie. private static final String DEFAULT_URL = "jdbc:oracle:thin:@localhost:1521:orcl";

    ReplyDelete
  17. Connexion baym java Oracle usgin Android studio

    ReplyDelete
  18. Connexion bayn Oracle usgin Android studio

    ReplyDelete
  19. is this code work with latest android studio

    ReplyDelete