简体   繁体   中英

how to pass a value from an activity class to a java class in android?

I have written a program in which i am putting some values through sharedpreferences and getting it through sharedpreferences and one from intent , and the values are showing perfectly which i tested it through Toast Now i want to store these values inside mysql database, for that i have written a plain java class which has jdbc prepared statements , but i am not able to figure out how to pass a value from my activity class to a plain java class

Posting my code below , please suggest me what do i need to do

public class Service_Access extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.service_access);

    // Get Email ID from Shared preferences
    // SharedPreferences prefs = getSharedPreferences("UserDetails",
    // Context.MODE_PRIVATE);
    // String eMailId = prefs.getString("eMailId", "");

    // Get Email ID from Shared preferences
    SharedPreferences prefs = getSharedPreferences("UserDetails", Context.MODE_PRIVATE);
    String eMailId = prefs.getString("eMailId", "");
    String registrationId = prefs.getString("regId", "");
    // Intent Message sent from Broadcast Receiver
    String str = getIntent().getStringExtra("msg");

    if (str != null) {
        // Set the message
        lblMessage = (TextView) findViewById(R.id.txtView);
        lblMessage.setText(str + "\n");
        // Toast.makeText(getBaseContext(), registrationId + "\n" + eMailId
        // + "\n", Toast.LENGTH_LONG).show();
        ServiceSql sersql = new ServiceSql();
        sersql.insertValues(eMailId, registrationId, str);
    }

}

}

in the above class , the values which i obtained are stored in String str , emailid , registrationid and i want to set these values in the below class , prep.setString(1,"") and so on

ServiceSql.java

public class ServiceSql {
Connection connection = null;
PreparedStatement prep = null;
ResultSet rSet = null;
Statement state = null;
java.sql.Timestamp timestamp = getCurrentJavaSqlTimestamp();
Context context;

public static java.sql.Timestamp getCurrentJavaSqlTimestamp() {
    java.util.Date date = new java.util.Date();
    return new java.sql.Timestamp(date.getTime());
}

public void readDatabase() {
    try {

        Class.forName("com.mysql.jdbc.Driver");
        connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/gcm",
        "root", "root");
        state = (Statement) connection.createStatement();

        prep = (PreparedStatement) connection
                .prepareStatement("Select email,gcm_regid,created_at,gcm_message from  
        gcm.gcm_users");
        rSet = prep.executeQuery();

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

public void insertValues(String email, String regID, String msg) {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/gcm",
        "root", "root");
        state = (Statement) connection.createStatement();

        prep = (PreparedStatement) connection
                .prepareStatement("insert into 
        gcm.gcm_users(email,gcm_regid,created_at,gcm_message)"
                        + "values(?,?,?,?)");

        prep.setString(1, email);
        prep.setString(2, regID);
        prep.setTimestamp(3, timestamp);
        prep.setString(4, msg);
        prep.executeUpdate();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

}

please need some help Thanking You

Read the sharedPreference values in the other class and write those values in the database.

SharedPreferences s = SharedMyAPP.getAppContext().getSharedPreferences("myPreferences", Context.MODE_PRIVATE); value = s.getString(key, "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM