How to connect Android application to SQL Server through jbdc?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 05:27:59

问题


I have tried to connect my Android app to SQL Server using jtds but it gives a login error.

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

My code is:

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());


        }
    });
}
}

回答1:


Check your passward param in the connection URL. Should it be password?




回答2:


Try:

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);


来源:https://stackoverflow.com/questions/17826096/how-to-connect-android-application-to-sql-server-through-jbdc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!