Integration of SQL Server with Android

Asked

Viewed 1,443 times

0

I’m having trouble connecting to Sql Server and Android. When I try to communicate, the message appears: "No address Associated with hostname." I’ve done everything I needed, already downloaded the Drive jtds, downloaded the sql server 2017, and could not make the communication, if anyone can give me a help, I will thank.

Full error message: Unknown server host name 'Unable to resolve host "192.168.1.3projectodendroid": No address Associated with hostname'.

      ip = "192.168.1.3";
    db = "projetoandroid";
    un = "sa";
    pass = "123";

    login.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            CheckLogin checkLogin = new CheckLogin();
            checkLogin.execute("");
        }
    });

}

public class CheckLogin extends AsyncTask<String,String,String>
{
    String z = "";
    Boolean isSuccess = false;

    @Override
    protected void onPreExecute()
    {
        //progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String r)
    {
       // progressBar.setVisibility(View.GONE);
        Toast.makeText(ParaTeste.this, r, Toast.LENGTH_SHORT).show();
        if(isSuccess)
        {
            Toast.makeText(ParaTeste.this , "Login Successfull" , Toast.LENGTH_LONG).show();
            //finish();
        }
    }
    @Override
    protected String doInBackground(String... params)
    {
        String usernam = username.getText().toString();
        String passwordd = password.getText().toString();
        if(usernam.trim().equals("")|| passwordd.trim().equals(""))
            z = "Please enter Username and Password";
        else
        {
            try
            {
                con = connectionclass(un, pass, db, ip);        // Connect to database
                if (con == null)
                {
                    z = "Check Your Internet Access!";
                }
                else
                {
                    // Change below query according to your own database.
                    String query = "select * from cliente where nome= '" + usernam.toString() + "' and cpf = '"+ passwordd.toString() +"'  ";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    if(rs.next())
                    {
                        z = "Login successful";
                        isSuccess=true;
                        con.close();
                    }
                    else
                    {
                        z = "Invalid Credentials!";
                        isSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                z = ex.getMessage();
            }
        }
        return z;
    }
}


@SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server)
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection = null;
    String ConnectionURL = null;
    try
    {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user+ ";password=" + password + ";";
        connection = DriverManager.getConnection(ConnectionURL);
    }
    catch (SQLException se)
    {
        Log.e("error here 1 : ", se.getMessage());
    }
    catch (ClassNotFoundException e)
    {
        Log.e("error here 2 : ", e.getMessage());
    }
    catch (Exception e)
    {
        Log.e("error here 3 : ", e.getMessage());
    }
    return connection;
}
  • and select host ? where is the code you are using ?

  • I put it there now. I forgot to put it. Is it some problem in sql server version?

  • I think you’re wrong here: + server + database + in the end it would be: 192.168.1.3projetoandroid and it will go wrong

  • I have already put a '/' also for forehead, I made some changes, similar type does to access the wamp, but he is just not able to recognize the hostname. I don’t know if this may be a problem with the sql server version. I’ve seen other examples that worked great, but mine is not working at all.

  • which string value at runtime ?

1 answer

1

I think you’re missing your connection string:

  ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user+ ";password=" + password + ";";

Should be:

  ConnectionURL = "jdbc:jtds:sqlserver://" + server + ":" + porta+";databaseName=" + database + ";user=" + user+ ";password=" + password + ";";
  • It seems that this way it was right, the message did not appear again no, but this appears from here. [ 03-09 15:20:03.754 25479:25665 W/ ] Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory W/art: Failed to open zip Archive '/system/framework/qcom.fmradio.jar': I/O Error

  • now it is already a file system error... I have no practice with android, I can’t help you in this =/

  • Easy brother, I’ll see what I can do. vlw!

  • @Hiagovenancio if the answer solved the problem reported, I think it can mark it. Then for the other problem, you open another question with specific data about it (environment, files, framework, etc...)

  • Still not working, this message I sent there was already appearing before, the file directory. I looked here now, the error message keeps popping up, just taking longer to appear. I understood nothing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.