0

I'm new to Android Studio and I hope you don't consider my question silly.I am trying to write a small program in the terminal of the android studio.When I try to run the same program in my terminal(not android studio) it's working fine.I added the MySQL-connector.jar file in android studio lib by going through this mysql JDBC driver to the android studio.But it didn't work.Please help me.Thanks in advance.

  //MySQConnectionExample.java
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.util.Properties;

  public class MySQLConnectionExample {
      public static void main(String[] args) {

            Connection conn1 = null;
            String result = " ";

            try {
                 Class.forName("com.mysql.jdbc.Driver");
                 String url1 = "jdbc:mysql://127.0.0.1:3306/demo";
                 String user = "root";
                 String password = "mypassword";

                 conn1 = DriverManager.getConnection(url1, user, password);
           if (conn1 != null) {
               System.out.println("Connected to the database test1");
           }
           String sql = " select address from pharmacy";
           PreparedStatement prest = conn1.prepareStatement(sql);
           ResultSet rs = prest.executeQuery();
            while(rs.next()) {
               result = rs.getString(1);
               System.out.println(result);

            }

        } catch (SQLException ex) {
        System.out.println("An error occurred. Maybe user/password is invalid");
        ex.printStackTrace();
    }  catch (ClassNotFoundException e) {
                e.printStackTrace();
    }
  }
}

//Error after execution

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at MySQLConnectionExample.main(MySQLConnectionExample.java:16)

When I remove "Class.forName("com.mysql.jdbc.Driver")" from the program this is the error i get

     An error occurred. Maybe user/password is invalid                         
    java.sql.SQLException: No suitable driver found for 
    jdbc:mysql://127.0.0.1:3306/demo
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at MySQLConnectionExample.main(MySQLConnectionExample.java:22)
1

2 Answers 2

1

You can't connect directly to databases from android devices. Build APIs that connects to your database. then connect your Android Application to these APIs.

Check this answer for a the same question https://stackoverflow.com/a/12233178/4442067

Sign up to request clarification or add additional context in comments.

Comments

0

I assume you're trying to directly connect to a MySQL database from your Android device. But first, you have a ClassNotFound exception, which means you haven't imported the library.

And when you get the driver to work, there's more problem in this part:

....
try {
     Class.forName("com.mysql.jdbc.Driver");
     String url1 = "jdbc:mysql://127.0.0.1:3306/demo";
     String user = "root";
     String password = "mypassword";

     conn1 = DriverManager.getConnection(url1, user, password);
....

By specifying 127.0.0.1 on your Android device, you're trying to connect to a MySQL server on your device. Now I don't know if you can install MySQL on Android, but you may want to install a MySQL server on your computer. This goes without saying that you must use the IP of that computer and have the MySQL port (3306) open on your computer.

So, what you may want to see is something like

....
try {
     Class.forName("com.mysql.jdbc.Driver");
     String url1 = "jdbc:mysql://192.168.1.10:3306/demo";
     String user = "root";
     String password = "mypassword";

     conn1 = DriverManager.getConnection(url1, user, password);
....

This assumes your MySQL server is installed and configured on your computer with IP address 192.168.1.10

Good luck.

EDIT: I can't really recommend this approach for a production application though, because I'm afraid what a hacker who can decompile your code can do with it.

4 Comments

Yeah, I'm trying to directly connect MySQL. I am using this approach just to understand how to connect MySQL to android using JDBC.I will definitely not use this method for my application.
I tried adding "192.168.1.10" but still it is giving the same error
Well, you still have that ClassNotFound exception which needs resolving. Have you checked if your JDBC driver's Java version is the same with your Android Studio?
Yeah, I still have that ClassNotFound exception.How do I check JDBC driver's Java version? My android studio version is 3.0.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.