簡體   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