Question [Q] JDBC in android fragment not working

I am working on an android application that modifies an external MySQL database. I know I can use an intermediate PHP/JSON service to do it, but I rather use JBDC because connection is faster and my project teachers want me to do it this way.

As it's my first app, I started with a simple button and an action(create a database), which actually works (in fact two buttons, the first one doesn't work on skd higher than 9, AsyncTask has to be used in them):

Code:
Select Code
package com.example.prova;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.sql.*;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button1 = (Button) findViewById(R.id.btconn1);
        final Button button2 = (Button) findViewById(R.id.btconn2);


        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                try
              {
                  String URL = "jdbc:mysql://" + "192.168.1.200" + ":" + "3306";
                  String USER = "app";
                  String PASS = "android";

                  Toast.makeText(getApplicationContext(),
                            "Conectando a servidor MySQL", 
                            Toast.LENGTH_SHORT).show();

                  Class.forName("com.mysql.jdbc.Driver").newInstance();  
                  Connection conn = DriverManager.getConnection(URL, USER, PASS);

                  Toast.makeText(getApplicationContext(),
                            "Conectado Servidor MySQL", 
                            Toast.LENGTH_LONG).show();

                  Statement stmt = conn.createStatement();

                  String SQL = "CREATE DATABASE SYNC";

                  stmt.executeUpdate(SQL);

                  conn.close();               
              }
                catch (ClassNotFoundException e) 
                {
                  Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                } 
                catch (SQLException e) 
                {
                  Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }
                catch (Exception e) 
                {  
                  Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new LED13ON().execute();
            }
        });
        }

    @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;
    }

    public class LED13ON extends AsyncTask<Void, Integer, Void> {

        @Override
        protected void onPostExecute(Void result){
            SystemClock.sleep(2000);
        }

        @Override
        protected void onPreExecute(){
            SystemClock.sleep(2100);
        }

        @Override
        protected void onProgressUpdate(Integer... values){
            SystemClock.sleep(100);
        }

        @Override
        protected Void doInBackground(Void... arg0){

            try
              {
              String URL = "jdbc:mysql://" + "192.168.1.200" + ":" + "3306";
              String USER = "app";
              String PASS = "android";

              Class.forName("com.mysql.jdbc.Driver").newInstance();  
              Connection conn = DriverManager.getConnection(URL, USER, PASS);

              Statement stmt = conn.createStatement();

              String SQL = "CREATE DATABASE aSYNC";

              stmt.executeUpdate(SQL);

              conn.close();               
              }
            catch (ClassNotFoundException e) 
            {
            } 
            catch (SQLException e) 
            {
            }
            catch (Exception e) 
            {  
            }
            return null;
                }
    }
}
The problem is when I try to use fragments, eclipse returns no errors but JDBC code is not working (logcat gives me no errors too). I know that's only the JDBC code which is not working because it gets inside the LED13ON and makes the SystemClock.sleep(2000), because the button is marked for two seconds. This is the code I have for the fragment in a new class:

Code:
Select Code
package com.example.smarthome;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;


import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;



public class Fragment_main extends Fragment {
        public Fragment_main() {
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
               View rootView = inflater.inflate(R.layout.fragment_main,container, false);

               Button btn = (Button) rootView.findViewById(R.id.btconn1);
               btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick (View v) {
                    new LED13ON().execute();
                }
               });

               return rootView;
        }   

        public class LED13ON extends AsyncTask<Void, Integer, Void> {

            @Override
            protected void onPostExecute(Void result){
                SystemClock.sleep(2000);
            }

            @Override
            protected void onPreExecute(){
                SystemClock.sleep(2100);
            }

            @Override
            protected void onProgressUpdate(Integer... values){
                SystemClock.sleep(100);
            }

            @Override
            protected Void doInBackground(Void... arg0){

                try
                  {
                  String URL = "jdbc:mysql://" + "192.168.1.200" + ":" + "3306";
                  String USER = "app";
                  String PASS = "android";

                  Class.forName("com.mysql.jdbc.Driver").newInstance();  
                  Connection conn = DriverManager.getConnection(URL, USER, PASS);

                  Statement stmt = conn.createStatement();

                  String SQL = "CREATE DATABASE aSYNC";

                  stmt.executeUpdate(SQL);

                  conn.close();               
                  }
                catch (ClassNotFoundException e) 
                {
                } 
                catch (SQLException e) 
                {
                }
                catch (Exception e) 
                {  
                }
                return null;
            }
        }

}
So I don't understand why being the same code it's not working for the second app, having changed the setOnClickListener to work in the fragment. Can anyone help me? I would really like to use the swipe views for my app as I think it fits more the android Holo style.

Thank you for your time!

EDIT:

I found the solution to my problem:

I logged the exceptions, it gave me the error:

Quote:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
The solution was to add newInstance in the Class.forName:

Quote:
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
So that's all, now my app is working as I intended. Thanks for everything!