Mehdi Safaei
Mehdi Safaei

Reputation: 37

connect to oracle database from android by programmatically

I'm beginner in android develop. I have a oracle database in remote system and I want to connect database from my android app. I used below code for connect but I can't connect to it (with ojdbc14.jar library). /n have you Ideas???

try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                String url = "jdbc:oracle:thin:@//192.20.xx.xxx:1521/SHOP47";

                Connection conn =
                        DriverManager.getConnection(url, "user", "pass");

                conn.setAutoCommit(false);
                Statement stmt = conn.createStatement();
                ResultSet rset =
                        stmt.executeQuery("select count(1) from GOODS");
                while (rset.next()) {
                    Toast.makeText(getBaseContext(),rset.getString(1).toString(), Toast.LENGTH_SHORT).show();

                }
                stmt.close();
            }
            catch(Exception e){
             Log.d(TAG,e.getMessage());
            }

Upvotes: 0

Views: 48

Answers (1)

Abhishek Jain
Abhishek Jain

Reputation: 888

Actually, you shouldn't do this directly from an android application, because:

  1. Android applications are really easy to decompile or to reverse engineer, and the client or the person decompiling will have credentials to access your database. If using the right hacking tools like Backtrack, then this malicious client can access, connect and exploit the data in your database.
  2. If your application is for clients all around the world, then the clients should open and maintain a connection to your database per operation or set of operations. Opening a physical database connection takes a lot of time and resources, even when your pc client is in a LAN next to the database engine server.

A better solution would be to use a service-oriented architecture where you will have at least two applications:

  1. Service provider application. This application will create and publish web services (preferably RESTful) and may establish policies to consume the web services like user authentication and authorization. This application will also connect to the database and execute CRUD operations against it.
  2. Service consumer application. This would be your Android (or any other mobile) application.

In Android, you can create a Restful Service Layer that will have methods for each service.

Upvotes: 2

Related Questions