开发者

Connecting to an OracleDB via an Android app

开发者 https://www.devze.com 2023-02-27 00:49 出处:网络
Thans for looking, I hope you could help me. Currently, I want to get some data from an OracleDB server (on the LAN) from my Android app. I\'m using JDBC with the ojdbc14.jar and the following code i

Thans for looking, I hope you could help me.

Currently, I want to get some data from an OracleDB server (on the LAN) from my Android app. I'm using JDBC with the ojdbc14.jar and the following code in my Android app and the stackTrace I have with the logcat :

http://pastebin.archlinux.fr/432118

As you can see, there is a big exception, and I'm not be able to fix it...

Has someone already succeeded in a OracleDB connection with his Android开发者_如何学运维 app and without webservices ? Could someone help me fixing this Exception ?

For information : I've tried to change the ojdbc driver (the worst thing I've ever done >.>), and I checked the URL validity.

Thanks for helping...

EDIT : The application will have to get data from the OracleDB, and store it on the local SQLite DB of Android, because the Android device will be disconnected of the LAN (and I don't want to make the data accessible from web [3G]). Once disconnected, the app will work with the local data on SQLite. When the user's activity is finished, the device will be reconnected to the LAN and the app syncs the edited SQLite local data with the main Oracle DB server. SQLite <-- local --> App <-- when connected/sync ---> OracleDB


Oracle actually has a product specifically designed for syncing the Oracle Database with mobile devices. It's called mobile server.

However the usage model is slightly different from what you're describing; instead of connecting directly to Oracle Database, you would use a local Berkeley DB or SQLite database, and then mobile server would sync that with the Oracle Database.

It can run as a separate process that automatically handles sync, or you can use API calls to control sync from within your program. If that sounds like something that could be useful to you, check it out here.

You can download it from the download tab and try it out.

Best of luck with solving your problem.

Regards

Eric, Oracle PM


I've found the answer ! The product Oracle Database Lite is the solution. I explain...

The Oracle Database Lite is a big product, with an unreadable documentation. Impossible for me to understand how it works. But, I've tried to install it. And, in the install folders, there is a jdbc folder.

There, you will find an ojdbc14.jar. Use it in your project instead of the ojdbc14.jar found on the classical Oracle webpage. And it works !

You will be able to connect an Oracle DB via your Android app, using JDBC.

Thanks for all, Best regards,

Eriatolc


ORACLE DATABASE CONNECTION WITH ANDROID THROUGH LAN

Grant Some Manifest Permissions

<permission
        android:name="info.android.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="info.android.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

MainActivity Class

    package example.com.myapplication;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import android.os.StrictMode;

    public class MainActivity extends AppCompatActivity {

    private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String DEFAULT_URL = "jdbc:oracle:thin:@192.168.0.1:1521:xe";
    private static final String DEFAULT_USERNAME = "system";
    private static final String DEFAULT_PASSWORD = "oracle";

    private Connection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        TextView tv = (TextView) findViewById(R.id.hello);


                try {
                    this.connection = createConnection();
                    e.Log("Connected");
                    Statement stmt=connection.createStatement();

                    ResultSet rs=stmt.executeQuery("select * from cat");
                    while(rs.next()) {
                        System.out.println("hello : " + rs.getString(1));
                    }
                    connection.close();
                }
                catch (Exception e) {
                    e.Log(""+e);
                    e.printStackTrace();
                }
            }

            public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {

                Class.forName(driver);
                return DriverManager.getConnection(url, username, password);
            }

            public static Connection createConnection() throws ClassNotFoundException, SQLException {
                return createConnection(DEFAULT_DRIVER, DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
            }
        }

Prerequisite are: Note there is no need to add dependency lib ojdbc14.jar just copy ojdbc14.jar to your JAVA_HOME jre -> lib -> ext & paste here ojdbc14.jar then first manually check jdbc connection by cmd/terminal make any simple java program http://www.javatpoint.com/example-to-connect-to-the-oracle-database

0

精彩评论

暂无评论...
验证码 换一张
取 消