0

So, i've already searched in millions of topics here in stack overflow and no one was able to help me. I have an android project in eclipse and i just want to connect to my mysql database that is being hosted locally but i keep receiving an error (i've treated the exception to display a toast message and all i get in this toast is "com.mysql.jdbc.Driver" which is the path of the drive, i assume). I've already pasted the jdbc connector in the "libs" folder i've created inside the project and i already added this jar file to the build path and now i have the mysql connector in the Referenced Libraries folder and in the libs folder. I also put the connector in the directory "c:\program files\java\jdk\jre\lib\ext". My code looks like this:

import java.sql.*;

public class MainActivity extends Activity{

    String driver = "com.mysql.jdbc.Driver";
    String url    = "jdbc:mysql://localhost/MyDB";
    String user   = "root";
    String pass   = "";

    Connection conn;

    private void LoadDB(){
        try{
            Class.forName(driver);
            conn = DriverManager.getConnection(url,user,pass);
        }
        catch(Exception e){
            Toast.makeText(getApplicationContext(),e.getMessage(),5).show();
        }
    }

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

        LoadDB();
    }
}
Levi Moraes
  • 57
  • 1
  • 8
  • [How can I use external JARs in an Android project?](http://stackoverflow.com/questions/1334802/how-can-i-use-external-jars-in-an-android-project) What is so difficult to do this? [JDBC driver for MySQL](https://dev.mysql.com/downloads/connector/j/) – asdf Nov 04 '16 at 16:58
  • It is strongly encouraged not to use a JDBC connection in an Android application and instead set your database behind a REST API like PHP (or web server of your choice) – OneCricketeer Nov 04 '16 at 17:02
  • I know, but i've already tried to do it using php and JSON with the resources from volley library but my project doesn't recognize the library so i'm trying to do it the way that i'm already used to, by developing java applications for desktop, wich is using JDBC connector for MySQL. And here it comes the problem which is the driver not being recognized, as i summarized in the above topic – Levi Moraes Nov 06 '16 at 20:51

1 Answers1

0

i tried use jdbc on my android app but it's not recommended now. try to use okhttpclient ( see "square.github.io/okhttp" on google

another tutorial with more is here, http://mathias-seguy.developpez.com/tutoriels/android/utiliser-retrofit/

i hope it will be usefull for u.

PS1: to use jdbc if u want really

String url = "jdbc:mysql://monserveurMySQL/test?autoReconnect=true";
            String user = "root";
            String passwd = "monPaaswd";

            Toast.makeText(this, "Connection ...",Toast.LENGTH_SHORT).show();
            Connection conn = DriverManager.getConnection(url, user, passwd);
            Toast.makeText(this, "Connection Etablished", Toast.LENGTH_SHORT).show();
  • but that's what i did, except i'm using a localhost server. Still my app seem not to be able to connect to the driver and the toast message keeps returning "com.mysql.jdbc.Driver" which id the path of the driver that i set in the Class.forName() command. And when i run the application without the Class.forName the error i get is "No suitable driver". – Levi Moraes Nov 06 '16 at 20:44