简体   繁体   中英

Android Database Connectivity

I am new in android development and I am creating an android app.

When user click on login I should receive its data on my database.

I think I have a problem in android database connectivity: when I click on login I receive no data in my database.

I use XAMPP for database.

login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, pass);
                String result = "Database connection successful";
                PreparedStatement ps = con.prepareStatement("Insert into login  VALUES(?, ?)");
                ps.setString(1, email.getText().toString());
                ps.setString(2, password.getText().toString());
                ps.executeUpdate();


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } //end of insert button calling
            catch (SQLException e) {
                e.printStackTrace();
            }
            //to Fetch the Record
            //finish();

            login();

        }
    });

As a good practice, you want to add some modularity to your application's architecture. Typically, the authentication logic will not be implemented inside your android application, but rather in a separate module. For instance, inside a PHP file that resides in your server:

  1. Read the username and password in your app
  2. POST them to the PHP file
  3. The PHP file reads from the database, checks if the credentials are correct, and returns either a success message (plus user information if applicable) or an error.
  4. In your application, your get the returned value from the PHP file, then you decide what to do (typically you will either move the user to the next activity or display some error message).

You may want to have a look at this tutorial: https://www.tutorialspoint.com/android/android_php_mysql.htm

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