开发者

Connecting to oracle in android

So people are probably going to tell me this is a bad idea, but I'd like to at least give it a go.

EDIT The intention of this app is that it can only work when the device is part of the same network the oracle db is on or is connected to the network via VPN. The information in the database is not going to be globally accessible, which is why I will need direct connection to the oracle db.

Now according to this thread

Connecting the oracle in android application

He was successful in querying the oracle db.

So I have a fairly basic class that when initialised will try to get a connection to my database.

package com.producermobile;

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

import android.util.Log;

public class ConnectOra {
    private Connection conn;
    private Statement stmt;
    public ConnectOra() throws ClassNotFoundException {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@x.x.x.x:1521:PR10";
            this.conn = DriverManager.getConnection(url,"xxx","xxx");
            this.conn.setAutoCommit(false);
            this.stmt = this.conn.createStatement();
        } catch(SQLException e) {
            Log.d("tag", e.getMessage());
        }       
    }
    public ResultSet getResult() throws SQLException {
        ResultSet rset = stmt.executeQuery("select customer from customers");
        stmt.close();
        return rset;            
    }
}

And in my main activity onCreate method I have this

@Override
public void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        ConnectOra db = new ConnectOra();
        ResultSet rs = db.getResult();
        ArrayList<String> list = new ArrayList<String>();
        while(rs.next()) {
            list.add(rs.getString(1));
        }
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);       开发者_JAVA百科   
    } catch(Exception e) {

    }
}

In Eclipse I add the external ojdbc14.jar file to the build path.

However when I run

this.conn = DriverManager.getConnection(url,"xxx","xxx");

I receive the following exception

"Io exception: The Network Adapter could not establish the connection"

If however I create an instance of this class inside a standard java app with a main method, the connection works. Any ideas?


Oracle has a product that provides both data sync with Oracle DB as well as offline capabilities; it's called Mobile Server

They way it works is, you just store your data in SQLite, and the Oracle client will handle sync. Of course you have to install and configure mobile server, and each new device will need to be provisioned, but once that is done, it "just works!"

There is a download tab in the link above, you can download and try it out of you like.

I hope that helps. Good luck with your problem.

Regards

Eric, Oracle PM


:) yes, I'm one that'll tell u it is a "bad" idea. IMHO, given that Android apps are intended to run on mobiles where connectivity might be an issue or be lost temporarily, I claim each good app should have some degree of offline capabilities. So you'd implement some very basic sync mechanism - as for instance demonstrated with the SampleSyncAdapter - which synchronizes with the apps local SQLite db.

I think this is the best way to go (also for the user experience).


Have you added in the AndroidManifest.xml the

uses-permission android:name="android.permission.INTERNET"

I've to create an app with the same constraint of connectivity and database. I've done this, and I've a lot of Exception (ArrayIndexOutOfBound during the connexion to the database.)

I use ojdbc14.jar for that. So, I "just have to" fix the Exception I have, and it would be OK...

For more information, see this topic


Sorry to revive an old thread but I had this problem for a long time and finally fixed it. Just run eclipse or whatever you're developing in "as administrator" and the error will go away.


Another and much easier approach is to use a Virtual JDBC Driver that relies on a secure three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code (configuration & security) before passing it to the Oracle JDBC Driver. The result is sent you back through HTTP.

There are some free software that use this technique. Just Google "Android JDBC Driver over HTTP".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜