繁体   English   中英

为什么无法将Android App与Oracle数据库连接?

[英]Why can't connect Android App with Oracle database?

我将ojdbc14.jar和oraclepki.jar添加到项目的libs文件夹中,这是android项目的MainActivity.java:

package com.example.testoracle;

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

import android.app.Activity;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            String userName = getDataFromOraDB();
            TextView tv = new TextView(this);
            tv.setText("Here is the name : "+userName);
            setContentView(tv);
            } catch (SQLException e) {
            Toast.makeText(this, "1st toast : "+e.getMessage(), Toast.LENGTH_LONG).show();
            } catch (ClassNotFoundException e) {
            Toast.makeText(this, "second toast : "+e.getMessage(), Toast.LENGTH_LONG).show();
            }
    }

    public String getDataFromOraDB() throws SQLException,
    ClassNotFoundException {
        String name = null;
        String jdbcURL = "jdbc:oracle:thin:@//localhost:1521:oracl";
        String user = "SYSTEM";
        String passwd = "root";
        // Load the Oracle JDBC driver

        try {
            Log.w("MyApp","Try");
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            Connection conn;
            ResultSet rs;
            Statement stmt;
            conn = DriverManager.getConnection(jdbcURL, user, passwd);
            stmt = conn.createStatement();
            Log.w("MyApp","Avant query");
            rs = stmt.executeQuery("select Name from table_people");
            Log.w("MyApp","Apres query");
            if (rs.next()) {
                name = rs.getString("Name");
            }
        } catch (java.sql.SQLException e) {
            // Auto-generated catch block
            System.out.println("the exception is : " + e.toString());
        }

        Toast.makeText(getApplicationContext(), "3rd toast : "+name, Toast.LENGTH_LONG).show();
        return name;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

清单确实具有INTERNET PERMISSION

<uses-permission android:name="android.permission.INTERNET" />

这是我在logcat中得到的

06-01 15:16:34.142: W/MyApp(402): Try
06-01 15:16:34.373: I/dalvikvm(402): Failed resolving Loracle/jdbc/xa/OracleXAResource; interface 927 'Ljavax/transaction/xa/XAResource;'
06-01 15:16:34.373: W/dalvikvm(402): Link of class 'Loracle/jdbc/xa/OracleXAResource;' failed
06-01 15:16:34.373: W/dalvikvm(402): Unable to resolve superclass of Loracle/jdbc/xa/client/OracleXAResource; (1341)
06-01 15:16:34.373: W/dalvikvm(402): Link of class 'Loracle/jdbc/xa/client/OracleXAResource;' failed
06-01 15:16:34.373: W/dalvikvm(402): Unable to resolve superclass of Loracle/jdbc/driver/T4CXAResource; (1348)
06-01 15:16:34.373: W/dalvikvm(402): Link of class 'Loracle/jdbc/driver/T4CXAResource;' failed
06-01 15:16:34.373: W/dalvikvm(402): VFY: unable to find class referenced in signature (Loracle/jdbc/driver/T4CXAResource;)
06-01 15:16:34.373: I/dalvikvm(402): Failed resolving Loracle/jdbc/xa/OracleXAResource; interface 927 'Ljavax/transaction/xa/XAResource;'
06-01 15:16:34.383: W/dalvikvm(402): Link of class 'Loracle/jdbc/xa/OracleXAResource;' failed
06-01 15:16:34.383: W/dalvikvm(402): Unable to resolve superclass of Loracle/jdbc/xa/client/OracleXAResource; (1341)
06-01 15:16:34.383: W/dalvikvm(402): Link of class 'Loracle/jdbc/xa/client/OracleXAResource;' failed
06-01 15:16:34.383: W/dalvikvm(402): Unable to resolve superclass of Loracle/jdbc/driver/T4CXAResource; (1348)
06-01 15:16:34.383: W/dalvikvm(402): Link of class 'Loracle/jdbc/driver/T4CXAResource;' failed
06-01 15:16:34.383: I/dalvikvm(402): Could not find method oracle.jdbc.driver.T4CXAResource.setPasswordInternal, referenced from method oracle.jdbc.driver.T4CConnection.getPasswordInternal
06-01 15:16:34.383: W/dalvikvm(402): VFY: unable to resolve virtual method 10574: Loracle/jdbc/driver/T4CXAResource;.setPasswordInternal (Ljava/lang/String;)V
06-01 15:16:34.533: I/dalvikvm(402): Failed resolving Loracle/jdbc/pool/OracleDataSource; interface 850 'Ljavax/naming/Referenceable;'
06-01 15:16:34.533: W/dalvikvm(402): Link of class 'Loracle/jdbc/pool/OracleDataSource;' failed
06-01 15:16:34.533: I/dalvikvm(402): Could not find method oracle.jdbc.pool.OracleDataSource.filterConnectionProperties, referenced from method oracle.jdbc.driver.PhysicalConnection.getProperties
06-01 15:16:34.533: W/dalvikvm(402): VFY: unable to resolve static method 11805: Loracle/jdbc/pool/OracleDataSource;.filterConnectionProperties (Ljava/util/Properties;)Ljava/util/Properties;
06-01 15:16:34.543: W/dalvikvm(402): VFY: unable to find class referenced in signature (Ljavax/transaction/xa/XAResource;)
06-01 15:16:35.192: I/System.out(402): the exception is : java.sql.SQLException: Io exception: The Network Adapter could not establish the connection

那么有没有办法避免“网络适配器无法建立连接”?

这可能不是您想听到的,但是....

尽管您可以通过使用正确的数据库服务器地址并摆弄其他位来使其正常工作,但实际上不应该这样做。

Oracle是服务器端技术,而Android显然是客户端。 几年前,大多数理智的人都停止了使用此类Client-Server数据库访问,这是有充分的理由的。。。到现在,这已经成为一种怀旧的玩笑了。

恕我直言,您应该使用自己选择的语言来构建服务器应用程序,该服务器应用程序向您的Android应用程序提供基于HTTP的API(XML,JSON等)。

然后,直接从您的Android应用程序使用该API,或使用该API使您的Android本地sqlite数据库与Oracle数据库的某些子集同步。

通过您最喜欢的网络搜索引擎,可以找到很多实现这两者的示例。

1.检查Oracle数据库是否已启动并正在运行。

2.-如果正在运行,请检查您的连接字符串是否正确,并根据数据库信息更改连接字符串。 a)转到您的本地主机: https:// localhost:1158 / em b)登录用户名密码以->普通身份连接c)在“常规”下面,单击LISTENER_localhost查看您的端口号

Net Address (ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1522)) Connect to port 1522 

3.-检查是否没有防火墙。

另外,由于android会进行网络调用,因此您不应在主线程中进行此操作,而应使用线程或AsyncTask进行连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM