android jdbc odbc connection

2019-03-01 00:15发布

问题:

i want to connect odbc connection to my android application. my database is oracle 10g. Here in my code my table name is world. After compiling my program and close the emulator open table in oracle database the values could not be stored

The same coding i compiled normal javac compiler in cmp prompt the values should be stored, if i compiling in android application in eclipse ide the values could not be stored.Pls give one solution or any changes in my code Thanks in advance package com.odbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import android.app.Activity;
import android.os.Bundle;
import android.R.id;

public class OdbcActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try
    {
        String country="india";
        String city="delhi";
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:world","system","love");
        PreparedStatement ps=con.prepareStatement("insert into world(country,city) values(?,?)");
        ps.setString(1,country);
        ps.setString(2,city);
        ps.executeUpdate();



    }
    catch(Exception e)
    {
        System.out.println("Exception:"+e);
    }
}

}

回答1:

Android apps connecting directly to a DMBS isn't an architecture I'd recommend. Instead, I suggest setting an application on the server (using Java, Rails, whatever) that reads the database and exposes a simple HTTP web service for the Android app to get at the data. Not only is that a more sound architecture, I'm not even sure you can run JDBC drivers in an Android device. Using my architecture, you isolate the device from the structure and type of your database.