我已经建立了数据库连接,其中id将在db中自动生成。对于eg.10001,当用户在表单中注册并提交表单时,我已经创建了一个表单,并将其存储在数据库中,并将邮件发送到电子邮件中。但我需要将自动生成的ID发送到他们的电子邮件,以便从数据库中获取id。
@Override
public void onClick(View v) {
if (v == Register) {
registerUser();
}
}
private void registerUser() {
int selectedId = Gender.getCheckedRadioButtonId();
Selection = (RadioButton) findViewById(selectedId);
if (Name.getText().toString().equals("")) {
Name.setError("Enter your name");
Name.requestFocus();
} else if (Dob.getText().toString().equals("")) {
Dob.setError("Enter Date of Birth");
Dob.requestFocus();
} else if (Collegeid.getText().toString().equals("")) {
Collegeid.setError("Enter your CollegeID");
Collegeid.requestFocus();
} else if (Education.getSelectedItem().toString().equals("")) {
Toast.makeText(RegisterFormActivity.this, "choose your education", Toast.LENGTH_SHORT).show();
} else if (Year.getText().toString().equals("")) {
Year.setError("Enter Year");
Year.requestFocus();
} else if (Address.getText().toString().equals("")) {
Address.setError("Enter Your Address");
Address.requestFocus();
} else if (Pincode.getText().toString().equals("")) {
Pincode.setError("Enter Pincode");
Pincode.requestFocus();
} else if (Mobile.getText().toString().equals("")) {
Mobile.setError("Enter your Mobile Number");
Mobile.requestFocus();
} else if (!validatePhone(Mobile.getText().toString())) {
Mobile.setError("Invalid Mobile Number");
Mobile.requestFocus();
} else if (landline.getText().toString().equals("")) {
landline.setError("Enter your landline Number");
landline.requestFocus();
} else if (Email.getText().toString().equals("")) {
Email.setError("Enter your email");
Email.requestFocus();
} else if (Fbid.getText().toString().equals("")) {
Fbid.setError("Enter your facebookId");
Fbid.requestFocus();
} else if (Occupation.getSelectedItem().toString().equals("")) {
Toast.makeText(RegisterFormActivity.this, "Fathers occupation", Toast.LENGTH_SHORT).show();
}
else if(Selection.getText().toString().equals(""))
{
Toast.makeText(RegisterFormActivity.this, "choose your gender", Toast.LENGTH_SHORT).show();
}
else if (Business.getText().toString().equals("")) {
Business.setError("please specify details");
Business.requestFocus();
} else if (Shortgoal.getText().toString().equals("")) {
Shortgoal.setError("Your short term goal");
Shortgoal.requestFocus();
} else if (Longgoal.getText().toString().equals("")) {
Longgoal.setError("Your long term goal");
Longgoal.requestFocus();
} else if (Size.getText().toString().equals("")) {
Size.setError("Enter T-shirt Size");
Size.requestFocus();
} else if (Reference.getText().toString().equals("")) {
Size.setError("Enter Reference name");
Size.requestFocus();
}
else if(Form.getText().toString().equals("")){
Form.setText("Enquiry Form");
}
else {
String sname = Name.getText().toString().trim();
String dob = Dob.getText().toString().trim();
String cid = Collegeid.getText().toString().trim();
String edu = Education.getSelectedItem().toString();
String year = Year.getText().toString().trim();
String addr = Address.getText().toString().trim();
String pin = Pincode.getText().toString().trim();
String mob = Mobile.getText().toString().trim();
String lan = landline.getText().toString().trim();
String email = Email.getText().toString().trim();
String fbid = Fbid.getText().toString().trim();
String focc = Occupation.getSelectedItem().toString();
String bus= Selection.getText().toString().trim();
String spe = Business.getText().toString().trim();
String stgoal = Shortgoal.getText().toString().trim();
String ltgoal = Longgoal.getText().toString().trim();
String tsize = Size.getText().toString().trim();
sub = Form.getText().toString().trim();
// String ref = Reference.getText().toString().trim();
// String gender = Selection.getSelected().toString().toLowerCase();
register(sname, dob, cid, edu, year, addr, pin, mob, lan, email, fbid, focc, bus, spe, stgoal, ltgoal, tsize, ref);
msg = "<p><b>Name: </b>" + sname + "</p><p><b>Dob: </b>" + dob +
"</p><p><b>Collegeid: </b>" + cid +"</p><p><b>Education: </b>" + edu +"</p><p><b>Year: </b>" + year +
"</p><p><b>Address: </b>" + addr +"</p><p><b>Pincode: </b>" + pin +"</p><p><b>Mobile: </b>" + mob +"</p><p><b>landline: </b>" + lan +"</p><p><b>Email: </b>" + email +
"</p><p><b>Fbid: </b>" + fbid +"</p><p><b>Occupation: </b>" + focc +"</p><p><b>Selection: </b>" + bus +"</p><p><b>Business: </b>" + spe +"</p><p><b>Shortgoal: </b>" + stgoal +
"</p><p><b>Longgoal: </b>" + ltgoal +"</p><p><b>Size: </b>" + tsize +
"</p><p><b>Reference: </b>" + Reference.getText().toString();
Properties props = new Properties();
props.put("mail.smtp.host", "md-in-13.webhostbox.net");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});
if(DetectConnection
.checkInternetConnection(RegisterFormActivity.this)){
//Toast.makeText(RegisterFormActivity.this, "mailn", Toast.LENGTH_LONG).show();
RetrieveFeedTask task = new RetrieveFeedTask();
task.execute();
}
else
{
Toast.makeText(RegisterFormActivity.this, "No internet connection", Toast.LENGTH_LONG).show();
}
}
}
private void register(String sname, String dob, String cid, String edu, String year, String addr, String pin, String mob, String lan, String email, String fbid, String focc, String bus, String spe, String stgoal, String ltgoal, String tsize, String ref) {
String urlSuffix = "?sname=" + sname + "&dob=" + dob + "&cid=" + cid + "&edu=" + edu + "&year=" + year + "&email=" + email + "&mob=" + mob + "&addr=" + addr + "&lan=" + lan + "&pin=" + pin + "&fbid=" + fbid + "&focc=" + focc + "&bus=" + bus + "&spe=" + spe + "&stgoal=" + stgoal + "<goal=" + ltgoal + "&tsize=" + tsize + "&ref=" + ref;
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(RegisterFormActivity.this, "Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
// Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
try {
if (s.equals("1")) {
Toast.makeText(RegisterFormActivity.this, "success", Toast.LENGTH_SHORT).show();
} else if (s.equals("0")) {
Toast.makeText(RegisterFormActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
}
}
@Override
protected String doInBackground(String... params) {
String s = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(REGISTER_URL + s);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result = bufferedReader.readLine();
return result;
} catch (Exception e) {
return null;
}
}
}
RegisterUser ru = new RegisterUser();
ru.execute(urlSuffix);
}
public class RetrieveFeedTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("username"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.
parse("mailid"));
// message.setRecipients(Message.RecipientType.CC, InternetAddress.
// parse("mailid"));
message.setSubject(sub);
//message.setContent(names, "text/html; charset=utf-8");
message.setContent(msg, "text/html; charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:1)
Db insert
函数将在插入后返回最后插入的ID,如果有任何错误,则返回-1
。
用此来获取ID。
long id = db.insert(...);