android+mysql:connection类返回空对象

s2j5cfk0  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(369)

我正在尝试通过jtdsjdbc将我的android登录应用程序连接到xamp服务器(一个apache正在本地机器上运行)。代码似乎无法连接服务器。代码如下:
连接类.java

package com.ercess.databaseconnection;

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.os.Bundle;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionClass {

    public String driver = "net.sourceforge.jtds.jdbc.Driver";

    String url = "jdbc:mysql://localhost/events-test";
    //String url = "jdbc:mysql://127.0.0.1/events-test";
    public String un = "root";
    public String password = "password";
    public String db = "users";

    @SuppressLint("NewApi")
    public Connection CONN() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        java.sql.Connection conn = null;
        String ConnURL = null;
        try{
            Class.forName(driver);
            ConnURL = "jdbc:jtds:sqlserver://" + "127.0.0.1" +";databaseName="+ db + ";user=" + un+ ";password=" + password + ";";

            conn = DriverManager.getConnection(ConnURL);

        }catch (SQLException se){
            Log.e("ERROR", se.getMessage());
        }catch (ClassNotFoundException e){
            Log.e("ERROR", e.getMessage());
        }catch(Exception e){
            Log.e("ERROR", e.getMessage());
        }
        //Log.d("conn",conn.toString());
        return conn;
    }
}

主活动.java

package com.ercess.databaseconnection;

    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    public class MainActivity extends AppCompatActivity {
        EditText email, password;
        Button login;
        ProgressDialog progressDialog;
        ConnectionClass connectionClass;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            email = findViewById(R.id.email);
            password = findViewById(R.id.password);
            login = findViewById(R.id.login);
            connectionClass = new ConnectionClass();
            progressDialog = new ProgressDialog(this);
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Dologin dologin = new Dologin();
                    dologin.execute();
                }
            });
        }
        public class Dologin extends AsyncTask<String, String, String>{
            String emailstring = email.getText().toString();
            String passstring = password.getText().toString();
            String z = "";
            boolean isSuccess = false;
            String em, pass;
            @Override
            protected void onPreExecute() {
                progressDialog.setMessage("Loading...");
                progressDialog.show();
                super.onPreExecute();
            }
            protected String doInBackground(String... params){
                if(emailstring.trim().equals("") || passstring.trim().equals(""))
                    z = "Please enter all fields........";
                else
                {
                    try{
                        Connection con = connectionClass.CONN();
                        if(con == null){
                            z="Please, check your internet connection....";
                        }else{
                            String query = "select * from users where user='"+emailstring+"'and password='"+passstring+"'";
                            Statement stmt = con.createStatement();
                            ResultSet rs = stmt.executeQuery(query);
                            while(rs.next())
                            {
                                em = rs.getString(0);

                                pass = rs.getString(1);

                                if(em.equals(emailstring) && pass.equals(passstring))
                                {
                                    isSuccess = true;
                                    z = "Login successful...";
                                }
                                else isSuccess = false;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        isSuccess = false;
                        z = "Exceptions"+ex;
                    }
                }
                return z;
            }
            @Override
            protected void onPostExecute (String s){
                Toast.makeText(getBaseContext(),""+z, Toast.LENGTH_LONG).show();
                progressDialog.hide();
            }
        }
}

我得到这个输出:“请,检查你的互联网连接…”。
此消息只应在con=null时触发。如何解决?

insrf1ej

insrf1ej1#

我也面临同样的问题。。。主要问题是你的连接类。

String url = "jdbc:mysql://localhost/events-test";
    public String un = "root";
    public String password = "password";
    public String db = "users";

这是您的代码,当您使用root和localhost时,它会产生检查您的internet连接问题。所以要解决这个问题,你必须创建一个新的用户,而不是localhost,使用你的internet的ip地址。
要创建新用户,如果您使用的是phpmyadmin,请单击“权限”选项卡,在那里可以看到添加用户选项。在那里创建用户,将所有数据库权限授予该用户并保存它。然后使用用户名和密码,而不是“root”和“password”。
现在要获取ip地址,请转到命令提示符,键入ipconfig,您将找到ip地址。在上面的代码中,输入ip地址,而不是localhost,例如,
字符串url=“jdbc:mysql://192.168.0.12/events test“;
您必须在androidmanifest.xml中提供互联网访问权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

这应该能解决你的问题。

u91tlkcl

u91tlkcl2#

android不支持mysql或sqlserver:-您可以使用sqllitehttps://www.tutorialspoint.com/android/android_sqlite_database.htm 也可以通过php访问mysql:https://www.tutorialspoint.com/android/android_php_mysql.htm

相关问题