如何将android连接到oracle数据库?

时间:2016-10-05 16:33:00

标签: java android oracle oracle11g

我在互联网上搜索并找到使用Oracle数据库移动服务器或其他东西的解决方案,但我的任务只是使用ojdbc。我做了所有这些事情,但它没有用:

  • 使用ojdbc14.jar和oraclepki.jar(我使用了最后一个jar,因为它在oracle.security.pki.OracleWallet上给了我一个ClassNotFoundException)
  • 用tnsnames.ora和listener.ora
  • 中的ip地址替换localhost
  • 启动oracle服务

这是它在Oracle Sql Developer中看起来的连接方式:  安德烈@ //本地主机:1521 / ORCL

这是我的代码:

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
     System.out.println(getDataFromDB());
    } catch (SQLException e) {
      // 
    }
  }

  public int getDataFromDB() throws SQLException {
    String jdbcURL = "jdbc:oracle:thin:@" + ip + ":1521:orcl";
    String user = "andr";
    String passwd = "andr";
    int age = 0;
    try {
      DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
      Connection conn = DriverManager.getConnection(jdbcURL, user, passwd);
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery("select age from person");

      if (rs.next()) {
        age = rs.getInt("age");
      }
    } catch (java.sql.SQLException e) {
      System.out.println("the exception is : " + e.toString());
    }

    return age;
  }
 }

启动应用程序时会出现以下异常和错误

 I/dalvikvm(1901): Failed resolving Loracle/jdbc/xa/OracleXAResource; interface 360 'Ljavax/transaction/xa/XAResource;'
 W/dalvikvm(1901): Link of class 'Loracle/jdbc/xa/OracleXAResource;' failed
 W/dalvikvm(1901): Unable to resolve superclass of Loracle/jdbc/xa/client/OracleXAResource; (771)
 W/dalvikvm(1901): Link of class 'Loracle/jdbc/xa/client/OracleXAResource;' failed
 W/dalvikvm(1901): Unable to resolve superclass of Loracle/jdbc/driver/T4CXAResource; (778)
 W/dalvikvm(1901): Link of class 'Loracle/jdbc/driver/T4CXAResource;' failed
 W/dalvikvm(1901): VFY: unable to find class referenced in signature (Loracle/jdbc/driver/T4CXAResource;)
  I/dalvikvm(1901): Could not find method oracle.jdbc.driver.T4CXAResource.setPasswordInternal, referenced from method oracle.jdbc.driver.T4CConnection.getPasswordInternal
 W/dalvikvm(1901): VFY: unable to resolve virtual method 6237: Loracle/jdbc/driver/T4CXAResource;.setPasswordInternal (Ljava/lang/String;)V
 D/dalvikvm(1901): VFY: replacing opcode 0x6e at 0x0002
 I/dalvikvm(1901): Failed resolving Loracle/jdbc/pool/OracleDataSource; interface 279 'Ljavax/naming/Referenceable;'
 W/dalvikvm(1901): Link of class 'Loracle/jdbc/pool/OracleDataSource;' failed
 I/dalvikvm(1901): Could not find method oracle.jdbc.pool.OracleDataSource.filterConnectionProperties, referenced from method oracle.jdbc.driver.PhysicalConnection.getProperties
 W/dalvikvm(1901): VFY: unable to resolve static method 7465: Loracle/jdbc/pool/OracleDataSource;.filterConnectionProperties (Ljava/util/Properties;)Ljava/util/Properties;
 D/dalvikvm(1901): VFY: replacing opcode 0x71 at 0x0002
 W/dalvikvm(1901): VFY: unable to find class referenced in signature (Ljavax/transaction/xa/XAResource;)
 D/AndroidRuntime(1901): Shutting down VM
 W/dalvikvm(1901): threadid=1: thread exiting with uncaught exception (group=0xaccbeb20)
 D/dalvikvm(1901): GC_FOR_ALLOC freed 232K, 9% free 2997K/3288K, paused 2ms, total 2ms
 E/AndroidRuntime(1901): FATAL EXCEPTION: main
 E/AndroidRuntime(1901): Process: com.example.test, PID: 1901
 E/AndroidRuntime(1901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: 
 E/AndroidRuntime(1901):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
 E/AndroidRuntime(1901):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 E/AndroidRuntime(1901):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
 E/AndroidRuntime(1901):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 E/AndroidRuntime(1901):    at android.os.Handler.dispatchMessage(Handler.java:102)
 E/AndroidRuntime(1901):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(1901):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(1901):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1901):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1901):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(1901):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1901):    at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(1901): Caused by: android.os.NetworkOnMainThreadException
 E/AndroidRuntime(1901):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
 E/AndroidRuntime(1901):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
 E/AndroidRuntime(1901):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
 E/AndroidRuntime(1901):    at libcore.io.IoBridge.connect(IoBridge.java:112)
 E/AndroidRuntime(1901):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
 E/AndroidRuntime(1901):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
 E/AndroidRuntime(1901):    at java.net.Socket.startupSocket(Socket.java:567)
 E/AndroidRuntime(1901):    at java.net.Socket.tryAllAddresses(Socket.java:128)
 E/AndroidRuntime(1901):    at java.net.Socket.<init>(Socket.java:178)
 E/AndroidRuntime(1901):    at java.net.Socket.<init>(Socket.java:150)
 E/AndroidRuntime(1901):    at oracle.net.nt.TcpNTAdapter.connect(Unknown Source)
 E/AndroidRuntime(1901):    at oracle.net.nt.ConnOption.connect(Unknown Source)
 E/AndroidRuntime(1901):    at oracle.net.nt.ConnStrategy.execute(Unknown Source)
 E/AndroidRuntime(1901):    at oracle.net.resolver.AddrResolution.resolveAndExecute(Unknown Source)
 E/AndroidRuntime(1901):    at oracle.net.ns.NSProtocol.establishConnection(Unknown Source)
 E/AndroidRuntime(1901):    at oracle.net.ns.NSProtocol.connect(Unknown Source)
 E/AndroidRuntime(1901):    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:844)
 E/AndroidRuntime(1901):    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:268)
 E/AndroidRuntime(1901):    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
 E/AndroidRuntime(1901):    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
 E/AndroidRuntime(1901):    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
 E/AndroidRuntime(1901):    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
 E/AndroidRuntime(1901):    at java.sql.DriverManager.getConnection(DriverManager.java:179)
 E/AndroidRuntime(1901):    at java.sql.DriverManager.getConnection(DriverManager.java:213)
 E/AndroidRuntime(1901):    at com.example.test.MainActivity.getDataFromOraDB(MainActivity.java:36)
 E/AndroidRuntime(1901):    at com.example.test.MainActivity.doStuff(MainActivity.java:18)
 E/AndroidRuntime(1901):    at com.example.test.MainActivity.onCreate(MainActivity.java:56)
 E/AndroidRuntime(1901):    at android.app.Activity.performCreate(Activity.java:5231)
 E/AndroidRuntime(1901):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 E/AndroidRuntime(1901):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 E/AndroidRuntime(1901):    ... 11 more

有人能帮助我吗?

0 个答案:

没有答案