简体   繁体   中英

Connect android app to mysql

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM