I am connecting android to SQLServer directly (I know its not recomended)
I have written following code:
public class MainActivity extends Activity {
I have Sucessfully done the Connection with JTDC Android to SQLServer.
Whoever wants to make connection Direct to SQLServer with android should first remember that "This is not at all the secure way".
Steps I followed:
Remember, JDBC creates SSL problems, JTDC drivers are open source and secure. So use JTDC Drivers.
While using JTDC driver make sure that you are using it with proper version. i.e. JTDC drivers version should match or be confirtable with java version. I used jtds-1.2.5-dist
you can get it from anywhere on net.
Paste Jar file to libs folder.
Go to Java Build Path (Right Click on project > Properties) In libraries > Add Externl jars. Import Jar file.
In Order And Export > Check on jtds jar as follows:
(other jars you are watching in pic are not useful)
6 You will have to set SQLServer Login Authentication (Windows Authentication for SQLServer) Screenshot as follows:
7 Configure your SQLServer as follows:
Make TCP/IP and VIA connection enabled.
In TCP/IP Properties Change The port to 1433
8 Then you will have to make Connection URL as:
url="jdbc:jtds:sqlserver://10.0.2.2:1433;instanceName=14GRAFICALI\MSSQLSERVER2008;DatabaseName="+DBName+";integratedSecurity=true;user="+UserName+";password="+Password;
9 From Here you can simply follow the code i give you:
public class gaConnection
{
String url ="";
Connection conn=null;
Statement statement=null;
ResultSet resultSet=null;
public void setConnection(String DBName,String UserName,String Password)
{
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
url ="jdbc:jtds:sqlserver://10.0.2.2:1433;instanceName=14GRAFICALI\\MSSQLSERVER2008;DatabaseName="+DBName+";integratedSecurity=true;user="+UserName+";password="+Password;
conn =DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
}
public String showResult()
{
String strResult="";
try
{
statement=conn.createStatement();
resultSet=statement.executeQuery("select * from UserMaster");
while(resultSet.next()){
strResult = strResult + " Name : "+resultSet.getString(1)+" SirName : "+resultSet.getString(2) + "\n";
}
}
catch (Exception e) {
e.printStackTrace();
}
return strResult;
}
}
10 I have made Class For Connection and instantiated it as follows:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvData=(TextView)findViewById(R.id.tvSelectedData);
String DBName="AndroidDB";
String UserName="sa";
String Password="ok";
try {gaConnection con=new gaConnection();
con.setConnection(DBName,UserName,Password);
tvData.setText(con.showResult());
} catch (Exception e) {
e.printStackTrace();
tvData.setText(e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}