Punkster
Punkster

Reputation: 221

Connect to AWS table from Android Studio

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?

Upvotes: 0

Views: 2988

Answers (1)

Rene M.
Rene M.

Reputation: 2690

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.

Upvotes: 2

Related Questions