1

I'm making an app in android studio with connection to a SQL database server, I have a problem in connecting to the database.

Code:


  import android.util.Log; import java.sql.Connection; import           java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import net.sourceforge.jtds.jdbc.*;
    Log.i("Android", " MySQL Connect Example.");
    Connection conn = null;
    try {
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        Class.forName(driver).newInstance();
        //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
     //String connString = "jdbc:jtds:sqlserver://localhost:1433/quehojaes;encrypt=false;user=Pc-PC;password=;instance=SQLEXPRESS;";
       //  String connString = "Data Source=localhost:1433;Initial Catalog=quehojaes;Integrated Security=True";
        String connString ="jdbc:jtds:sqlserver://localhost:1433/quehojaes;";


        String username = "Pc-PC";
        String password = "";
        conn = DriverManager.getConnection(connString);
        Log.w("Connection", "open");
        Statement stmt = conn.createStatement();
        ResultSet reset = stmt.executeQuery("select * from planta where id=1");

        //Print the data to the console
        while (reset.next()) {
             dato = reset.getString(3);
            Log.w("Data:", reset.getString(3));
        Log.w("Data",reset.getString(2));

        }
        conn.close();

    } catch (Exception e) {
        Log.w("Error connection", "" + e.getMessage());
    }


    return dato;
}

.................................

I got an error at line (conn = DriverManager.getConnection (connString)), so I guess it is wrong user I'm trying to get into the database, I enter Windows user authentication with a local database and I have no password for that user called Pc.

There are lines discussed by failed login attempts I've tried.

Thanks for the help!

1 Answer 1

2

localhost means your current machine. That would be the phone. Since SQLServer isn't running on your phone, its the wrong string. Use your PCs IP address, and make sure you can access that port through any ISP or personal firewalls.

As an aside- this is a HORRIBLE way to do things. You have to put your password the the SQL server in your app. Its trivial to decompile it and own your data. Instead you should put up a web service inbetween the two, so only machines you own on your local network need to have the db password.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.