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