How to connect Android with MySQL using Mysql JDBC driver [duplicate]

If you want to connect to Mysql database from Android you just need to follow these steps:

  • Download the driver mysql-connector-java-3.0.17-ga-bin.jar
  • Paste it to the folder libs in your android project.
  • Click ConfigureBuildPath->add jar to include the jar to the project.
  • Now you have the driver, but you also need to give permissions in the androidManifest.xml for INTERNET.
  • Use the next code for connecting:

    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    }catch(Exception e){
        System.err.println("Cannot create connection");
    }
    try{
        connection = DriverManager.getConnection("jdbc:mysql://192.168.xx.xx:3306/dbname","root","password");
        Statement statement = connection.createStatement();
    
        String query = "SELECT column1, column2 FROM table1 WHERE column3 = ";
        query = query +"'" +variable+"'";           
        ResultSet result = statement.executeQuery(query);
    }catch(Exception e){
        System.err.println("Error");
    } 
    

Advice: If the instance of the drivers doesn’t give any errors but you get an exception with the connection you should try to remove the Target SDK version from your manifest, as for some versions this gives problems.

Leave a Comment