oracle 在android中连接到oracle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5015592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 22:46:39  来源:igfitidea点击:

Connecting to oracle in android

javaandroidoraclejdbc

提问by PDStat

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

所以人们可能会告诉我这是一个坏主意,但我想至少试一试。

EDITThe 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.

编辑这个应用程序的目的是它只能在设备是 oracle db 所在的同一网络的一部分或通过 VPN 连接到网络时才能工作。数据库中的信息不会被全局访问,这就是为什么我需要直接连接到 oracle db。

Now according to this thread

现在根据这个线程

Connecting the oracle in android application

在android应用程序中连接oracle

He was successful in querying the oracle db.

他成功查询了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

在我的主要活动 onCreate 方法中,我有这个

@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);          
    } catch(Exception e) {

    }
}

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

在 Eclipse 中,我将外部 ojdbc14.jar 文件添加到构建路径。

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"

“Io 异常:网络适配器无法建立连接”

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

但是,如果我使用 main 方法在标准 java 应用程序中创建此类的实例,则连接有效。有任何想法吗?

采纳答案by Juri

:) 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.

:) 是的,我会告诉你这是一个“坏”主意。恕我直言,鉴于 Android 应用程序旨在在连接可能出现问题或暂时丢失的移动设备上运行,我声称每个好的应用程序都应该具有一定程度的离线功能。因此,您将实现一些非常基本的同步机制——例如用SampleSyncAdapter演示的——它与应用程序本地 SQLite 数据库同步。

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

我认为这是最好的方法(也是为了用户体验)。

回答by Eric Jensen

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

Oracle 有一个产品,它提供与 Oracle DB 的数据同步以及离线功能;它叫做移动服务器

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!"

他们的工作方式是,您只需将数据存储在 SQLite 中,Oracle 客户端将处理同步。当然,您必须安装和配置移动服务器,并且需要配置每个新设备,但是一旦完成,它就“正常工作了!”

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

埃里克,甲骨文产品经理

回答by Eriatolc

Have you added in the AndroidManifest.xml the

您是否在 AndroidManifest.xml 中添加了

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

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.)

我必须创建一个具有相同连接和数据库约束的应用程序。我已经这样做了,并且我有很多异常(在连接到数据库期间的 ArrayIndexOutOfBound。)

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

我为此使用 ojdbc14.jar。所以,我“只需要”修复我的异常,就可以了……

For more information, see this topic

有关更多信息,请参阅此主题

回答by erebel55

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.

很抱歉恢复旧线程,但我遇到这个问题很长时间并最终解决了它。只需运行 eclipse 或您在“以管理员身份”开发的任何内容,错误就会消失。

回答by kaw

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.

另一种更简单的方法是使用依赖于安全三层架构的虚拟 JDBC 驱动程序:您的 JDBC 代码通过 HTTP 发送到远程 Servlet,该 Servlet 过滤 JDBC 代码(配置和安全),然后再将其传递给 Oracle JDBC司机。结果通过 HTTP 发回给您。

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

有一些免费软件使用这种技术。只是谷歌“Android JDBC Driver over HTTP”。