Connect to remote database using jdbc : Android Tutorial

In this turoial I am going to explain how can we connect to our remote mysql database using jdbc. JDBC is a software component which helps the java application to interact with mysql database. The database can be stored either in your local computer (using xampp or wamp) or in the remote server. If you want to store your database at the remote server then you might want to purchase a hosting server from a hosting website. It has phpmyadmin pre-installed. You can purchase one from godaddy from this link. Deluxe hosting should be good enough for you.

After purchasing the hosting server, login to your control panel using your login credential. In the main page you can see mysql database wizard under database section.

5.PNG

Click on it. Now you can see the page below

6.PNG

Enter your database name and click next step. Here you have to enter your username and password for the database.

7.PNG

Then click on Create User.

8.PNG

Here you have to give administrative privilege to the user. Without the administrative privileges you can not access the database. So select ALL PRIVILEGES and click next step. Now you have finished setting up database and users related with it. Click on go to home. Navigate to phpmyadmin and goto your database.  Click on sql and enter following code and click Go. You can customize the column name by yourselves. Now you can see a table named users on your database. So the database is setup finishing.

We need mysql-connector-java-5.1.18.jar to use the jdbc component. You can download the jar file from this link. You have to add this jar file to the library to use jdbc.

After adding the jar file to the library you need to enter following code  to setup connection to the database. server_url is in the format of  “jdbc:mysql://server_address/database_name”. Server address is the ip address of your hosting server and database_name is the name of your database.

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(server_url, database_username, database_password);
Statement st = conn.createStatement();

The full code for the MyUser class is given below

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by Abinash on 4/19/2016.
 */
public class Myuser {

    public static String server_url = "jdbc:mysql://xxx.xxx.xxx.xxx/database";
    public static String database_username = "abinash";
    public static String database_password = "password";

    public static String email, firstname, lastname, password, phonenumber;

    public void setEmail(String email) {
        this.email = email;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public interface SignupCallBack {
        void OnDone();
    }

    public static void Signup(final SignupCallBack callback){

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(server_url, database_username, database_password);
            Statement st = conn.createStatement();
            String sql = "INSERT INTO users (email, firstname, lastname, password, phonenumber) VALUES ('" + email + "','" + firstname + "','" + lastname + "','" + password + "','"+phonenumber+"' )";
            st.execute(sql);
            callback.OnDone();
        } catch (SQLException e) {
            callback.OnDone();
        } catch (Exception e) {
            callback.OnDone();
        }
    }


}

You can use the Myuser class to signup for a new user. Below is the sample code.

Myuser user = new Myuser();
user.setEmail("email");
user.setFirstname("abinash");
user.setLastname("neupane");
user.setPassword("password");
user.setPhonenumber("phonenumber");
user.Signup(new Myuser.SignupCallBack() {
    @Override
    public void OnDone() {
        Toast.makeText(activity, "Successfully signed up!", Toast.LENGTH_LONG).show();
    }
});

And that’s it. You have now stored your information in your remote database.

Happy Programming fellas

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s