3

hi im trying to connect to my MS SQL Server 2008 R2 from my Android application this is the code:

try {
    Log.i("Login", "Establishing Connection...");
    // SET CONNECTIONSTRING
    Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
    Log.i("JDBC","found");
    Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://MYPC/" + DB + ";instance=SQLEXPRESS;user=" + USERNAME + ";password=" + PASSWORD);

    Log.i("Login","Connected");
    Statement stmt = DbConn.createStatement();
    ResultSet insert = stmt.executeQuery("insert into UserLogin(username, password) values (admin, admin);");
    ResultSet reset = stmt.executeQuery(" select * from UserLogin ");

    Toast.makeText(this, reset.getString(1), Toast.LENGTH_SHORT).show();

    DbConn.close();

    // go to newsfeed
} catch (Exception e) {
    Log.e("Error connection","" + e.getMessage());
}

im getting an error at this line:

Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://MYPC/" + DB + ";instance=SQLEXPRESS;user=" + USERNAME + ";password=" + PASSWORD);

with Error Connection null in the Logcat. Im using jtds-1.3.1.jar. i have the user defined in the database instance and the actual database. i have TCP/IP enabled, and i have i have Windows and SQL authentication enabled as well as allowed remote connections. ive looked up other posts but no help. not sure what's wrong, any ideas?

user1459976
  • 207
  • 6
  • 14
  • I am just curious to know why you would query SQL server directly, is there any scenario where a web service wouldn't be better? – meda Apr 29 '14 at 01:54
  • Please read: http://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android – Morrison Chang Apr 29 '14 at 01:55
  • meda - its just for testing purposes. will probably implement a web service in the future – user1459976 Apr 29 '14 at 01:59
  • I am facing the same problem as well, did you try changing the jar file version? and see if it works for you? I changed the jar file to another version and i get the char error – AuroraBlaze Nov 10 '14 at 09:10

0 Answers0