Buen día, estoy tratando de realizar una conexion a mysql con JDBC pero me sale el siguiente error: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientC onnectionException: Could not create connection to database server.
Main Activity
package com.example.datecsa.jdbcproyect;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Connection conn = null;
String url = "jdbc:mysql://10.0.2.2:3306/discoteca";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
Toast.makeText(this, "Se encontro el driver exitosamente" , Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Error al encontrar el driver"+e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url,userName ,password );
Toast.makeText(this, "Se conecto a la bd exitosamente", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Error creando la conexion a la bd ---\n"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
try {
Statement st = (Statement) conn.createStatement();
ResultSet rs = st.executeQuery("select nombre from productos");
while (rs.next()) {
String coffeeName = rs.getString("nombre");
Toast.makeText(this, coffeeName, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, "Error despues de conexion"+e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.datecsa.jdbcproyect" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>