Search code examples
androidsqlsql-serverjdbcjtds

"Invalid object name" when referring to a table


I am trying to connect to SQL server database from Android app. Connection works but I get expection because of Table

java.sql.SQLException: Invalid object name 'dbo.Names'.

The SQL code I send to the server is

stmt.execute("INSERT INTO [dbo].[Names] ([name]) VALUES ('Test')");

Any ideas?

EDIT:

Here is the code:

static String serverIp = "xxx.database.windows.net:1433";
static String serverDb = "xxxApp";
static String serverUserName = "xxx";
static String serverPassword = "xxx";

Connection con;

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

    CheckLogin checkLogin = new CheckLogin();
    checkLogin.execute("");

}

public class CheckLogin extends AsyncTask<String, String, String> {

    String z = "";
    Boolean isSuccess = false;

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onPostExecute(String r) {

    }

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

        try {
            con = connectionclass(serverUserName, serverPassword, serverDb, serverIp);

            if (con == null) {
                z = "Check Internet Access";
            } else {

                Statement stmt = con.createStatement();
                stmt.execute("INSERT INTO [dbo].[Names] ([name]) VALUES ('Test')");

                String x = ""; // DEBUG point
            }
        } 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=" + database + ";user=" + user + ";password=" + password + ";encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";

            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;
    }

}

Solution

  • The SQL statement is failing because you are not using the correct connection URL format for jTDS so you are not actually connecting to the database specified by the String variable serverDb.

    You are trying to use a connection URL parameter named database that jTDS does not recognize:

    String serverDb = "myDb";
    String connUrl = "jdbc:jtds:sqlserver://localhost:49242;database=" + serverDb;
    try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
        System.out.println(conn.getCatalog());  // prints: master
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    

    Instead, you should be using the server:port/database format described in the documentation

    String serverDb = "myDb";
    String connUrl = "jdbc:jtds:sqlserver://localhost:49242/" + serverDb;
    try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
        System.out.println(conn.getCatalog());  // prints: myDb
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }