This discussion is archived
7 Replies Latest reply: Apr 25, 2013 1:30 AM by 931063 RSS

android link oracle  database?????

931063 Newbie
Currently Being Moderated
oracle database:9i
jar:ojdbc6.jar
ERROR:
Could not find class 'javax.management.MBeanServer', referenced from method oracle.jdbc.driver.OracleDriver.registerMBeans


package com.example.dblink;

import android.os.Bundle;
import android.os.StrictMode;

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


import android.app.Activity;
import android.database.SQLException;

public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
     
     
super.onCreate(savedInstanceState);
try {
getDataFromOraDB();
} catch (SQLException e) {
     System.out.println("the SQLException is" + e.toString());
} catch (ClassNotFoundException e) {
     System.out.println("the ClassNotFoundException is" + e.toString());
}

}

public void getDataFromOraDB() throws SQLException,
ClassNotFoundException {

String jdbcURL = "jdbc:oracle:thin:@192.168.0.1:1521:xe";
String user = "SUPPER";
String passwd = "1234";
// Load the Oracle JDBC driver

try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(jdbcURL, user, passwd);
String sql = "insert into TEST users(A) values(?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "cc");
stmt.executeUpdate();
stmt.close();
conn.close();


} catch (java.sql.SQLException e) {

System.out.println("the exception is" + e.toString());
}

}

}
  • 1. Re: android link oracle Internal database?????
    EdStevens Guru
    Currently Being Moderated
    928060 wrote:
    oracle database:9i
    A database version whose death certificate is older than Android's birth certificate ....

    jar:ojdbc14.jar
    ERROR:http://ppt.cc/FW3L

    package com.example.db;

    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.widget.TextView;
    import android.widget.Toast;
    import android.util.Log;

    public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
    String userName = getDataFromOraDB();
    TextView tv = new TextView(this);
    tv.setText(userName);
    setContentView(tv);
    } catch (SQLException e) {
    Toast.makeText(this, e.getMessage(), 1).show();
    } catch (ClassNotFoundException e) {
    Toast.makeText(this, e.getMessage(), 1).show();
    }

    }

    public String getDataFromOraDB() throws SQLException,
    ClassNotFoundException {

    String name = null;
    String jdbcURL = "jdbc:oracle:thin:@192.168.0.1:1521:xe";
    String user = "supper";
    String passwd = "1234";
    // Load the Oracle JDBC driver

    try {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection conn;
    ResultSet rs;
    Statement stmt;
    conn = DriverManager.getConnection(jdbcURL, user, passwd);
    stmt = conn.createStatement();
    rs = stmt.executeQuery("select A from TEST");
    if (rs.next()) {
    name = rs.getString("A");
    }
    } catch (java.sql.SQLException e) {
    // TODO Auto-generated catch block
    System.out.println("the exception is" + e.toString());
    }

    Toast.makeText(getApplicationContext(), name, 1).show();
    return name;
    }

    }
  • 2. Re: android link oracle Internal database?????
    931063 Newbie
    Currently Being Moderated
    the same problem,I USE Oracle Database 11g Express Edition Release 11.2.0.2.0
  • 3. Re: android link oracle Internal database?????
    sb92075 Guru
    Currently Being Moderated
    928060 wrote:
    the same problem,I USE Oracle Database 11g Express Edition Release 11.2.0.2.0
    on a Good Day I can only spell Java.

    It is not clear to me what exactly your problem is.

    Below is a working example of how to have Java connect to Oracle DB & execute a SQL statement
    bcm@bcm-laptop:~$ cat Conn.java
    import java.sql.*;
    class Conn {
      public static void main (String[] args) throws Exception
      {
       Class.forName ("oracle.jdbc.OracleDriver");
    
       Connection conn = DriverManager.getConnection
         ("jdbc:oracle:thin:@//localhost:1521/v112", "scott", "tiger");
                            // @//machineName:port/SID,   userid,  password
       try {
         Statement stmt = conn.createStatement();
         try {
           ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
           try {
             while (rset.next())
               System.out.println (rset.getString(1));   // Print col 1
           } 
           finally {
              try { rset.close(); } catch (Exception ignore) {}
           }
         } 
         finally {
           try { stmt.close(); } catch (Exception ignore) {}
         }
       } 
       finally {
         try { conn.close(); } catch (Exception ignore) {}
       }
      }
    }
    bcm@bcm-laptop:~$ javac Conn.java
    bcm@bcm-laptop:~$ export CLASSPATH=/u01/app/oracle/product/11.2.0/dbhome_1/owb/wf/lib/ojdbc14.jar:.
    bcm@bcm-laptop:~$ java Conn
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
    PL/SQL Release 11.2.0.1.0 - Production
    CORE     11.2.0.1.0     Production
    TNS for Linux: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    bcm@bcm-laptop:~$
  • 4. Re: android link oracle Internal database?????
    931063 Newbie
    Currently Being Moderated
    Could not find class 'javax.management.MBeanServer', referenced from method oracle.jdbc.driver.OracleDriver.registerMBeans
  • 5. Re: android link oracle  database?????
    931063 Newbie
    Currently Being Moderated
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dblink/com.example.dblink.MainActivity}: android.os.NetworkOnMainThreadException

    Edited by: 928060 on 2013/4/24 下午 7:58
  • 6. Re: android link oracle  database?????
    sb92075 Guru
    Currently Being Moderated
    928060 wrote:
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dblink/com.example.dblink.MainActivity}: android.os.NetworkOnMainThreadException

    Edited by: 928060 on 2013/4/24 下午 7:58
    why do you post Java problems in this Oracle forum?
    posted errors occur before ever leaving your device & getting onto the network.

    root cause and fix are guaranteed external to Oracle!
  • 7. Re: android link oracle  database?????
    931063 Newbie
    Currently Being Moderated
    1.add onCreate
    -------------------------------------------------------------
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads()
    .detectDiskWrites()
    .detectNetwork() // or .detectAll() for all detectable problems
    .penaltyLog()
    .build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
    .detectLeakedSqlLiteObjects()
    .detectLeakedClosableObjects()
    .penaltyLog()
    .penaltyDeath()
    .build());     
    ------------------------------------------------------------------
    2.add AndroidManifest.xml
    <uses-permission android:name="android.permission.INTERNET"/>

    3.USE ojdbc14.jar

    TEST OK

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points