0

my code is crashing when connecting to the mysql database in xampp server . im running my app in android emulator that come with android studio and i tried change my hostname url from 10.0.2.2 to my pc ip 192.168.some.thing.

I tried to debug my application i foundout that isconnectionstringsupported() saying that the url is not supported.

and this same code is working just fine in windows. I think the problem is in this line of code below i tried to change 10.0.2.2 to my pc ip address(192.168.1.12) but still no luck but i can able to access my phpmyadmin with these to address in browser. 10.0.2.2/phpmyadmin and 192.168.1.12/phpmyadmin working fine in android emulator browser.

THIS LINE MAY BE Culprit

static final String DB_URL = "jdbc:mysql://10.0.2.2/codeig";

and one more thing im not using any php code i just want to connect to server directly . HERE IS MY CODE

package com.managear.aiverreaver.managearlite.data.database.dbhelper;

import android.util.Log;

import com.managear.aiverreaver.managearlite.data.ManaUniData;
import com.managear.aiverreaver.managearlite.data.database.IDBHelper;
import java.sql.*;

public class MySqlDBHelper implements IDBHelper {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://10.0.2.2/codeig";

    //  Database credentials
    static final String USER = "ashish";
    static final String PASS = "ashish";

    private boolean m_isMysqlPhp;

    public MySqlDBHelper(boolean m_isMysqlPhp)
    {
        this.m_isMysqlPhp = m_isMysqlPhp;
    }

    @Override
    public ManaUniData ReadDB() {
        if(m_isMysqlPhp)
        {

        }
        else
        {
            Connection conn = null;
            Statement stmt = null;
            try{
                //STEP 2: Register JDBC driver
                Class.forName("com.mysql.jdbc.Driver");

                //STEP 3: Open a connection
                Log.i("hello","Connecting to a selected database...");
                conn = DriverManager.getConnection(DB_URL, USER, PASS); // FIXME: After this line of code exception is firing
                Log.i("hello","Connected database successfully...");

                //STEP 4: Execute a query
                Log.i("hello","Creating statement...");
                stmt = conn.createStatement();

                String sql = "SELECT * FROM posts";
                ResultSet rs = stmt.executeQuery(sql);
                //STEP 5: Extract data from result set
                while(rs.next()){
                    //Retrieve by column name
                    int id  = rs.getInt("id");
                   /* int age = rs.getInt("age");
                    String first = rs.getString("first");
                    String last = rs.getString("last");
                    */

                    //Display values
                    Log.i("hello","ID: " + id);
                  /*  Log.i("hello",", Age: " + age);
                    Log.i("hello",", First: " + first);
                    Log.i("hello",", Last: " + last);
                    */
                }
                rs.close();
            }catch(SQLException se){
                //Handle errors for JDBC
                se.printStackTrace();
            }catch(Exception e){
                //Handle errors for Class.forName
                e.printStackTrace();
            }finally{
                //finally block used to close resources
                try{
                    if(stmt!=null)
                        conn.close();
                }catch(SQLException se){
                }// do nothing
                try{
                    if(conn!=null)
                        conn.close();
                }catch(SQLException se){
                    se.printStackTrace();
                }//end finally try
            }//end try
            Log.i("hello","Goodbye!");
        }//end main


        return null;
    }
}

and HERE IS ERROR IM GETTING

Caused by: java.lang.ExceptionInInitializerError
        at com.mysql.cj.conf.ConnectionUrlParser.isConnectionStringSupported(ConnectionUrlParser.java:148)
        at com.mysql.cj.conf.ConnectionUrl.acceptsUrl(ConnectionUrl.java:259)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:195)
        at java.sql.DriverManager.getConnection(DriverManager.java:179)
        at java.sql.DriverManager.getConnection(DriverManager.java:213)
        at com.managear.aiverreaver.managearlite.data.database.dbhelper.MySqlDBHelper.ReadDB(MySqlDBHelper.java:41)
        at com.managear.aiverreaver.managearlite.data.database.ManaDBManager.Execute(ManaDBManager.java:60)
        at com.managear.aiverreaver.managearlite.utility.ManaAsyncTaskLoader.loadInBackground(ManaAsyncTaskLoader.java:38)
        at com.managear.aiverreaver.managearlite.utility.ManaAsyncTaskLoader.loadInBackground(ManaAsyncTaskLoader.java:24)
        at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:306)
        at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:59)
        at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:47)
        at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:138)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        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)
            ... 17 more
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
AiverReaver
  • 164
  • 2
  • 10
  • but i searched on google and found some videos where they can be able to connect to my sql server https://www.youtube.com/watch?v=N0FLT5NdSNU and this https://www.youtube.com/watch?v=Gc7C95qHa3g – AiverReaver Oct 26 '18 at 11:24
  • "but i searched on google and found some videos where they can be able to connect to my sql server " Besides if so you shouldn't be exposing a online MySQL database username and password in a Android app which can be decompiled from Android application package (APK) into readable code. – Raymond Nijland Oct 26 '18 at 11:27
  • i does care about that in the end im taking input from the user for username and password . and pls tell me is there any solution for that error im getting. – AiverReaver Oct 26 '18 at 11:42
  • Besides forget to mention you need to total open your MySQL port to the outside world and a make a MySQL user account with a wildcard on the host position.. Not something you want to do.. Besides if you follow the first video link you would see you need to import and use MySQL's native jar connection packet (Connector/J ). – Raymond Nijland Oct 26 '18 at 11:46
  • 1
    bro first off all this app is just prototype i will add all thing later i just want to test if this work and for my personal work i just want to connect to server directly without any php code or something like that .** I have already added mysql native jar file to my libs and added all thing to app.gradle file ** and one more thing pls if you help only then replay pls bro. – AiverReaver Oct 26 '18 at 12:05
  • i don't like street language so don't call me bro.. Besides ive given you helpfull tips in mine comments.. What the stackoverflow comment system is all about. – Raymond Nijland Oct 26 '18 at 12:14
  • Try this code/method https://gist.github.com/cofearabi/5039135 – Raymond Nijland Oct 26 '18 at 12:23
  • sorry, I didn't mean to upset up. – AiverReaver Oct 26 '18 at 13:19
  • Can you solved this problem? I found the problem like you. – Fame th Jun 18 '19 at 03:44
  • I dropped my idea. – AiverReaver Jun 19 '19 at 04:07

0 Answers0