Click here to Skip to main content
15,912,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have tried to connect my android app to sql server using jtds but is give login error. if any one could tell me it's solution... plzz guys it's urgent........

i have use jtds1.2.7-jar in my libs folder

my code is .......
[code]
public class MainAct extends Activity{
EditText e;
Button bt;
ListView lv;
Connection connect;
SimpleAdapter sm;

public void declere(){
e=(EditText)findViewById(R.id.et1);
bt=(Button)findViewById(R.id.bt1);
lv=(ListView)findViewById(R.id.list);
}

public void initilize(){
declere();
e.setText("SELECT TOP 10 * FROM FORM1");
connect=CONN("sa","cbo@morc","KULDEEP","122.160.255.218:1433");
}
@SuppressLint("NewApi")
private Connection CONN(String user,String pass,String db,String server){
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn=null;
String connUrl=null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connUrl="jdbc:jtds:sqlserver://" + server + ";" + "databaseName=" + db + ";user=" + user + ";passward=" + pass + ";";
conn=DriverManager.getConnection(connUrl);
}catch(SQLException se){
Log.e("ERROR", se.getMessage());
}catch(ClassNotFoundException cl){
Log.e("ERROR", cl.getMessage());
}catch(Exception e){
Log.e("ERROR", e.getMessage());
}

return conn;

}

public void querySQL(String COMMANDSQL){
ResultSet rs;
try{
Statement statement=connect.createStatement();
rs=statement.executeQuery(COMMANDSQL);
List<Map<string,string>>data=null;
data=new ArrayList<Map<string,string>>();

while(rs.next()){
Map<string,string>datanum=new HashMap<string,string>();
datanum.put("a", rs.getString("NAME"));
datanum.put("b", rs.getString("CITY"));
data.add(datanum);
}
String [] from={"a","b"};
int [] to={R.id.tv1,R.id.tv2};

sm=new SimpleAdapter(this, data, R.layout.second, from, to);
lv.setAdapter(sm);
}catch(Exception e){
Log.e("ERROR", e.getMessage());
}

}
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main);
initilize();

bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
querySQL(e.getText().toString());


}
});
}

}

[/code]
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900