Estoy trabajando con jdbc para conectar a base de datos mysql con android studio pero cuando ejecuto el programa y quiero hacer un registro de usuario la app se detiene y me marca en el logcat un error enorme que comienza con FATAL EXCEPTION: AsyncTask #1.
Estuve leyendo y vi mucha gente que decía que los permisos de red pero yo ya puse el permission internet, no es eso y ya me quede sin ideas.
Este es el código de coneccion a base:
package com.example.metrofood;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConectionClass
{
String classs = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://ip/metrofood";
String un = "root";
String pass = "";
@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName(classs);
conn = DriverManager.getConnection(url, un, pass);
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return conn;
}
}
Y este el código de la actividad de registro
package com.example.metrofood;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.transition.Explode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.Statement;
public class SignUp extends AppCompatActivity {
EditText txt_User,txt_Pass,txt_ConfPass,txt_Email;
Button sig;
ConectionClass conectionClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
txt_User=findViewById(R.id.txt_User);
txt_Pass=findViewById(R.id.txt_Pass);
txt_ConfPass=findViewById(R.id.txt_ConfPass);
txt_Email=findViewById(R.id.txt_Email);
conectionClass = new ConectionClass();
Explode explode = new Explode();
explode.setDuration(500);
getWindow().setEnterTransition(explode);
sig = findViewById(R.id.btn_Next);
sig.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Register register = new Register();
register.execute("");
startActivity(new Intent(SignUp.this, Data.class));
}
});
}
public class Register extends AsyncTask<String,String,String>
{
String nombreUsu = txt_User.getText().toString();
String passUsu = txt_Pass.getText().toString();
String emailUsu = txt_Email.getText().toString();
String z="";
boolean isSuccess=false;
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
if(nombreUsu.trim().equals("")|| emailUsu.trim().equals("") ||passUsu.trim().equals(""))
z = "Please enter all fields correctamente";
else
{
try {
Connection con = conectionClass.CONN();
if (con == null) {
z = "Please check your internet connection";
} else {
String query="insert into usuario values('"+nombreUsu+"','"+emailUsu+"','"+passUsu+"','4')";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
z = "Register successfull";
isSuccess=true;
}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions"+ex;
}
}
return z;
}
@Override
protected void onPostExecute(String s) {
Toast.makeText(getBaseContext(),""+z, Toast.LENGTH_LONG).show();
if(isSuccess) {
startActivity(new Intent(SignUp.this,Data.class));
}
}
}
}
Por ultimo este es el error que me muestra a la hora de ejecutar la aplicacion:
> FATAL EXCEPTION: AsyncTask #1
Process: com.example.metrofood, PID: 16471
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ExceptionInInitializerError
at com.mysql.cj.conf.ConnectionUrlParser.isConnectionStringSupported(ConnectionUrlParser.java:148)
at com.mysql.cj.conf.ConnectionUrl.acceptsUrl(ConnectionUrl.java:258)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:187)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:213)
at com.example.metrofood.ConectionClass.CONN(ConectionClass.java:29)
at com.example.metrofood.SignUp$Register.doInBackground(SignUp.java:76)
at com.example.metrofood.SignUp$Register.doInBackground(SignUp.java:55)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 4:
(?<scheme>[\w:%]+)\s*(?://(?<authority>[^/?#]*))?\s*(?:/(?!\s*/)(?<path>[^?#]*))?(?:\?(?!\s*\?)(?<query>[^#]*))?(?:\s*#(?<fragment>.*))?
^
at java.util.regex.Pattern.compileImpl(Native Method)
at java.util.regex.Pattern.compile(Pattern.java:411)
at java.util.regex.Pattern.<init>(Pattern.java:394)
at java.util.regex.Pattern.compile(Pattern.java:381)
at com.mysql.cj.conf.ConnectionUrlParser.<clinit>(ConnectionUrlParser.java:89)
... 14 more
Agradeceria su ayuda, en serio me estoy martirizando con esa app.
http
. Querer meter tu BD MySQL dentro de la app es una pésima idea, porque Android no está pensado para eso.