0

我尝试使用 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());


        }
    });
}
}
4

2 回答 2

7

检查passward连接 URL 中的参数。应该是password吗?

于 2013-07-24T05:51:27.400 回答
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);
于 2014-07-17T22:47:04.113 回答