如何使用android连接到sql server express

如何使用android连接到sql server express,android,Android,首先,我想让你知道,我真的很努力地寻找我的问题,但我什么也没找到。。。 我无法使用android应用程序连接到sql serverexpress实例,但我尝试连接jdbc:jtds:sqlserversourceforge的驱动程序。 我不会忘记在manifest文件中添加这一行: <uses-permission android:name="android.permission.INTERNET" /> public class MainActivitySF extends Ac

首先,我想让你知道,我真的很努力地寻找我的问题,但我什么也没找到。。。 我无法使用android应用程序连接到
sql server
express实例,但我尝试连接
jdbc:jtds:sqlserver
sourceforge的驱动程序。 我不会忘记在
manifest
文件中添加这一行:

<uses-permission android:name="android.permission.INTERNET" />
public class MainActivitySF extends Activity {
    private Connection connection;
    private Statement statement;
    private ResultSet resultat;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void OnClick(View v) {
        TextView tv = (TextView) findViewById(R.id.textView1);
        EditText ehost = (EditText) findViewById(R.id.ehost);
        EditText eport = (EditText) findViewById(R.id.eport);
        EditText ebase = (EditText) findViewById(R.id.ebase);
        EditText euser = (EditText) findViewById(R.id.euser);
        EditText epasswd = (EditText) findViewById(R.id.epasswd);
        switch (v.getId()) {
        case  R.id.Connection:
            try {
                Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
            } catch (InstantiationException e){
                Toast.makeText(this, "Instantiation failled",
                        Toast.LENGTH_LONG).show();
                return;
            } catch (ClassNotFoundException e1) {
                Toast.makeText(this, "Instantiation failled",
                        Toast.LENGTH_LONG).show();
                return;
            } catch(IllegalAccessException e2){
                Toast.makeText(this, "Instantiation failled",
                        Toast.LENGTH_LONG).show();
                return;
            }
            try {
                String connString = "jdbc:jtds:sqlserver://"+ehost.getText().toString()+"" +
                                    ":"+eport.getText().toString()+"/"+ebase.getText().toString()+"" +
                                    ";encrypt=false;user="+euser.getText().toString()+";" +
                                    "password="+epasswd.getText().toString()+";instance=SQLEXPRESS;";
                String username = euser.getText().toString();
                String password = epasswd.getText().toString();
                connection = DriverManager.getConnection(connString,username,password);


            } catch (SQLException e) {
                connection = null;
                Toast.makeText(this, "connection failled",
                        Toast.LENGTH_LONG).show();
                tv.setText(e.getLocalizedMessage());
                return;
            }
            tv.setText("connection OK !!");
            break;

        case  R.id.statement:
            try {
                statement = connection.createStatement();
            } catch (SQLException e) {
                statement=null;
                Toast.makeText(this, "statement failled",
                        Toast.LENGTH_LONG).show();
                return;
            }
            tv.setText("statement OK !!");
            break;

        case  R.id.req:
            try {
                resultat = statement.executeQuery(
                        "SELECT * FROM produits");
                tv.setText(resultat.getString("LIB_PRODUIT")+ " OK !!");
            } catch (SQLException e) {
                Toast.makeText(this, "req failled",
                        Toast.LENGTH_LONG).show();
                return;
            }
            break;
        default:
            break;
        }
    }
}