在android中连接到oracle

发布于 2024-10-17 22:49:36 字数 2275 浏览 2 评论 0原文

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

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

现在根据这个线程

Connecting the oracle in android application

他查询oracle数据库成功。

所以我有一个相当基本的类,在初始化时将尝试连接到我的数据库。

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;            
    }
}

在我的主要活动 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) {

    }
}

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

但是,当我运行时,

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

我收到以下异常

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

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

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);          
    } 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我不咬妳我踢妳 2024-10-24 22:49:36

Oracle有一款产品既提供与Oracle DB的数据同步,又提供离线功能;它被称为 移动服务器

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

上面的链接中有一个下载选项卡,您可以下载并尝试一下您喜欢的。

我希望这有帮助。祝你的问题好运。

问候

Oracle 产品经理 Eric

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

表情可笑 2024-10-24 22:49:36

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

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

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

情泪▽动烟 2024-10-24 22:49:36

您是否在 AndroidManifest.xml 中添加了

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

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

我使用 ojdbc14.jar 来实现。因此,我“只需”修复我遇到的异常,就可以了...

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

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

ヤ经典坏疍 2024-10-24 22:49:36

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

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.

我们只是彼此的过ke 2024-10-24 22:49:36

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文