PRANATHI G
PRANATHI G

Reputation: 1

MySQL connection using jdbc, android studio

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)

Upvotes: 0

Views: 22095

Answers (2)

ionizer
ionizer

Reputation: 1721

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.

Upvotes: 0

Abdullah ALT
Abdullah ALT

Reputation: 106

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

Upvotes: 1

Related Questions