1

首先,我不想让你知道我真的很努力地寻找我的问题,但我什么也没找到……我无法使用 android 应用程序连接到expresssql server实例,但是我尝试连接. 我不要忘记在文件中添加这一行:jdbc:jtds:sqlserversourceforgemanifest

<uses-permission android:name="android.permission.INTERNET" />

并且错误消息是无法从中获取信息sql server :HostName

这里我的源代码可能有问题:

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;
        }
    }
}
4

0 回答 0