是否将我的应用程序连接到我的SQL Server数据库?

jpfvwuh4  于 2023-01-20  发布在  SQL Server
关注(0)|答案(2)|浏览(128)

我试图做一个登录屏幕,当用户的详细信息输入后,它将连接到MS SQL数据库,问题是它没有连接。我这样做是正确的方式还是有更好的方法来做到这一点?
我得到的错误。
错误:未知的服务器主机名'无法解析主机“myipaddressestDatabasetestDatabase”:没有与"主机名“关联的地址。
这是我试过的代码。

public class LoginActivity extends AppCompatActivity {

    private static String ip = "myip";
    private static String port = "myportnum";
    private static String Class = "net.sourceforge.jtds.jtbc.Driver";
    private static String database = "name";
    private static String username = "name";
    private static String password = "password";
    private static String url = "jdbc:jtds:sqlserver://"+ip+":"+port+"/"+database;

    private Connection connection = null;



    private EditText userNameET, passwordEt;
    private Button loginBTN;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            userNameET = findViewById(R.id.userNameEditText);
            passwordEt = findViewById(R.id.passEditText);

            loginBTN = findViewById(R.id.loginBtn);

            StrictMode.ThreadPolicy policy = null;
            policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

        }


   // @android.support.annotation.RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    private class DoLoginForUser extends AsyncTask<String, Void, String> {
        String emailId, password;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            emailId = userNameET.getText().toString();
            password = passwordEt.getText().toString();
           // progressBar.setVisibility(View.VISIBLE);
            loginBTN.setVisibility(View.GONE);
        }

        @Override
        protected String doInBackground(String... params) {

            try {
                ConnectionHelper con = new ConnectionHelper();
                Connection connect = ConnectionHelper.CONN();

                String query = "Select * from testDatabase where UserId='" + emailId + "'";
                PreparedStatement ps = connect.prepareStatement(query);

                Log.e("query",query);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                    String passcode = rs.getString("password");
                    connect.close();
                    rs.close();
                    ps.close();
                    if (passcode != null && !passcode.trim().equals("") && passcode.equals(password))
                        return "success";
                    else
                        return "Invalid Credentials";

                } else
                    return "User does not exists.";
            } catch (Exception e) {

                return "Error:" + e.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) {

            //Toast.makeText(signup.this, result, Toast.LENGTH_SHORT).show();
           // ShowSnackBar(result);
           // progressBar.setVisibility(View.GONE);
            loginBTN.setVisibility(View.VISIBLE);
            if (result.equals("success")) {
                SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("userdetails",0);
                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString("email",userNameET.getText().toString());

                editor.commit();

                Intent i = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(i);

            } else {
                //ShowSnackBar(result);
            }
        }
    }

    //public void ShowSnackBar(String message) {
      //  Snackbar.make(lvparent, message, Snackbar.LENGTH_LONG)
               // .setAction("CLOSE", new View.OnClickListener() {
                 //   @Override
                //    public void onClick(View view) {

              ////      }
            //    })
            //    .setActionTextColor(getResources().getColor(android.R.color.holo_red_light))

           //     .show();
   // }

    public void DoLogin(View v)
    {
        DoLoginForUser login = null;
        login = new DoLoginForUser();
        login.execute("");
    }

我期望它连接,然后带我到下一个屏幕,但错误是持久的?

2ledvvac

2ledvvac1#

您是否在手机上使用sqlserver,Microsoft SQL Server不适用于Android

hfyxw5xn

hfyxw5xn2#

错误消息“Unable to resolve host”指示您没有在连接字符串中输入正确的sql server主机名或ip,或者您尝试访问无法访问的服务器(从测试设备)。
您可以从开发计算机访问sql server吗?如果可以,您可能需要通过wifi连接测试设备。
确保设备和sql server在同一网络中。

相关问题