0

I have this problem, when i execute mi android app and try to connect my app to mysql database using xamp .

Using this code

    package com.example.on_target;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    EditText user,pass;
    Button btn_login;
    Connection conexionMySQL=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user=(EditText)findViewById(R.id.etUser);
        pass=(EditText)findViewById(R.id.etPassword);
        btn_login=(Button)findViewById(R.id.button1);       
        btn_login.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) 
              {
                 try
                     {
                     Class.forName("com.mysql.jdbc.Driver").newInstance();
                    conexionMySQL = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/android_test?connectTimeout=3000"
                            ,"root","");
                    Log.i("Conexion","Conexion pasable");
                      String usuario=user.getText().toString();
                      String contrasena=pass.getText().toString();                    
                      Statement st = conexionMySQL.createStatement();
                      String QSLEjecutar = "select count(*) from user where user_name='"+usuario+"' and user_password='"+contrasena+"'";                                                              
                      ResultSet rs = st.executeQuery(QSLEjecutar);                                                
                      //número de columnas (campos) de la consula SQL                                

                      //mostramos el resultado
                      while (rs.next()) 
                      {                           
                             int h=rs.getInt(1);
                             if(h==1){
                                 Intent intento=new Intent(getApplicationContext(),Welcome.class);
                                 startActivity(intento);
                                 Toast.makeText(getApplicationContext(), R.string.correct,Toast.LENGTH_LONG).show();
                             }else{
                                 Toast.makeText(getApplicationContext(), R.string.incorrect,Toast.LENGTH_LONG).show();
                             }
                          }                                                                                                                 
                 }                             
                  catch (Exception e) 
                  {  
                      Log.d("WTF", e.getMessage());
                  } 
              }
            }); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch(id){
        case R.id.exit:
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }


}

And when I execute the app I got this error for no reason at all and even have the version 5.181.30, so I don't know what to do:

01-20 16:21:23.184: D/WTF(2150): Communications link failure
01-20 16:21:23.184: D/WTF(2150): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Please read: http://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android – Morrison Chang Jan 20 '16 at 22:54
  • 127.0.0.1 = localhost, that's your phone! (and I'm pretty certain there is no mysql server running on it) –  Feb 02 '16 at 08:55

0 Answers0