"Android Studio 3.1.2" connection error jdbc and mysql

0

I have already "scrutinized" this web in search of the solution to the problem of the connection with MySql through jdbc and I see that some users have achieved it, without the Webservice.

The error message you tell me is:

  

Error: java.lang.NoClassDefFoundError: com / mysql / jdbc / Driver

My code is not very different from what I've seen in other questions ... and I use the library mysql-connector-java-8.0.11, which I do not know if it will be well inserted (?)

package com.example.proyectojd.login.login;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MainActivity extends AppCompatActivity {
    private EditText etUsuario;
    private EditText etContrasena;
    private String bd = "autos-control";
    private String sv = "192.168.1.3";
    private String pt = "3306";
    private String us = "root";
    private String ct = "";
    private Button btAceptar;
    private TextView tvMensaje;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btAceptar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (conectarMySQL()==true) {
                    Toast.makeText(getBaseContext(), "Conexion establecida.", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getBaseContext(), "Conexion NO establecida.", Toast.LENGTH_LONG).show();
                }

            }
        });
    }
    public boolean conectarMySQL ()
    {
        boolean estadoConexion = false;
        Connection conexionMySQL = null;

        String driver = "com.mysql.jdbc.Driver";
        String urlMySQL = "jdbc:mysql://" + sv + ":" + pt + "/";
        Toast.makeText(this,urlMySQL + bd + " " + us+ " " + ct,Toast.LENGTH_LONG).show();
        try {
            Class.forName(driver).newInstance();
            conexionMySQL = DriverManager.getConnection(urlMySQL + bd ,us,ct);
            return true;
        } catch (Exception ex) {
            Toast.makeText(MainActivity.this,"Error al comprobar las credenciales:" + ex.getMessage(),Toast.LENGTH_LONG).show();
            Toast.makeText(MainActivity.this,"Error:" + ex.getCause(),Toast.LENGTH_LONG).show();
            return false;
        }
    }
}

I would appreciate a little help. Thanks

    
asked by jabm2000 07.06.2018 в 18:31
source

1 answer

0

Regarding the error:

  

Error: java.lang.NoClassDefFoundError: com / mysql / jdbc / Driver

You are not registering the .jar correctly in your project, you can add the .jar in Android 3.0 or later, the correct thing would be to go to:

File > New > New Module

Select Import .JAR/.AAR Package :

here you select the location of your .jar file and it will be added and registered in your project.

    
answered by 07.06.2018 в 19:41