title: Android JDBC MySQL get tables by Database
body: <p>I got through some tutorials, but all of them are very simple an can not be used in my solution.</p>
  
  <p>I have connection to a database via JDBC MySQL.</p>
  
  <pre><code>public class ConnCore extends AsyncTask&lt;Void, Void, Boolean&gt; {
  
  Activity activity;
      Context context;
      public ConnCore(Context context, Activity currActivity) {
          this.context = context.getApplicationContext();
          this.activity = currActivity;
      }
  
  
      @Override
      protected Boolean doInBackground(Void... params) {
  
  
          try{Class.forName("com.mysql.jdbc.Driver");}
          catch(Exception e){
             DataStore.DataStoreClass.connExc = e;
              return false;}
  
  
          try{
  
            DataStore.DataStoreClass.mysqlConn = DriverManager.getConnection(DataStore.DataStoreClass.connectionString);
  
              return true;
          }catch(Exception e){
              DataStore.DataStoreClass.connExc = e;
              return false;
          }
  
  
  
  
  
      }
  
      @Override
      protected void onPostExecute (Boolean b) {
  
  
           if(b == true) {
  
              Intent intent = new Intent(context, DBList.class);
              intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(intent);
  
  
  
          } else {
              AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
              alertDialog.setTitle("Error");
              alertDialog.setMessage(DataStore.DataStoreClass.connExc.toString());
              alertDialog.show();
  
          }
  }
  </code></pre>
  
  <p>This will do in connection with this string:</p>
  
  <pre><code>static String connectionString = "jdbc:mysql://XXX.XXX.XXX.XXX:3306?user=root&amp;password=test&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;maxReconnects=10";
  </code></pre>
  
  <p>If connected succesfully, it starts new activity DBList, which starts new AsyncTask to get Databases.
  AsyncTask looks like this:</p>
  
  <pre><code>public class GetDBList extends AsyncTask&lt;Void, Void, Boolean&gt; {
      Activity activity;
      Context context;
      public GetDBList(Context context, Activity activity){
          this.activity = activity;
          this.context = context;
      }
  
      @Override
      protected Boolean doInBackground(Void... params){
  
  
          Statement stmt = null;
          String query = "SHOW DATABASES";
          DataStore.DataStoreClass.DBResponse = new ArrayList&lt;String&gt;();
  
  
  
          try {
              DatabaseMetaData meta = DataStore.DataStoreClass.mysqlConn.getMetaData();
              ResultSet res = meta.getCatalogs();
              while (res.next()) {
                  DataStore.DataStoreClass.DBResponse.add(res.getString("TABLE_CAT"));
  
              }
              res.close();
              return true;
          }
          catch (SQLException e){
  
              AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
              alertDialog.setTitle("Error");
              alertDialog.setMessage(e.toString());
              alertDialog.show();
              return false;
          }
  
  
  
  
  
      }
  
      @Override
      protected void onPostExecute(Boolean b){
         if(b) {
             ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(context, android.R.layout.simple_list_item_1, DataStore.DataStoreClass.DBResponse){
                 @Override
                 public View getView(int position, View convertView, ViewGroup parent) {
                     View view = super.getView(position, convertView, parent);
                     TextView text = (TextView) view.findViewById(android.R.id.text1);
                     text.setTextColor(Color.BLACK);
                     return view;
                 }
             };
             ListView lv = (ListView) activity.findViewById(R.id.db_lv);
             lv.setAdapter(arrayAdapter);
  
             lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                                         int position, long id) {
  
  
                     DataStore.DataStoreClass.currentDB = ((TextView) view).getText().toString();
                     Toast.makeText(context, DataStore.DataStoreClass.currentDB,
                             Toast.LENGTH_SHORT).show();
  
                     AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
                     alertDialog.setTitle("Error");
                     alertDialog.setMessage(DataStore.DataStoreClass.connExc.toString());
                     alertDialog.show();
  
                 }
             });
  
         }
  
  
      }
  }
  </code></pre>
  
  <p>Up to this point, everything works just fine, I can get the ListView with databases.</p>
  
  <p>And now. Every List_Item has onClickListener.
  How I can pass the String value (containing name of database) to my existing connection to get its tables?</p>
  
  <p>Every tutorial is already with name of database. I am trying to create an universal DB client, so you actually dont know, what databases are on server.</p>
  
  <pre><code>String url = "jdbc:mysql://" + serverName +  "/" + schema;
  </code></pre>
  
  <p>And I don't want to close previous connection and make a new one directly to the database</p>
  
  <p>So is it somehow possible to "edit"</p>
  
  <pre><code>(static Connection mysqlConn;)
  DataStore.DataStoreClass.mysqlConn = DriverManager.getConnection(DataStore.DataStoreClass.connectionString);
  </code></pre>
  
  <p>somehow ... or any other way?</p>
  
  <p>Thank you.</p>