java – MYSQL connection with AsyncTask Android

I have the following method to insert data in a MYSQL table:

public static void Insert (String nLocal, String nClient, String address, String telephone) {

    String insertTableSQL = "INSERT INTO license (nlocal, ncliente, direccion, telefono) VALUES"
            + "(?,?,?,?)";

    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;

    try {

        dbConnection = with;
        Log.i ("LMO", "with" + con.toString ());
        preparedStatement = dbConnection.prepareStatement (insertTableSQL);

        preparedStatement.setString (1, nLocal);
        preparedStatement.setString (2, nClient);
        preparedStatement.setString (3, address);
        preparedStatement.setString (4, telephone);

        // execute insert SQL stetement
        preparedStatement.executeUpdate ();

        System.out.println ("Record is inserted into DBUSER table!");

    } catch (SQLException e) {

        System.out.println (e.getMessage ());



This method I think works perfectly, but at the time of executing the connection I wanted to do it with AsyncTask because otherwise if I try to establish the connection in the main thread the application is closed.

This is my connection method with AsyncTask:

private class Connect extends AsyncTask {

    protected Connection doInBackground (Connection ... strings) {

        try {
            Class.forName (DB_DRIVER);
            con = DriverManager.getConnection (DB_URL + DB_NAME, DB_USER, DB_PASSWORD);

        } catch (ClassNotFoundException e) {
            e.printStackTrace ();
        } catch (SQLException e) {
            e.printStackTrace ();

    return with;}


This last code gives me errors everywhere but I do not know how to do it correctly, I've been researching but people do the query in the same doinbackground but I want to perform the query in a separate method to run on an onclick. These 2 methods are in the same class, but I do not know how to call the doinbackground so that it establishes the connection in a thread different from the main one and thus pick up the data of the connection and introduce it in the insert method.

Thanks in advance and greetings.