Sunday, April 29, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...