0

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 .

3
  • Do you literally mean "from Android studio", or from an Android app? If an app, it is not recommended to connect to a SQL database from a mobile app Commented Feb 4, 2019 at 17:59
  • i,m new programming android , i use Android studio for building android app , when i run app , The error appears ,I do not know the problem Commented Feb 4, 2019 at 18:05
  • Okay. My point still stands. Do not use SQL Server drivers within your Android app. Use a webservice. Expose a REST API to your database over your network -- stackoverflow.com/questions/15853367/… Commented Feb 4, 2019 at 19:15

1 Answer 1

1

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