0

I have a MYSQL table hosted in amazon AWS called LoginScreen. How to access it with Android Studio. In a normal java code i am using a jdbc-mysql connector and i connect to it. So i tried to do the same but i am unable to connect with it.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    }catch(Exception e){
        System.err.println("Cannot create connection");
    }
    Connection connection=null;
    TextView sample;
    try{
        connection = DriverManager.getConnection("jdbc:mysql://samplelink/database","root","password");
        Statement statement = connection.createStatement();

        sample = (TextView) findViewById(R.id.sample);
        ResultSet resultset = statement.executeQuery("select * from Loginscreen");
        while(resultset.next()){
            System.out.println("Solution"+resultset.getString(1));
            txtLat.append(resultset.getString(1));

        }
    }catch(Exception e){
        sample = (TextView) findViewById(R.id.sample);
        System.err.println("Error");
        sample.append(e.toString());
    }
}

I have added the following to android manifest.

  <uses-permission android:name="android.permission.INTERNET" />

I am getting the following error

    java.lang.classNotFoundException:com.mysql.jdbc.Driver 

How to use jdbc driver in Android studio like we use in Netbeans. If i import the connector i get a gradle error while running the code. Can someone explain me how to do this in a step by step fashion?

Punkster
  • 221
  • 6
  • 17
  • 1
    This question has been asked countless times on this site alone. Just searching for "Android JDBC" on here turns up tons of identical questions. – Mark B Nov 02 '15 at 16:15
  • I am unable to find something that is suitable for android studio. Because i get an gradle error when i import it normally. – Punkster Nov 02 '15 at 16:30
  • http://stackoverflow.com/questions/24784871/how-to-mysql-jdbc-driver-to-android-studio/24785110#24785110 Just need a guide to do this – Punkster Nov 02 '15 at 16:32

1 Answers1

2

In AWS You had defined a SecurityGroup for your RDS Instance (mySQL Database).

Add a rule to allow access to your instance from everywhere.

Then in the Info-Panel in AWS there is an endpoint url. This is your external address on which you ca access your db.

If this is given, you can directly connect over jdbc using any kind of java application, running on a device with internet connectivity.

BUT!!! I would never expose my RDS instance to everyone! Write a backend application, prodiving you your own API, is best practice.

Rene M.
  • 2,660
  • 15
  • 24
  • Agreeing with Rene about writing your own backend application/webservice. See: http://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android – Morrison Chang Nov 02 '15 at 18:01