title: Connect to mysql database in Android with java
body: <p>I have installed mysql in my computer.and also have jetty..
  My requirement:-
  I have a <strong>database student with table name ari</strong>;
  ari table is:-</p>
  
  <pre><code>+----+-------+
  | id | name  |
  +----+-------+
  | 22 | Sandy |
  +----+-------+
  </code></pre>
  
  <p>from My Android program I want to fetch it from database..I have done this:-
  <strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
      private static final String url = "jdbc:mysql://localhost:3306/student";
      private static final String user = "root";
      private static final String pass = "root";
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          testDB();
      }
  public void testDB() {
          TextView tv = (TextView)this.findViewById(R.id.textView1);
          try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection(url, user, pass);
              String result = "Database connection success\n";
              Statement st = con.createStatement();
              ResultSet rs = st.executeQuery("select * from ari");
              ResultSetMetaData rsmd = rs.getMetaData();
  while(rs.next()) {
                  result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
                  result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
                          }
              tv.setText(result);
          }
          catch(Exception e) {
              e.printStackTrace();
              tv.setText(e.toString());
          }   
      }
  }
  </code></pre>
  
  <p>and <strong>activity_main.xml</strong> is:--</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin" &gt;
      &lt;TextView
           android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and the manifext file is:---</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.TourBus.info"
      android:versionCode="1"
      android:versionName="1.0-SNAPSHOT" &gt;
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="21" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt; 
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".MainActivity" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>But the problem is when I run that program in my android phone or emulator it is showing that:--</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:  
  Could not create connection to database server. 
  </code></pre>
  
  <p>I also have the jar file:--</p>
  
  <pre><code>mysql-connector-java
  </code></pre>
  
  <p>can anybody help me??pls</p>