-1

I'm Trying to get my sql server information from SharedPreferences but cant seem to get it working in me connectioclass if i would do the same in een extended class it would work but for this code i dont want a extended class:

Normaly i use:

PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences sharedPreferences =

so i tryed:

Context context = this;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

and:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

But not working

Thi is my code:

public class ConnectionClass{

PreferenceManager.getDefaultSharedPreferences(this);
String servername = sharedPreferences.getString("SQLSERVER", "");
String databasenaam = sharedPreferences.getString("SQLDATABASE", "");
String serverusernaam = sharedPreferences.getString("SQLUSERNAAM", "");
String serverpassword = sharedPreferences.getString("SQLPASSWORD", "");

String ip = servername;
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = databasenaam ;
String un = serverusernaam;
String password = serverpassword ;

@SuppressLint("NewApi")
public Connection CONN() {

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

}
Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
user2433624
  • 59
  • 1
  • 6

2 Answers2

0

Since getDefaultSharedPreferencesName() needs Context as argument, most classes not extending Context nor Activity (incl. subclasses) are out of luck while passing this as argument. This shall now be pretty clear why this happens and knowing that you know what would be proper substitutio: Context or subclasses. In you case I'd try to call getApplicationContext()

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

Your problem arises because your class is custom and does not extend the Context class. The method getDefaultSharedPreferences takes a Context object as parameter.

You could do it like this: The moment you create a ConnectionClass object pass the context of the Activity you are calling from to the constructor.

ConnectionClass c = new ConnectionClass(this); // this is instance of calling Activity

Then initialize the fields of your choice from SharedPreferences inside of your constructor.

public ConnectionClass(Context context) {
   SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
   servername = sharedPreferences.getString("SQLSERVER", "");
   // ... and so on
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121