连接类中的sharedprefference导致IOException

时间:2016-09-02 13:36:02

标签: java android database-connection sharedpreferences

所以我试图将我的连接字符串作为共享预设,但我得到了

  

错误:网络错误IOException:无法连接到/30.25.15.232(端口1433):连接失败:EHOSTUNREACH(无主机路由)

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.StrictMode;
import android.util.Log;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionClass {
    Context context;
    private SharedPreferences setingPreferences;
    String ip;// = "30.25.15.232";
    String classs = "net.sourceforge.jtds.jdbc.Driver";
    String db; //= "testDB";
    String un;// ="test";
    String password;// = "test";
    public ConnectionClass (Context context)
    {
        this.context = context;
    }
    @SuppressLint("NewApi")
    public Connection CONN() {
        setingPreferences = context.getSharedPreferences("Settings", Activity.MODE_PRIVATE);
        ip = setingPreferences.getString("server", "");
       db = setingPreferences.getString("db", "");
        un = setingPreferences.getString("dbuser", "");
        password = setingPreferences.getString("dbpass", "");
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL = null;
        try {

            Class.forName(classs);
            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                    + "databaseName=" + db + ";user=" + un + ";password="
                    + password + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (SQLException se) {
            Log.e("ERRO", se.getMessage());
        } catch (ClassNotFoundException e) {
            Log.e("ERRO", e.getMessage());
        } catch (Exception e) {
            Log.e("ERRO", e.getMessage());
        }
        return conn;
    }
}

但是,如果我只是直接从类中执行连接字符串(因为我已经在//之后键入),那么我可以在没有问题的情况下登录

我在文本框中输入了

server 30.25.15.232 
db testDB
dbuser test
dbpas test

0 个答案:

没有答案