0

I'm new to the android, to connect android app to mysql I done like this:

Imported mysqlconnecter.jar in to Android app libs folder and then written my code as shown in below:

import java.sql.*;
public class MainActivity extends Activity {

    private EditText  username=null;
    private EditText  password=null;
    private TextView attempts;
    private Button login;
    int counter = 3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = (EditText)findViewById(R.id.editText1);
        password = (EditText)findViewById(R.id.editText2);
        attempts = (TextView)findViewById(R.id.textView3);
        attempts.setText(Integer.toString(counter));
        login = (Button)findViewById(R.id.button1);
    }

    public void login(View view) throws ClassNotFoundException{
        Connection conn=null;
        ResultSet rs=null;
        PreparedStatement  stmt=null;
        String username1 = String.valueOf(username.getText());
        String password1 =  String.valueOf(password.getText());
        String Query="SELECT * from users where mail=? and password=?";
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/wst","xxxxxxxx","xxxxxx");

            stmt=conn.prepareStatement(Query);
            stmt.setString(1, username1);
            stmt.setString(2, password1);
            rs=stmt.executeQuery();
            boolean more=rs.next();

            if(more){
                Toast.makeText(getApplicationContext(), "Redirecting...",
                        Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(getApplicationContext(), "Wrong Credentials",
                        Toast.LENGTH_SHORT).show();
                attempts.setBackgroundColor(Color.RED);
                counter--;
                attempts.setText(Integer.toString(counter));
                if(counter==0){
                    login.setEnabled(false);
                }

            }

        }

        catch(SQLException e){

        }



    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Is it right way to connect to MySQL database?

1
  • do you run a mysql database on your android device ('localhost')? Commented Apr 2, 2014 at 13:26

1 Answer 1

2

There's not a built-in MySQL connector for the Android framework, and even using an external jar for this is discouraged. The best way you can accomplish this is via a webservice in the remote server, so instead of sending direct MySQL queries to a database, you'd send a HTTP POST to the remote webserver (for instance, written in PHP, Python or whatever you want) and this would connect to the local database and make the query.

I think this example might help you.

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

Comments

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.