gpt4 book ai didi

java - 如何将值从 Activity 类传递到 android 中的 java 类?

转载 作者:行者123 更新时间:2023-11-29 03:32:49 25 4
gpt4 key购买 nike

我编写了一个程序,其中我通过 sharedpreferences 放置了一些值并通过 sharedpreferences 获取它,还有一个来自 intent ,并且这些值显示得非常完美,我通过 Toast 对其进行了测试现在我想将这些值存储在 mysql 数据库中,为此我编写了一个普通的 java 类,其中包含 jdbc 准备好的语句,但我无法弄清楚如何将值从我的 Activity 类传递到普通的 java 类

在下面发布我的代码,请告诉我我需要做什么

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

}

在上面的类中,我获得的值存储在 String str , emailid , registrationid我想在下面的类中设置这些值,prep.setString(1,"") 等等

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

请帮忙谢谢你

最佳答案

读取其他类中的 sharedPreference 值并将这些值写入数据库中。

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

关于java - 如何将值从 Activity 类传递到 android 中的 java 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27928792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com