java.sql.SQLException: Invalid object name 'tablename' in android studio

1.4k Views Asked by At

I'm trying to Make a connection android studio to the database in SQL server 2014 , But this error appears :

java.sql.SQLException: Invalid object name 'tablename'

I use the :jtds 1.3.1 and :sqljdbc4-2.0

I connect a local network .

1

There are 1 best solutions below

0
Hamza Rasheed On

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