简体   繁体   中英

Why can't connect Android App with Oracle database?

I added ojdbc14.jar and oraclepki.jar to the libs folder of the project, and here is the android project's 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;
    }
}

The manifest does have the INTERNET PERMISSION

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

Here is what I get in the 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

So is there a way to avoid "The Network Adapter could not establish the connection" ?

This is probably not what you want to hear, but....

While you could probably get this working by using the correct database server address, and fiddling around with a bunch of other bits, you really shouldn't.

Oracle is a server-side technology, and Android, obviously, is a client. Most sane people stopped using this sort of Client-Server database access quite a number of years ago for very good reasons...to the point where it's pretty much a nostalgic joke now.

IMHO, you should build a server application, in the language of your choice, that presents an HTTP-based API (XML, JSON, etc.) to your Android application.

Then, either use the API directly from your Android app, or use the API to keep your Android's local sqlite database synched with some subset of the Oracle database.

There's lots of examples of doing both available through your favorite web search engine.

1.-Check if your Oracle database is up and running.

2.- If it is up an running check that your connection string is correct and change your connection string according to the database information. a)go to your localhost: https://localhost:1158/em b)login user name password connect as --> normal c)Below 'General' click on LISTENER_localhost look at you port number

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

3.-Check if there isn't a firewall.

Also as android will do a network call, you shouldn't do this in the main thread, use a thread or an AsyncTask for the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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