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 .

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 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 – OneCricketeer Feb 04 '19 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 – mohammd qandeel Feb 04 '19 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 -- https://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android – OneCricketeer Feb 04 '19 at 19:15

1 Answers1

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);
}
Hamza Rasheed
  • 179
  • 1
  • 2