Sunday, 29 April 2018

sql server - connect android to MSSQL db using jtds.jdbc driver to specific port

step by step instructions of how to connect android app to MSSQL database on Windows machine.



struggling to find a right solution for connecting to MSSQL database from android using
jtds.jdbc driver, what makes it worst is that it is not clear for me whether the problem is with database or with jtds.jdbc driver.



public class DBTestActivity extends Activity {         

private static final String url = "jdbc:jtds:sqlserver://10.0.2.2:1433;instance=testdb;DatabaseName=androidtest";
private static final String user = "test\'testuser";

private static final String pass = "testlog";

TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.dbconnect);


final Button button = (Button) findViewById(R.id.connect);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("test3", "text4");
// Perform action on click
Connect();
tv = (TextView) findViewById(R.id.db_text);
}
});


}

private class Connect extends AsyncTask {
@Override
protected String doInBackground(String... urls) {
String response = "";

try {

Log.d("test5", "text5");

Class.forName("net.sourceforge.jtds.jdbc.Driver");
Log.d("test77", "text77");

Connection con = DriverManager.getConnection(url, user, pass);

Log.d("test12", "text12");
String result = "Database connection success\n";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from dbo.Test_Manufacturer");
ResultSetMetaData rsmd = rs.getMetaData();*/


while(rs.next()) {
result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
}
tv.setText(result);
}
catch(Exception e) {
e.printStackTrace();

// tv.setText(e.toString());
}
Log.d("test20", "text212");

return response;

}

@Override
protected void onPostExecute(String result) {

Log.d("test2", "text2");
tv.setText(result);
}
}

public void Connect() {

Log.d("test", "text1");
Connect task = new Connect();
task.execute();


}

}


Got different sort of errors:




  • SQLException: server 10.0.2.2 has no instance named ......


  • ECONNREFUSED

  • COULD NOT FIND METHOD ORG.IETF.JGSS.GSSCONTEXT.INITSECCONTEXT

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...