简体   繁体   English

Android数据库连接

[英]Android Database Connectivity

I am new in android development and I am creating an android app. 我是android开发的新手,我正在创建一个Android应用程序。

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. 我想我的android数据库连接有问题:当我点击登录时,我的数据库中没有收到任何数据。

I use XAMPP for database. 我使用XAMPP作为数据库。

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. 通常,身份验证逻辑不会在Android应用程序内部实现,而是在单独的模块中实现。 For instance, inside a PHP file that resides in your server: 例如,在驻留在服务器中的PHP文件中:

  1. Read the username and password in your app 阅读您应用中的用户名和密码
  2. POST them to the PHP file 将它们发布到PHP文件
  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. PHP文件从数据库中读取,检查凭据是否正确,并返回成功消息(如果适用,还有用户信息)或错误。
  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). 在您的应用程序中,您从PHP文件中获取返回的值,然后您决定要做什么(通常您将用户移动到下一个活动或显示一些错误消息)。

You may want to have a look at this tutorial: https://www.tutorialspoint.com/android/android_php_mysql.htm 您可能需要查看本教程: https//www.tutorialspoint.com/android/android_php_mysql.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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