2

I have an application that requires a connection to a MS SQL Server 2008 database. Now I have the jdbc driver and I have been struggling to get it to connect successfully. This is my connection code:

try {
    Class.forName("net.sourceforge.jtds.jdbc.Driver");
    int duration = Toast.LENGTH_SHORT;
    Connection con = DriverManager.getConnection(jdbc:jtds:sqlserver://41.76.209.156:1433; databaseName=prooptin_ProOptData", "username", "password");
    textview7.setText(con.toString());
    textview7.setText("Successful");
} catch (ClassNotFoundException e) {
    textview7.setText("Could not find the database driver " + e.getMessage());

} catch (SQLException e) {         
    textview7.setText("Could not connect to the database " + e.getMessage());

} catch (Exception e) {
    textview7.setText(e.getMessage());
}

It seems to hang at the DriverManager.getConnection() line. I have tried to alter the connection string, and tried it with and without the username and password and database name, and nothing will work. It does not print an exception message, just returns a blank message and nothing happens.

halfer
  • 19,824
  • 17
  • 99
  • 186
barnacle.m
  • 2,070
  • 3
  • 38
  • 82
  • Perhaps a firewall issue? If the SQL server doesn't accept connections, something like this will happen. – chkal Aug 19 '13 at 10:52
  • Have you checked error messages on DB server side? – Guillaume Aug 19 '13 at 10:54
  • I can connect with my ASP.net web app with the same credentials I used there, so it does accept connections. It is shared hosting so I have no way of checking server side. – barnacle.m Aug 19 '13 at 10:58
  • 1
    I'm not sure you can use JDBC on Android. Check [this](http://stackoverflow.com/questions/7221620/android-jdbc-not-working-classnotfoundexception-on-driver/7221716#7221716) out. – aim Aug 19 '13 at 11:03
  • 1
    I desperately need to connect this app to a database though. That answer doesn't show me how. – barnacle.m Aug 19 '13 at 11:57
  • @barnacle.m jTDS does work for me on Android so do not give up! As you can see [here](http://jtds.sourceforge.net/faq.html#urlFormat) there is no property called `databaseName`, so the database name should be given like this: "jdbc:jtds:sqlserver://41.76.209.156:1433/prooptin_ProOptData" – Bill Sep 02 '13 at 13:56
  • I decided to use an ASP.Net web service to get around it :) – barnacle.m Sep 02 '13 at 17:05

0 Answers0