我曾尝试使用jtds将我的Android应用程序连接到SQL Server,但它会出现登录错误。
我在我的libs文件夹中使用了jtds1.2.7-jar
我的代码是:
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());
}
});
}
}
答案 0 :(得分:7)
检查连接网址中的passward
参数。它应该是password
吗?
答案 1 :(得分:0)
尝试:
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
String connString = "jdbc:jtds:sqlserver://<ip>:1433/<databasename>;encrypt=false;user=<username>;password=<password>;instance=SQLEXPRESS;";
con = DriverManager.getConnection(connString);