title: Connecting to oracle in android
body: <p>So people are probably going to tell me this is a bad idea, but I'd like to at least give it a go. </p>
  
  <p><strong>EDIT</strong> The intention of this app is that it can only work when the device is part of the same network the oracle db is on or is connected to the network via VPN. The information in the database is not going to be globally accessible, which is why I will need direct connection to the oracle db. </p>
  
  <p>Now according to this thread</p>
  
  <p><a href="https://stackoverflow.com/questions/4991334/connecting-the-oracle-in-android-application/5013285#5013285">Connecting the oracle in android application</a></p>
  
  <p>He was successful in querying the oracle db.</p>
  
  <p>So I have a fairly basic class that when initialised will try to get a connection to my database.</p>
  
  <pre><code>package com.producermobile;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  import android.util.Log;
  
  public class ConnectOra {
      private Connection conn;
      private Statement stmt;
      public ConnectOra() throws ClassNotFoundException {
          try {
              Class.forName("oracle.jdbc.driver.OracleDriver");
              String url = "jdbc:oracle:thin:@x.x.x.x:1521:PR10";
              this.conn = DriverManager.getConnection(url,"xxx","xxx");
              this.conn.setAutoCommit(false);
              this.stmt = this.conn.createStatement();
          } catch(SQLException e) {
              Log.d("tag", e.getMessage());
          }       
      }
      public ResultSet getResult() throws SQLException {
          ResultSet rset = stmt.executeQuery("select customer from customers");
          stmt.close();
          return rset;            
      }
  }
  </code></pre>
  
  <p>And in my main activity onCreate method I have this</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      try {
          super.onCreate(savedInstanceState);
          ConnectOra db = new ConnectOra();
          ResultSet rs = db.getResult();
          ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
          while(rs.next()) {
              list.add(rs.getString(1));
          }
          setListAdapter(new ArrayAdapter&lt;String&gt;(this, R.layout.list_item, list));
  
          ListView lv = getListView();
          lv.setTextFilterEnabled(true);          
      } catch(Exception e) {
  
      }
  }
  </code></pre>
  
  <p>In Eclipse I add the external ojdbc14.jar file to the build path.</p>
  
  <p>However when I run</p>
  
  <pre><code>this.conn = DriverManager.getConnection(url,"xxx","xxx");
  </code></pre>
  
  <p>I receive the following exception</p>
  
  <blockquote>
    <p>"Io exception: The Network Adapter
    could not establish the connection"</p>
  </blockquote>
  
  <p>If however I create an instance of this class inside a standard java app with a main method, the connection works. Any ideas?</p>